code on finding the roots using simple bisection method

조회 수: 2 (최근 30일)
B
B 2015년 5월 7일
댓글: Geoff Hayes 2015년 5월 7일
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에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by