Getting an error "Error in MuPAD command: Index exceeds matrix dimensions."
Show older comments
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)
Walter Roberson
on 7 Feb 2016
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
Alexander Eckhoff
on 7 Feb 2016
Walter Roberson
on 7 Feb 2016
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.
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!