I need help fixing my Bisection method code.

조회 수: 2 (최근 30일)
Jacob Savona
Jacob Savona 2015년 3월 9일
I'm trying to find the root of t , using the bisection method, where P=Ce^kt, with a percent error of .05%,or .0005. Here's the code:
function [t]=HW7_JAS_1(P,k,C)
P1=P;
k1=k;
C1=C;
t_lower=1;
t_upper=15;
t_error=.0005;
t_step=[1:t_error:t_upper];
P2=NaN(1,length(t_step));
for j=0:t_upper+1
for f=0:t_error:t_upper+1
P2(j+1)=C1*exp(f*k1);
end
end
while abs(1-(P2(t_upper)-P2(t_lower)))>t_error
t_mid=(t_lower+t_upper)/2;
if P2(t_mid)<P1 && P2(t_upper)>P1
t_upper=t_mid;
t_mid=((t_upper+t_lower)/2);
elseif P2(t_mid)>P1 && P2(t_lower)<P1
t_lower=t_mid;
t_mid=((t_upper+t_lower)/2);
else
t_upper=t_upper*2;
t_lower=t_lower-t_upper;
end
end
t=t_mid;
t
end
Sorry for how scrappy it is.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by