# publicPaulche/num_analysis_labs

### Subversion checkout URL

You can clone with HTTPS or Subversion.

commit 3982baa131f2cbda1168ba5755e38d765da8d7ca 1 parent 590a75e
authored
23 lab5/action.m
 ... ... @@ -1,16 +1,10 @@ 1 1 function action() 2 2 num = 20; 3 - [A b] = generate_equation(24,16); 4 - start_x = ones(7,1) 5 - 6 - 7 - % 8 - % TODO: Find way to check convergency of matrix 9 - % 10 - % c = check_convergency(A) 3 + [A b] = generate_equation(26,15); 4 + start_x = ones(7,1); 11 5 12 6 % Iterative method 13 - [iter_solution iter_plot] = iter_method(A,b,start_x,num); 7 + [iter_solution iter_conv iter_plot] = iter_method(A,b,start_x,num); 14 8 15 9 iter_residual = b - A*iter_solution; 16 10 @@ -18,7 +12,7 @@ function action() 18 12 19 13 20 14 % Seidel method 21 - [seidel_solution seidel_plot] = seidel_method(A,b,start_x,num); 15 + [seidel_solution seidel_conv seidel_plot] = seidel_method(A,b,start_x,num); 22 16 23 17 seidel_residual = b - A*seidel_solution; 24 18 @@ -48,25 +42,26 @@ function action() 48 42 49 43 % Output plots 50 44 subplot(2,1,1); 51 - H = plot(0:num, iter_plot) 45 + H = plot(0:num, iter_plot); 52 46 legend(H, arrayfun(@(x)(sprintf('x%d',x)),1:7,'UniformOutput',false)); 53 47 ylabel('Iterative method','fontsize',20,'fontweight','b'); 54 48 55 49 subplot(2,1,2); 56 - H = plot(0:num, seidel_plot) 50 + H = plot(0:num, seidel_plot); 57 51 legend(H, arrayfun(@(x)(sprintf('x%d',x)),1:7,'UniformOutput',false)); 58 52 ylabel('Seidel method','fontsize',20,'fontweight','b'); 59 53 54 + % Input data 60 55 A, b 61 56 62 57 % Errors 63 58 iter_error, norm(iter_error), seidel_error, norm(seidel_error) 64 59 65 60 % Output iter values 66 - iter_residual, iter_norm_residual 61 + iter_residual, iter_conv, iter_norm_residual 67 62 68 63 % Output seidel values 69 - seidel_residual, seidel_norm_residual 64 + seidel_residual, seidel_conv, seidel_norm_residual 70 65 71 66 rel_error 72 67
2  lab5/iter.m → lab5/compute_bc.m
 ... ... @@ -1,4 +1,4 @@ 1 -function [B c] = iter(A,b) 1 +function [B c] = compute_bc(A,b) 2 2 % 3 3 % x = B*x + c 4 4 %
2  lab5/generate_equation.m
 @@ -52,5 +52,5 @@ 52 52 A=0.08*A; 53 53 end 54 54 55 - A=0.5*A+2.75*diag(diag(A)); 55 + A=0.5*A+5*diag(diag(A)); 56 56 end
16 lab5/iter_method.m
 ... ... @@ -1,13 +1,19 @@ 1 -function [solution serial] = iter_method(A,b,x,n) 1 +function [solution conv serial] = iter_method(A,b,x,n) 2 2 r = x; 3 - serial = []; 4 - serial(1,:) = x; 3 + serial = transp(x); 5 4 6 - [B c] = iter(A,b); 5 + [B c] = compute_bc(A,b); 6 + 7 + % Check convergency 8 + if norm(B) < 1 9 + conv = true; 10 + else 11 + conv = false; 12 + end 7 13 8 14 for i = 1:n 9 15 x = B * x + c; 10 - serial(i+1,:) = x; 16 + serial = [serial; transp(x)]; 11 17 end 12 18 13 19 solution = x;
20 lab5/seidel_method.m
 ... ... @@ -1,13 +1,25 @@ 1 -function [solution serial] = seidel_method(A,b,x,n) 2 - serial = []; 1 +function [solution conv serial] = seidel_method(A,b,x,n) 2 + serial = transp(x); 3 3 len = length(x); 4 - serial(1,:) = x; 4 + 5 + [B c] = compute_bc(A, b); 6 + 7 + Bl = tril(B); 8 + Bu = triu(B); 9 + 10 + norm_B = norm(Bl) + norm(Bu); 11 + 12 + if norm_B < 1 13 + conv = true; 14 + else 15 + conv = false; 16 + end 5 17 6 18 for iter = 1:n 7 19 for i = 1:len 8 20 x(i) = (b(i) - sum(A(i,1:i-1)*x(1:i-1)) - sum(A(i,i+1:end)*x(i+1:end))) / A(i,i); 9 21 end 10 - serial(iter+1,:) = x; 22 + serial = [ serial; transp(x) ]; 11 23 end 12 24 13 25 solution = x;