simple False Position code
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm trying to do a simple False Position code following my teachers model but can't get it to actually work. Not sure if my if, else is wrong or what as it only says Method failed but I know there should be and answer of P=3.0571 and i=16. Totally new to programming and matlab so any help would be greatly valued. Thanks
%False Position %(x^2-4x+4-lnx=0 for 2<=x<=4)
p0=2; p1=4; TOL=10^-6; N=100;
q0=p0^2-4*p0+4-log(p0); q1=p1^2-4*p1+4-log(p1);
i=2; while i<=N p=p1-q1*(p1-p0)/(q1-q0);
if abs(p-p1)<TOL
disp('False Position')
p
i
break
end
i=i+1;
q=p^2-4*p+4-log(p);
if q*q1<0
p0=p1;q0=q1;
else
p1=p;q1=q;
end
end
if i>=N disp('Method Failed')
end
0 commentaires
Réponses (1)
Roger Stafford
le 16 Sep 2013
I believe your error lies in the lines
if q*q1<0
p0=p1;
q0=q1;
They ought to read
if q*q1<0
p0=p;
q0=q;
The idea is to keep the signs of q0 and q1 opposite, so you set either (p1,q1) or (p0,q0) to (p,q) whichever will retain that as true while continually shrinking the interval length. You were setting (p0,q0) to (p1,q1) which would suddenly shrink the interval length to zero with the true solution off to one side.
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!