-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_regula_falsi.m
executable file
·86 lines (78 loc) · 2.68 KB
/
mod_regula_falsi.m
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function a = modified_regula_falsi( f )
%for modified Regula Falsi mathod method
% asking for the range
R= input ( 'You are looking for the roots in [ x_min , x_max] :\n');
% check for range
[ nr , mr ] = size( R);
if nr ~= 1 || mr~= 2
disp( 'Input error ..The matrix should be 1x2 matrix')
return
end
% if roots lies on the boundary
if feval( f , R( 1,1) )* feval( f , R(1,2)) == 0
if feval( f , R( 1,1) ) == 0
R(1,1)
return
else
feval( f , R(1,2)) == 0
R(1,2)
return
end
end
% condition for convergence of method
if feval( f , R( 1,1) )* feval( f , R(1,2)) > 0
disp ( 'Either NO root lies in the in the given range or EVEN no of roots')
disp( 'lies in the given range and hence this method can not be applied.');
return
end
%error allowed in final answer
tol = abs(input(' Pls enter the error allowed in the final answer :'));
% no of itarations to be performed
n = input('Pls inter the no of itaration to be performed :');
%initialising the value of k and matrix X
k=1;
X= zeros(n+1,3);
%initial disply of tabel & initialising values for look
disp(sprintf('\t iterate \t value of x \t error'));
x0= R(1,1); x1= R(1,2); err = x1-x0;
disp(sprintf ('\t %3d \t %11.5f \t %11.5f ', 0, x1,err));
% itaration loop starts
while k <=n && abs(err) > tol
x = x1 - (x1-x0)/( feval(f,x1)-feval(f,x0) ) *feval(f,x1);%MODIFIED REGULA FALSI formula
if feval(f , x0) * feval(f , x) == 0
x
return
else
err = x - x1;
x0 = x1;
x1 = x;
end
% storing values in the form of matrix
X(k,1) = k;
X(k,2) = x1;
X(k,3) = abs(err);
disp(sprintf ('\t %3d \t %11.5f \t %11.5f ', k, x1,err));
k = k + 1;
end
% for the display of final result
if abs(err) > tol
disp(sprintf ('The final answer abtained after %3d itarations is %10.10f with an error %10.10f \n' , n , X(n,2),X(n,3)))
disp('Process is not convergent.Try other process')
return
else
disp(sprintf ('The final answer abtained after %3d itarations is %10.10f with an error %10.10f \n' , (k-1) , X((k-1),2),X((k-1),3)))
end
% for graph
m = menu('would you like to see how process converges with itaration?',...
'show in graphical form','No thanks..Get me out of this');
switch m
case 1
x=X(1:(k-1),1);
y=X(1:(k-1),3);
plot(x,y)
xlabel (' No of itarations ')
ylabel (' Error ')
title ('Convergence of Modified Regula-Falsi methode')
otherwise
return
end