-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl5.tar
290 lines (221 loc) · 20 KB
/
l5.tar
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
disco.m 0000664 0001750 0001750 00000000473 13373642005 011624 0 ustar cccsar cccsar #Dibujo de disco de Gershgorin
function [] =disco(A)
circulo=0:0.01:2*pi;
n=length(A);
x=zeros(length(circulo),n);
r=sum(abs(A),2)-abs(diag(A));
for i=1:n
x(:,i)=r(i)*cos(circulo)+real(A(i,i));
y(:,i)=r(i)*sin(circulo)+imag(A(i,i));
end
e=eig(A);
plot(x,y,real(e),imag(e),'x');
end
gauss_seidel.m 0000664 0001750 0001750 00000000733 13374045644 013200 0 ustar cccsar cccsar
function [xn,k]=gauss_seidel(A,b,xv,maxit,tol)
n=length(A);
xn=NaN*ones(n,1);
for k=0:1:maxit
for i=1:1:n
suma1=0;
for j=1:1:i-1
suma1+=A(i,j)*xn(j); # Unica diferencia
endfor
suma2=0;
for j=i+1:1:n
suma2+=A(i,j)*xv(j);
endfor
xn(i)=(b(i)-suma1-suma2)/A(i,i);
endfor
if (norm(xv-xn,inf))<tol
return
endif
xv=xn;
endfor
endfunction
jacobi.m 0000664 0001750 0001750 00000000700 13374045645 011753 0 ustar cccsar cccsar
function [xn,k]=jacobi(A,b,xv,maxit,tol)
n=length(A);
xn=NaN*ones(n,1);
for k=0:1:maxit
for i=1:1:n
suma1=0;
for j=1:1:i-1
suma1+=A(i,j)*xv(j);
endfor
suma2=0;
for j=i+1:1:n
suma2+=A(i,j)*xv(j);
endfor
xn(i)=(b(i)-suma1-suma2)/A(i,i);
endfor
if (norm(xv-xn,inf))<tol
return
endif
xv=xn;
endfor
endfunction
lab5.m 0000664 0001750 0001750 00000002401 13374053605 011342 0 ustar cccsar cccsar #Ejercicio1
%{
#Calculos A1:
X=-1+(2)*rand(10,10);
A1=X+10*eye(10,10);
b1=A1*ones(10,1);
x0=0*ones(10,1);
maxit=1000;
tol=10^-13;
#Calculos A2:
Y=-1+(2)*rand(20,20);
A2=Y.'*Y+(10^-3)*eye(20,20);
b2=A2*ones(20,1)
x1=0*ones(20,1);
#Soluciones:
[xn1 k1]=jacobi(A1,b1,x0,maxit,tol)
Mi1=matriziteracion(A1,0);
re1=max(abs(eig(Mi1)))
[xv1 k11]=gauss_seidel(A1,b1,x0,maxit,tol)
Mi2=matriziteracion(A1,1);
re2=max(abs(eig(Mi2)))
[xn2 k2]=jacobi(A2,b2,x1,maxit,tol)
Mi3=matriziteracion(A2,0);
re3=max(abs(eig(Mi3)))
[xv2 k22]=gauss_seidel(A2,b2,x1,maxit,tol)
Mi4=matriziteracion(A2,1);
re4=max(abs(eig(Mi4)))
#Ejercicio2
%}
#Matrices dadas:
M1=[7 3 -1 2 ; 3 8 1 -4 ; -1 1 4 -1 ; 2 -4 -1 6];
j=[-1;0;-3;1];
M2=[4 3 0;3 4 -1; 0 -1 4];
k=ones(3,1);
tol=10^-13;
xaux=0*ones(4,1);
xaux1=0*ones(3,1);
#Graficas:
%{
Se utiliza el siguiente for para crear dos lista de los resultados
de la funcion SOR sobre cada valor dado de W. Cada lista corresponde
a una de las matrices dadas
%}
imag=[]
for w=100:1:19900
imag(w)=sor(M1,j,xaux,w/(10^4),tol);
imag2(w)=sor(M2,k,xaux1,w/(10^4),tol);
#plot(w,sor(M2,k,xaux1,w,tol))
endfor
w=100:1:19900
#Grafica 1:
tic();
plot(w,imag(w))
tiempog1=toc()
hold on
#Grafica 2:
tic();
plot(w,imag2(w))
tiempog2=toc()