code on finding the roots using simple bisection method

2 次查看(过去 30 天)
what's wrong in the following code:
function s=bisectionpart(s);
xl=5;
xu=100;
xr=xl;
while(1)
xrold=xr;
xr=(xl+xu)/2;
error=abs((xr-xrold)/xr)*100;
if error<0.1
break,
end;
test=fs(xl)*fs(xr);
if test<0;
xu=xr;
else,
xl=xr;
end
end
s=xr;
plot(t,s);
end
function f=fs(s);
s0=10;
vm=0.5;
ks=2;
t=0:50;
f=s0-(vm*t)+ks*log(s0/s)-s;
end
  1 个评论
Geoff Hayes
Geoff Hayes 2015-5-7
B - try formatting your above code that it is readable by others, or attach the code to your question using the paperclip button.
A couple of comments: why is s an input parameter to your bisectionpart function. How is it used, and what would it be when you call this function? Also, don't use error as the name of a variable as this is also the name of a built-in MATLAB function.
When I try to run the bisectionpart function, I get an error at line 17
Error using *
Inner matrix dimensions must agree.
Error in bisectionpart (line 17)
test=fs(xl)*fs(xr);
This is because the result returned from fs is an array of 51 elements. Is this expected? What is the purpose of fs i.e. what does it represent or model?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by