Getting an error "Error in MuPAD command: Index exceeds matrix dimensions."

Here is my function to do the false postioning method:
function [error, xr] = falsepositioning( xl,xu )
r=3;
syms h integer
f=(pi*(h^2))*(((3*r)-h)/3)
for iter=1:3
xr = xu-( f(xu)*(xl-xu) )/( f(xl)-f(xu) ); %compute xr
xrold=xr;
if f(xr)==0
return
elseif f(xr)*f(xl)<0 %root is left of xr
xu=xr;
else %root is right of xr
xl=xr;
end
error=((xr-xrold)/xr)*100;
end
Here is the file I am calling the function to
r=3;
xu=r;
xl=0;
falsepositioning( xl,xu );
I am trying to understand syms and how to use it correctly. It seems to be very easy and useful for many things but for some reason can't get it to work.

Answers (1)

f=(pi*(h^2))*(((3*r)-h)/3) creates f as an expression
xr = xu-( f(xu)*(xl-xu) )/( f(xl)-f(xu) ) attempts to use f as a function of one argument.
If you have a new enough version of MATLAB, then
syms h f(h)
f(h) = (pi*(h^2))*(((3*r)-h)/3);
However, this is just going to cause you problems about trying to mix symbols and numeric values. You should instead skip all reference to symbols and define
f = @(h) (pi*(h^2))*(((3*r)-h)/3);

2 Comments

It's giving me these errors now. "Error in falsepositioning (line 3) f=@(h) (pi*(h^2))*(((3*3)-h)/3);
Output argument "error" (and maybe others) not assigned during call to "C:\Users\Alex\Desktop\School\Spring 2016\CAE\falsepositioning.m>falsepositioning".
Error in CH5P17 (line 7) [error, xr] = falsepositioning( xl,xu )"
function [error, xr] = falsepositioning( xl,xu )
f=@(h) (pi*(h^2))*(((3*3)-h)/3);
iter=1;
for iter=1:3
xr = xu-( f(xu)*(xl-xu) )/( f(xl)-f(xu) ); %compute xr
xrold=xr;
if f(xr)==0
return
elseif f(xr)*f(xl)<0 %root is left of xr
xu=xr;
else %root is right of xr
xl=xr;
end
error=((xr-xrold)/xr)*100;
end
r=3;
xu=r;
xl=0;
[error, xr] = falsepositioning( xl,xu )
You have in your code
if f(xr)==0
return
You might never have assigned a value to error at that point.
In other words, the very first xr that is tried happens to be a zero of the function.

Sign in to comment.

Asked:

on 7 Feb 2016

Commented:

on 7 Feb 2016

Community Treasure Hunt

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

Start Hunting!