-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLE_Newtons_Method.m
196 lines (160 loc) · 5.32 KB
/
NLE_Newtons_Method.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
clear;clc
n=input('Enter the number of equations n=');
fprintf('\nEnter only the LHS of Equation F(x)=0 \n\n')
if n==2
f1= input('Enter Function f1(x1,x2)= ','s');
f2=input('Enter Function f2(x1,x2)= ','s');
%Coverting functions to symbolic form from string
syms x1 x2
f1=str2sym(f1);
f2=str2sym(f2);
fx=[f1;f2];
% Taking Partial Derivatives for Jacobian Matrix
f1x1=diff(f1,x1);
f1x2=diff(f1,x2);
f2x1=diff(f2,x1);
f2x2=diff(f2,x2);
J=[f1x1 f1x2;f2x1 f2x2];
%Asking the user to input initial approximation
i=input('Enter initial approximation of x1=');
j=input('Enter initial approximation of x2=');
%Initial appproximation
x=[i;j];
tol=input('Input tolerence=');
iter=0;%Initialization
iterlimit=100;
while iter<=iterlimit
iter=iter+1;
%Calculating the values of functions
fxd=double(subs(fx(:,:),[x1 x2 ],x'));
%Calculating the the values of Jacobi's matrix
Jd=double(subs(J(:,:),[x1 x2],x'));
if cond(Jd)==inf
fprintf('\n\nSolution can not be computed since Jacobian Matrix is singular \n')
return
end
xnew=x-Jd\fxd;
Error(iter,:)=max(abs(xnew-x));
t(iter,:)=xnew';
if max(abs(xnew-x))<tol
break;
end
x=xnew;
end
elseif n==3
f1= input('Enter Function f1(x1,x2,x3)= ','s');
f2=input('Enter Function f2(x1,x2,x3)= ','s');
f3=input('Enter Function f3(x1,x2,x3)= ','s');
%Coverting functions to symbolic form from string
syms x1 x2 x3
f1=str2sym(f1);
f2=str2sym(f2);
f3=str2sym(f3);
fx=[f1;f2;f3];
% Taking Partial Derivatives for Jacobian Matrix
f1x1=diff(f1,x1);
f1x2=diff(f1,x2);
f1x3=diff(f1,x3);
f2x1=diff(f2,x1);
f2x2=diff(f2,x2);
f2x3=diff(f2,x3);
f3x1=diff(f3,x1);
f3x2=diff(f3,x2);
f3x3=diff(f3,x3);
J=[f1x1 f1x2 f1x3;f2x1 f2x2 f2x3;f3x1 f3x2 f3x3];
%Asking the user to input initial approximation
i=input('Enter initial approximation of x1=');
j=input('Enter initial approximation of x2=');
k=input('Enter initial approximation of x3=');
%Initial appproximation
x=[i;j;k];
tol=input('Input tolerence=');
iter=0;%Initialization
iterlimit=100;
while iter<=iterlimit
iter=iter+1;
%Calculating the values of functions
fxd=double(subs(fx(:,:),[x1 x2 x3],x'));
%Calculating the the values of Jacobi's matrix
Jd=double(subs(J(:,:),[x1 x2 x3],x'));
xnew=x-Jd\fxd;
Error(iter,:)=max(abs(xnew-x));
t(iter,:)=xnew';
if max(abs(xnew-x))<tol
break;
end
x=xnew;
end
elseif n==4
f1= input('Enter Function f1(x1,x2,x3,x4)= ','s');
f2=input('Enter Function f2(x1,x2,x3,x4)= ','s');
f3=input('Enter Function f3(x1,x2,x3,x4)= ','s');
f4=input('Enter Function f4(x1,x2,x3,x4)= ','s');
%Coverting functions to symbolic form from string
syms x1 x2 x3 x4
f1=str2sym(f1);
f2=str2sym(f2);
f3=str2sym(f3);
f4=str2sym(f4);
fx=[f1;f2;f3;f4];
% Taking Partial Derivatives for Jacobian Matrix
f1x1=diff(f1,x1);
f1x2=diff(f1,x2);
f1x3=diff(f1,x3);
f1x4=diff(f1,x4);
f2x1=diff(f2,x1);
f2x2=diff(f2,x2);
f2x3=diff(f2,x3);
f2x4=diff(f2,x4);
f3x1=diff(f3,x1);
f3x2=diff(f3,x2);
f3x3=diff(f3,x3);
f3x4=diff(f3,x4);
f4x1=diff(f4,x1);
f4x2=diff(f4,x2);
f4x3=diff(f4,x3);
f4x4=diff(f4,x4);
J=[f1x1 f1x2 f1x3 f1x4;f2x1 f2x2 f2x3 f2x4;f3x1 f3x2 f3x3 f3x4;f4x1 f4x2 f4x3 f4x4];
%Asking the user to input initial approximation
i=input('Enter initial approximation of x1=');
j=input('Enter initial approximation of x2=');
k=input('Enter initial approximation of x3=');
l=input('Enter initial approximation of x4=');
%Initial appproximation
x=[i;j;k;l];
tol=input('Input tolerence=');
iter=0;%Initialization
iterlimit=100;
while iter<=iterlimit
iter=iter+1;
%Calculating the values of functions
fxd=double(subs(fx(:,:),[x1 x2 x3 x4],x'));
%Calculating the the values of Jacobi's matrix
Jd=double(subs(J(:,:),[x1 x2 x3 x4],x'));
xnew=x-Jd\fxd;
Error(iter,:)=max(abs(xnew-x));
t(iter,:)=xnew';
if max(abs(xnew-x))<tol
break;
end
x=xnew;
end
end
Jacobian_Matrix=J
fprintf('\nNumber_of_iterations=%3.0f \n\n',iter)
if n==2
fprintf('Table of Iterations \n')
fprintf(' No. of x1 x2 \niterations \n')
fprintf('\t%2.0f \t\t %2.8f \t %2.8f\n',[1:iter;(t(:,1))';(t(:,2))'])
elseif n==3
fprintf('Table of Iterations \n')
fprintf(' No. of x1 x2 x3 \niterations \n')
fprintf('\t%2.0f \t\t %2.8f \t %2.8f \t %2.8f\n',[1:iter;(t(:,1))';(t(:,2))';(t(:,3))'])
elseif n==4
fprintf('Table of Iterations \n')
fprintf(' No. of x1 x2 x3 x4 \niterations \n')
fprintf('\t%2.0f \t\t %2.8f \t %2.8f \t %2.8f \t %2.8f\n',[1:iter;(t(:,1))';(t(:,2))';(t(:,3))';(t(:,4))'])
end
fprintf('\nTable of Maximum Errors \n')
fprintf(' No. of Max_Error \niterations \n')
fprintf('\t%2.0f \t\t %2.8E \n',[1:iter;Error'])