Cannot compute number of steps

17 views (last 30 days)
Philip Baczek
Philip Baczek on 26 Feb 2016
Edited: Philip Baczek on 27 Feb 2016
Hey,
Im trying to integrate using the trapezoid method, and works just fine when working with constant boundaries. However i'm having a hard time coming around this issue:
Error using : (line 38)
Cannot compute the number of steps from 0 to 5*sin((xx*pi)/10) by (5*sin((xx*pi)/10))/round(50*sin((xx*pi)/10)).
Error in Lab2 (line 24)
y=[0:hy:ymax(xx)] ;
n=100;
hx=10/n;
x=[0:hx:10];
ymax= @ (xx)5.*sin(pi.*xx./10);
syms xx
hy=ymax(xx)/round(ymax(xx)/hx);
y=[0:hy:ymax(xx)] ;
[xx,yy]=ndgrid(x,y);
mat = exp(-0.25.*((xx-8).^2 + (yy-0).^2)) .*cos((xx-8-yy)/6); %trapets
b{1}=x; b{2}=y;
T=trapets(b,mat,2);
F = @(x,y)exp(-0.25.*((x-8).^2 + (y-0).^2)).*cos((x-8-y)/6); %integral
Refvalue=integral2(F,0,10,0,ymax) ;
disp(T)
What to do?
  3 Comments
Philip Baczek
Philip Baczek on 26 Feb 2016
Its not a problem with the function. The function just uses the built in function trapz. My problem lies getting y=[0:yh:ymax(xx)] to work, which is the steplength
Philip Baczek
Philip Baczek on 26 Feb 2016
Edited: Walter Roberson on 26 Feb 2016
function T = trapets(x,mat,N)
mat = trapz(x{N},mat,N) ;
if N==1
T=mat;
return;
end
T = trapets(x,mat,N-1) ;
PS: sorry for the format, not quite sure how to paste code properly

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 26 Feb 2016
Look at your code,
ymax= @ (xx)5.*sin(pi.*xx./10);
Anonymous function. Okay so far.
syms xx
Symbolic declaration. Okay so far.
hy=ymax(xx)/round(ymax(xx)/hx);
Applying the anonymous function to the symbolic value -- dubious. Expecting to be able to round() the result of the symbolic calculation: very dubious. These are not impossible but are unlikely (there are some uses for passing a symbolic value to an anonymous function.)
y=[0:hy:ymax(xx)] ;
Clearly broken. hy is symbolic, ymax(xx) is symbolic, and you cannot use the colon operator with symbolic bounds.
Unfortunately there is a shortage of comments about the purpose of that section of code...
  1 Comment
Philip Baczek
Philip Baczek on 27 Feb 2016
Edited: Philip Baczek on 27 Feb 2016
The syms was just an act of desperation to try something different. It becomes obvious to me now that you mention that fact. The problem I cant come around is how to use a function as ymax(which depends on x) as an upper boundary to my trapezoid method since i in the previous section used similar code with constant boundary and it worked. Example. y=0:hy:10, x=0:hx:10
After further inspection i notice that my y variable just is a 0by1 matrix and contains no values. Where as x is 1x101(depending on n) My best guess being this is my main concern.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!