% ===================================================================== % ****** To find root of given equation by false position method ****** % ************************ By Mahesha MG ****************************** % Date: 28/11/2012 % ===================================================================== display('Equation is x^2+x-2 = 0') i=1; while(i) xl=input('Enter lower value:'); xu=input('Enter upper value: '); e=input('Enter accuracy: '); if equan(xl)*equan(xu)<0 i=0; else warning('Enter proper range'); end end if equan(xl)<0 xn=xl; xp=xu; else xn=xu; xp=xl; end xm=xl; while (abs(equan(xm))>e) xm=(xn*equan(xp)-xp*equan(xn))/(equan(xp)-equan(xn)); if equan(xm)<0 xn=xm; else xp=xm; end end Root=xm SAMPLE OUTPUT: >> false_position Equation is x^2+x-2 = 0 Enter lower value:-2 Enter upper value: 2 Enter accuracy: 1e-4 Warning: Enter proper range > In false_position at 15 Enter lower value:-1 Enter upper value: 2 Enter accuracy: 1e-4 Root = 1.0000
Tuesday, November 27, 2012
MATLAB - False Position Method
Subscribe to:
Post Comments (Atom)
if equan(xl)*equan(xu)<0
ReplyDeleteDoesn't that come up with an error?
if equan(xl)*equan(xu)<0
Deletethis statement makes sure that at lower value and upper value of x, function will have opposite signs. this is the 'must' condition for false position method. Once this condition is satisfied, 0 is assigned to i. So the while(i) loop ends.
Dear Mr. Mahesha MG ,,,,
DeleteI am not getting output after this programm it showing error as below.
Error using ==> le
Not enough input arguments.
Error in ==> Untitled2 at 12
e=input('Enter accuracy: ');
Hi
Deletecreate a file equan.m and copy the following lines to it & save.
% Equation to be solved
function[eqn]=equan(x);
eqn=x^2+x-2;
Now run false position. It should work. The function is defined in equan.m file and it is called in the present code. Since it is already mentioned in bisection code (see code page), I didn't mention that in this page.
Thanks for intimating the problem.
hi is there any truncation of iteration criteria?
ReplyDeleteEquation is x^2+x-2 = 0
ReplyDeleteEnter lower value:0
Enter upper value: 9
Enter accuracy: 1e-4
??? Undefined function or method 'equan' for input arguments of type 'double'.
im entering these values and getting this error....
Hi
Deletecreate a file equan.m and copy the following lines to it & save.
% Equation to be solved
function[eqn]=equan(x);
eqn=x^2+x-2;
Now run false position. It should work. The function is defined in equan.m file and it is called in the present code. Since it is already mentioned in bisection code (see code page), I didn't mention that in this page.
Thanks for intimating the problem.
Can this method be used for another function>
ReplyDeleteCould this method be used for any other function?
ReplyDeleteCan this method be used for another function>
ReplyDeleteYes. It can be...
Deleteclc
ReplyDeleteclear all
syms x
f=inline(input('enter the equation:','s'))
i=1;
while(i)
xl=input('Enter lower value:');
xu=input('Enter upper value: ');
e=input('Enter accuracy: ');
if f(xl)*f(xu)<0
i=0;
else
display('root not located between the entered values');
end
end
if f(xl)<0
xn=xl;
xp=xu;
else
xn=xu;
xp=xl;
end
xm=xl;
t=1;
while (abs(f(xm))>e)
xm=(xn*f(xp)-xp*f(xn))/(f(xp)-f(xn));
fprintf('%d)\tx%d=%f\n',t,t,xm)
t=t+1;
if f(xm)<0
xn=xm;
else
xp=xm;
end
end
Root=xm
please use this code instead to find out roots for any expression.. just enter an equation with proper syntax in the command window. e.g enter the expression like this when asked x^2+x-2 . the results will be shown in table form
Is it not the formula xn-((equan(xn)-(xp-xn)/(equan(xp)-equan(xn)) the formula in getting xm or is your formula derived from that formula? Or maybe an alternative formula. I'm confused to what formula am i going to use when i only know the formula given to us by our school proffesors. Thats all ty.
ReplyDeletexn-((equan(xn)*(xp-xn)/(equan(xp)-equan(xn))is right. What I used is derived from this itself. Take common denominator and simplify. You will get the formula that I used.
DeleteFor getting xm, can we use the formula xn-(equan(xn)-(xp-xn)/(equan(xp)-equan(xn)))?
ReplyDelete