I need help fixing my Bisection method code.

1 次查看(过去 30 天)
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