-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumericMethods.m
256 lines (237 loc) · 5.64 KB
/
NumericMethods.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
%% Definiton
% This code contains algorithms for numerical methods. It is written as
% part of Numerical Methods course by following the book of
% Chapra,S.C.,and Canale,R.P., Numerical Methods for Engineers, 7th ed.,...
% McGraw-Hill Education, 2015.
% f = function
% Step = structure for solution. It contains percentage error.
% err_limit = desired error% level
%% CLOSED METHODS
%% 1.GRAPHICAL APPROACH
clear
clc
syms x
%f=5*x^3-5*x^2+6*x-2;
f=log(x^2)-0.7;
Desired=0;
g(x)=f-Desired;
fplot(g(x))
grid on
eq=g(x)==0;
[sol]=vpasolve(eq,x);
%% 2.BISECTION METHOD
clear
clc
syms x
f=x^3-6*x^2+11*x-6.1;
Desired=0;
err_limit=1;
g(x)=f-Desired;
eq=g(x)==0;
[sol]=vpasolve(eq,x);
x_r_true=(double([sol]));
Step(1).x_r_t=x_r_true;
x_upper=3.5;
x_lower=2.5;
Step(2).x_r_t=x_r_true;
xr=(x_upper+x_lower)/2;
for i=1:20
Step(i+1).xup=x_upper;
Step(i+1).xlow=x_lower;
a=double(g(x_upper));
Step(i+1).g_up=a;
b=double(g(x_lower));
Step(i+1).g_lw=b;
xr=(x_upper+x_lower)/2;
Step(i+1).x_r=xr;
c=double(g(xr));
Step(i+1).g_x_r=c;
if g(xr)*g(x_lower)<0
x_upper=xr;
else
if g(xr)*g(x_lower)>0
x_lower=xr;
else
if g(xr)*g(x_lower)==0
xr=xr;
end
end
end
error=(Step(i+1).x_r-Step(i).x_r)/Step(i+1).x_r*100;
Step(i+1).err=abs(error);
error_t=(x_r_true-Step(i+1).x_r)/x_r_true*100;
Step(i+1).trerror=abs(error_t);
if Step(i+1).err<=err_limit
break
end
end
%% FALSE POSITION METHOD
clear
clc
syms x
%f=(667.38/x)*(1-exp(-0.146843*x));
%f=x^10-1;
%f=log(x^2)-0.7;
f=x*exp((-2*x))+x^2+2;
Desired=0;
err_limit=0.1;
g(x)=f-Desired;
eq=g(x)==0;
[sol]=vpasolve(eq,x);
x_r_true=(double([sol]));
Step(1).x_r_t=x_r_true;
x_upper=0;
x_lower=-1;
a=double(g(x_upper));
b=double(g(x_lower));
x_r_true=x_r_true(x_lower<real(x_r_true)&real(x_r_true)<x_upper&imag(x_r_true)==0);
Step(2).x_r_t=x_r_true;
for i=1:20
Step(i+1).xup=x_upper;
Step(i+1).xlow=x_lower;
a=double(g(x_upper));
Step(i+1).g_up=a;
b=double(g(x_lower));
Step(i+1).g_lw=b;
xr=x_upper-(a*(x_lower-x_upper))/(b-a);
Step(i+1).x_r=xr;
c=double(g(xr));
Step(i+1).g_x_r=c;
%if c==0
% break;
%end
if g(xr)*g(x_lower)<0
x_upper=xr;
else
if g(xr)*g(x_lower)>0
x_lower=xr;
else
if g(xr)*g(x_lower)==0
xr=xr;
end
end
end
error=(Step(i+1).x_r-Step(i).x_r)/Step(i+1).x_r;
Step(i+1).err=abs(error);
error_t=(x_r_true-Step(i+1).x_r)/x_r_true*100;
Step(i+1).trerror=abs(error_t);
if Step(i+1).err<err_limit
break
end
end
%% OPEN METHODS
%% 1.SIMPLE FIXED ITERATION
clear
clc
syms x
f(x)=x-4*cos(1/sqrt(x));
g(x)=4*cos(1/sqrt(x));
dif=diff(g(x));
x_o=2.5;
err_limit=0.1;
eq=f(x)==0;
[sol]=vpasolve(eq,x);
x_r_true=(double([sol]));
Step(1).x_r=x_o;
dif(x)=diff(g(x));
i=1;
while 1
if abs(double(dif(Step(i).x_r)))<1 %converge
a=double(g(Step(i).x_r));
Step(i+1).x_r=a;
error=(Step(i+1).x_r-Step(i).x_r)/Step(i+1).x_r*100;
Step(i+1).err=abs(error);
error_t=(x_r_true-Step(i+1).x_r)/x_r_true*100;
if Step(i+1).err<err_limit
break
end
else %diverge
break
end
i=i+1;
end
%% 2.NEWTON-RAPSHSON METHOD
clear
clc
syms x
err_limit=1e-16;
f(x)=x^2-5;
x_o=2;
eq=f(x)==0;
[sol]=vpasolve(eq,x);
x_r_true=(double([sol]))
Step(1).x_r_t=x_r_true; %True root of function
dif(x)=diff(f(x))
Step(1).x_r=x_o;
for i=1:100
a=double(f(Step(i).x_r));
Step(i).f=a;
b=double(dif(Step(i).x_r));
Step(i).df=b;
Step(i+1).x_r=Step(i).x_r-(a/b);
error=(Step(i+1).x_r-Step(i).x_r)/Step(i+1).x_r*100;
Step(i+1).err=abs(error);
error_t=(x_r_true-Step(i+1).x_r)/x_r_true*100;
Step(i+1).trerror=abs(error_t);
if Step(i+1).err<err_limit
break
end
end
%% 3.SECANT METHOD
clear
clc
syms x
err_limit=10^-6;
f(x)=exp(-x)-x;
x_o1=0; %xi-1
x_o=1; %xi
eq=f(x)==0;
[sol]=vpasolve(eq,x);
x_r_true=(double([sol]));
Step(1).x_r_t=x_r_true;
Step(2).x_r_t=x_r_true;
Step(1).x_r=x_o1; %xi-1
Step(2).x_r=x_o; %xi
for i=1:20
a=double(f(Step(i).x_r));
Step(i).f=a; %xi-1
b=double(f(Step(i+1).x_r));
%Step(i+1).fo=b; %xi
Step(i+2).x_r=Step(i+1).x_r-((b*(Step(i).x_r-Step(i+1).x_r)/(a-b)));
error=(Step(i+2).x_r-Step(i+1).x_r)/Step(i+2).x_r;
Step(i+2).err=abs(error);
error_t=(x_r_true-Step(i+2).x_r)/x_r_true*100;
Step(i+2).trerror=abs(error_t);
if Step(i+2).err<err_limit
break
end
end
%% 4.MODIFIDED SECANT METHOD
clear
clc
syms x
err_limit=10^-6;
%f(x)=x*exp(-2*x)+x^2+2;
f(x)=exp(-x)-x;
x_o=1; %xi-1
delx=0.01;
eq=f(x)==0;
[sol]=vpasolve(eq,x);
x_r_true=(double([sol]));
Step(1).x_r_t=x_r_true;
Step(2).x_r_t=x_r_true;
Step(1).x_r=x_o; %xi-1
for i=1:20
a=double(f(Step(i).x_r));
Step(i).f=a; %xi-1
b=double(f(Step(i).x_r+delx));
Step(i).fo=b; %xi
Step(i+1).x_r=Step(i).x_r-((a*(Step(i).x_r*delx)/(b-a)));
error=(Step(i+1).x_r-Step(i).x_r)/Step(i+1).x_r*100;
Step(i+1).err=abs(error);
error_t=(x_r_true-Step(i+1).x_r)/x_r_true*100;
Step(i+1).trerror=abs(error_t);
if Step(i+1).err<err_limit
break
end
end