blob: 6703a8b0947b67f445a26554305cae333a2f8989 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
//Eg-4.5
//pg-149
clear
clc
//False Position Method
clear ;
close ;
clc ;
//Coefficients of polynomial in increasing order of power of x
A = [-3.1622777 -1.2649111 -0.1264911 0 0 100];
x1 = 0 ;
x2 = 2 ;
fx = poly(A,'x','c');
printf('\n\nThe given equation can be modified and written in the following form after substituting the values of given constants\n')
disp(fx)
i = 0;
eps = 1;
while(eps > 10^(-6))
i = i+1;
//printf('\n\nIteration No. %i \n',i);
fx1 = horner(fx,x1);
fx2 = horner(fx,x2);
xnew = (x1*fx2 - x2*fx1)/(fx2-fx1);
fxnew = horner(fx,xnew);
//printf('xnew = %f \nfxnew = %f',xnew,fxnew);
if fx1*fxnew < 0 then
x2 = xnew ;
else
x1 = xnew ;
end
eps = abs(fxnew);
end
printf('\n\nThe result obtained after %d iterations is x = %f\n',i,xnew)
|