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.
답변 (1개)
Walter Roberson
2016년 2월 7일
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
Alexander Eckhoff
2016년 2월 7일
Walter Roberson
2016년 2월 7일
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.
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!