False Position Meth
False Position Meth
0002 mprintf("approximate the real root of the polynomial equation using the FALSE POSITION METHOD \n
\n")
0003
0004 //define the function
0001 function y=f(x)
0002 y=a*x.^n+b*x.^(n-1)+c*x.^(n-2)+d;
0003 endfunction
0008
0009 //define the inputs
0010 a=1;
0011 b=5;
0012 c=2;
0013 d=-8;
0014
0015 //define initial values
0016 n=input("Input degree of the function: ")
0017 xa=input("Input lower limit: ");
0018 xb=input("Input upper limit: ");
0019 while(f(xa)*f(xb)>0)
0020 disp("Limits incorrect. Please input another.");
0021 xa=input("Input lower limit: ");
0022 xb=input("Input upper limit: ");
0023 end
0024 xa1=[xa:.1:xb]
0025 xm=(xa*f(xb)-xb*f(xa))/(f(xb)-f(xa));
0026 i=0;
0027 z=0;
0028 mprintf ("i\t x\t\t f(xm)\n")
0029 while (abs(f(xm))>0.0001)
0030 xm=(xa*f(xb)-xb*f(xa))/(f(xb)-f(xa));
0031 if(f(xm)*f(xb)<0)then
0032 xa=xm;
0033 else
0034 xb=xm;
0035 end
0036
0037 i=i+1;
0038 mprintf('%d:\t%f\t%f\n',i,xm,f(xm))
0039 end
0040 mprintf("\nSolution of equation is %f ",xm)