I need help fixing my Bisection method code.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!