Discussion:
one-dimensional integration over two-dimensional function
(too old to reply)
Patrick David
2008-03-10 15:46:47 UTC
Permalink
Hello out there,

I used MATLAB a few times but I am still beginner. Here is my problem:

function retval = foo(x,y)
retval = x + y;
endfunction

I want to calculate the integration of foo in the limits from x=3 to x=5, y
shall be konstant (lets say y=10)!

quad("foo(x,10)", 3, 5) --> works
quad(@(x) foo(x,10), 3, 5 --> works too
n = 10
quad("foo(x,n)", 3, 5) --> doesn't work
quad(@(x) foo(x,n), 3, 5 --> doesn't work too

I am sure the problem is really simple, but I try for about two hours. How
can I define the integral without writing the number 10 directly in the
line (I want to use it in a script, so I have to use a variable)

Thanks for your help
Greetings
Patrick
Thomas Plehn
2008-03-10 16:41:19 UTC
Permalink
Post by Patrick David
n = 10
quad("foo(x,n)", 3, 5) --> doesn't work
the last approach with the anonymous function call is correct. according
to the documentation, anonymous function calls inherit all variables not
used as parameters from the enclosing context.

see here
http://www.gnu.org/software/octave/doc/interpreter/Anonymous-Functions.html

perhaps you should consider updating your octave version to 3.0
Post by Patrick David
I am sure the problem is really simple, but I try for about two hours. How
can I define the integral without writing the number 10 directly in the
line (I want to use it in a script, so I have to use a variable)
Patrick David
2008-03-12 09:21:40 UTC
Permalink
Post by Thomas Plehn
perhaps you should consider updating your octave version to 3.0
Oh yes, the solution can be so simple...

Thanks for your help
Patrick
ricoh51
2008-03-23 23:54:17 UTC
Permalink
Post by Patrick David
n = 10
quad("foo(x,n)", 3, 5) --> doesn't work
the last approach with the anonymous function call is correct. according to
the documentation, anonymous function calls inherit all variables not used as
parameters from the enclosing context.
Hi,
I'm beginner with Octave :)
I have a similar problem : what is wrong in my code? (I want to plot
Integral of (x+n)dx ( x from 0 to n)

function retval=foo(y)
f=@(x) x+y;
retval=quad ( f, 0, y );
endfunction

n=3; <---- work
plot(n,foo(n),"ro");

n=[3:5:10]; <---- don't work
plot(n,foo(n));

Thank's

eric
--
http://ricoh51.free.fr/
David Bateman
2008-03-26 21:50:57 UTC
Permalink
Post by ricoh51
Post by Thomas Plehn
Post by Patrick David
n = 10
quad("foo(x,n)", 3, 5) --> doesn't work
the last approach with the anonymous function call is correct.
according to the documentation, anonymous function calls inherit all
variables not used as parameters from the enclosing context.
Hi,
I'm beginner with Octave :)
I have a similar problem : what is wrong in my code? (I want to plot
Integral of (x+n)dx ( x from 0 to n)
function retval=foo(y)
retval=quad ( f, 0, y );
endfunction
n=3; <---- work
plot(n,foo(n),"ro");
n=[3:5:10]; <---- don't work
plot(n,foo(n));
Thank's
eric
The quad function requires a function that for a scalar value input,
returns a scalar output.. If n=[3:5:10], then your function handle
returns a vector, even for a scalar input value, and the quad function
won't be happy. This is stated clearly in the help text of the quad
function.

D.
ricoh51
2008-03-27 11:48:17 UTC
Permalink
Post by David Bateman
Post by ricoh51
I have a similar problem : what is wrong in my code? (I want to plot
Integral of (x+n)dx ( x from 0 to n)
function retval=foo(y)
retval=quad ( f, 0, y );
endfunction
n=3; <---- work
plot(n,foo(n),"ro");
n=[3:5:10]; <---- don't work
plot(n,foo(n));
The quad function requires a function that for a scalar value input,
returns a scalar output.. If n=[3:5:10], then your function handle
returns a vector, even for a scalar input value, and the quad function
won't be happy. This is stated clearly in the help text of the quad
function.
oh, ok, thank you...
What kind of mechanism can I use? (to plot my function)

eric
--
http://ricoh51.free.fr/
David Bateman
2008-03-27 20:08:45 UTC
Permalink
Post by ricoh51
Post by David Bateman
Post by ricoh51
I have a similar problem : what is wrong in my code? (I want to plot
Integral of (x+n)dx ( x from 0 to n)
function retval=foo(y)
retval=quad ( f, 0, y );
endfunction
n=3; <---- work
plot(n,foo(n),"ro");
n=[3:5:10]; <---- don't work
plot(n,foo(n));
The quad function requires a function that for a scalar value input,
returns a scalar output.. If n=[3:5:10], then your function handle
returns a vector, even for a scalar input value, and the quad function
won't be happy. This is stated clearly in the help text of the quad
function.
oh, ok, thank you...
What kind of mechanism can I use? (to plot my function)
Don't worry about plotting it yet, try and figure out what the semantics
are to calculate what you want to calculate. "n =[3:5:10]" is a two
element vector [3,8]. So are you trying to calculate "\int_3^8 foo(x)
dx"? If you say in words if TeX what you are trying to calculate then
there might be some chance of helping you.

D.
ricoh51
2008-03-27 20:35:33 UTC
Permalink
Post by David Bateman
Post by ricoh51
Post by David Bateman
Post by ricoh51
I have a similar problem : what is wrong in my code? (I want to plot
Integral of (x+n)dx ( x from 0 to n)
function retval=foo(y)
retval=quad ( f, 0, y );
endfunction
n=3; <---- work
plot(n,foo(n),"ro");
n=[3:5:10]; <---- don't work
plot(n,foo(n));
The quad function requires a function that for a scalar value input,
returns a scalar output.. If n=[3:5:10], then your function handle
returns a vector, even for a scalar input value, and the quad function
won't be happy. This is stated clearly in the help text of the quad
function.
oh, ok, thank you...
What kind of mechanism can I use? (to plot my function)
Don't worry about plotting it yet, try and figure out what the semantics
are to calculate what you want to calculate. "n =[3:5:10]" is a two
element vector [3,8].
yes...

My real function is a bit more complex (elliptic integral) :
elliptic(x) = int_0^x {dt over sqrt{cos t - cos x}}
and plot elliptic(x) for x from 0.1 to 1.5 for example.

At this moment, I do that : (and it works fine, but is this a good
way?)

function y = elliptic(x)
f=@(t) 1./sqrt(cos(t)-cos(x));
y = quad(@(t) f(t), 0, x);
endfunction;

v=[0.1 : 0.1 : 1.5];
for x=1:15
h(x)=elliptic(v(x));
endfor;

i=[1:15];
plot(v(i),h(i));

thank's

eric
--
http://ricoh51.free.fr/
David Bateman
2008-03-27 22:56:07 UTC
Permalink
Post by ricoh51
elliptic(x) = int_0^x {dt over sqrt{cos t - cos x}}
and plot elliptic(x) for x from 0.1 to 1.5 for example.
At this moment, I do that : (and it works fine, but is this a good way?)
function y = elliptic(x)
endfunction;
v=[0.1 : 0.1 : 1.5];
for x=1:15
h(x)=elliptic(v(x));
endfor;
i=[1:15];
plot(v(i),h(i));
Frankly I think this or a variation of it is pretty much what you'd have
to do. A variation might include "arrayfun" or some such, but I don't
expect it to be any faster than what you already have.

D.
ricoh51
2008-03-28 17:20:48 UTC
Permalink
Post by David Bateman
Post by ricoh51
v=[0.1 : 0.1 : 1.5];
for x=1:15
h(x)=elliptic(v(x));
endfor;
i=[1:15];
plot(v(i),h(i));
Frankly I think this or a variation of it is pretty much what you'd have
to do. A variation might include "arrayfun" or some such, but I don't
expect it to be any faster than what you already have.
ok, and "v", "x" and "i"?? If I want to change the values of v, I need
to change x and i... It's not nice :) Is this the correct way??

thank's

eric
--
http://ricoh51.free.fr/
Thomas Plehn
2008-03-31 19:04:45 UTC
Permalink
Post by ricoh51
Post by David Bateman
Post by ricoh51
Post by David Bateman
Post by ricoh51
I have a similar problem : what is wrong in my code? (I want to plot
Integral of (x+n)dx ( x from 0 to n)
function retval=foo(y)
retval=quad ( f, 0, y );
endfunction
n=3; <---- work
plot(n,foo(n),"ro");
n=[3:5:10]; <---- don't work
plot(n,foo(n));
The quad function requires a function that for a scalar value input,
returns a scalar output.. If n=[3:5:10], then your function handle
returns a vector, even for a scalar input value, and the quad function
won't be happy. This is stated clearly in the help text of the quad
function.
oh, ok, thank you...
What kind of mechanism can I use? (to plot my function)
Don't worry about plotting it yet, try and figure out what the semantics
are to calculate what you want to calculate. "n =[3:5:10]" is a two
element vector [3,8].
yes...
elliptic(x) = int_0^x {dt over sqrt{cos t - cos x}}
and plot elliptic(x) for x from 0.1 to 1.5 for example.
At this moment, I do that : (and it works fine, but is this a good way?)
function y = elliptic(x)
if isvector(x)
for i=1:length(x)
y(i) = elliptic(x(i));
endfor
endif
Post by ricoh51
endfunction;
#this allows you to later call elliptic() on vector arguments for plotting

x=0.1:0.1:1.5;
plot(x,elliptic(x));

#that's it, a bit more comfortable, but don't expect it to be faster.
Post by ricoh51
v=[0.1 : 0.1 : 1.5];
for x=1:15
h(x)=elliptic(v(x));
endfor;
i=[1:15];
plot(v(i),h(i));
thank's
eric
ricoh51
2008-03-31 19:41:06 UTC
Permalink
Post by Thomas Plehn
Post by ricoh51
function y = elliptic(x)
if isvector(x)
for i=1:length(x)
y(i) = elliptic(x(i));
endfor
endif
Post by ricoh51
endfunction;
#this allows you to later call elliptic() on vector arguments for plotting
x=0.1:0.1:1.5;
plot(x,elliptic(x));
#that's it, a bit more comfortable, but don't expect it to be faster.
Hi,
Thank's for the trick, but Octave returns many errors :
error: max_recursion_limit exceeded
error: evaluating assignment expression near line 4, column 14
error: evaluating for command near line 3, column 7
error: evaluating if command near line 2, column 5
error: called from `elliptic'

eric
--
http://ricoh51.free.fr/
David Bateman
2008-03-31 22:31:55 UTC
Permalink
Post by ricoh51
Post by Thomas Plehn
Post by ricoh51
function y = elliptic(x)
if isvector(x)
for i=1:length(x)
y(i) = elliptic(x(i));
endfor
endif
Post by ricoh51
endfunction;
#this allows you to later call elliptic() on vector arguments for plotting
x=0.1:0.1:1.5;
plot(x,elliptic(x));
#that's it, a bit more comfortable, but don't expect it to be faster.
Hi,
error: max_recursion_limit exceeded
error: evaluating assignment expression near line 4, column 14
error: evaluating for command near line 3, column 7
error: evaluating if command near line 2, column 5
error: called from `elliptic'
eric
It should be rare that you hit the maximum recursion limit. It means you
call a function that uses this flag (probably quad), 256 times.. You can
increase the limit if you like. For example

maximum_recursion_depth (1024)

with increase it to 1024. But be careful and you might just be hiding a
bug in your code.

D.
David Bateman
2008-03-31 22:32:07 UTC
Permalink
Post by ricoh51
Post by Thomas Plehn
Post by ricoh51
function y = elliptic(x)
if isvector(x)
for i=1:length(x)
y(i) = elliptic(x(i));
endfor
endif
Post by ricoh51
endfunction;
#this allows you to later call elliptic() on vector arguments for plotting
x=0.1:0.1:1.5;
plot(x,elliptic(x));
#that's it, a bit more comfortable, but don't expect it to be faster.
Hi,
error: max_recursion_limit exceeded
error: evaluating assignment expression near line 4, column 14
error: evaluating for command near line 3, column 7
error: evaluating if command near line 2, column 5
error: called from `elliptic'
eric
It should be rare that you hit the maximum recursion limit. It means you
call a function that uses this flag (probably quad), 256 times.. You can
increase the limit if you like. For example

maximum_recursion_depth (1024)

with increase it to 1024. But be careful and you might just be hiding a
bug in your code.

D.
ricoh51
2008-04-01 11:18:50 UTC
Permalink
Hi,
I was tired... ;-)
it works fine! thank you

eric

function y = elli(x)
f=@(t) 1./sqrt(cos(t)-cos(x));
y = quad(@(t) f(t), 0, x);
endfunction;

function y = elliptic(x)
if isvector(x)
for i=1:length(x)
y(i) = elli(x(i));
endfor
endif
endfunction;

x=0.1:0.1:1.5;
plot(x,elliptic(x));
--
http://ricoh51.free.fr/
Thomas Plehn
2008-04-01 08:47:43 UTC
Permalink
Post by ricoh51
Hi,
error: max_recursion_limit exceeded
error: evaluating assignment expression near line 4, column 14
error: evaluating for command near line 3, column 7
error: evaluating if command near line 2, column 5
error: called from `elliptic'
seems to be, that isvector does not show the same behaviour as in
matlab, seems to be that you have to use !isscalar instead, because a
scalar is treated as a vector, as a vetor is also treated as a Matrix.

I have also missed a return statement.

The attached code works for me.
Continue reading on narkive:
Loading...