# Paulche/num_analysis_labs

### Subversion checkout URL

You can clone with HTTPS or Subversion.

commit 8281bb3b273fe1aa74af1abd40867792cb8372a0 1 parent effad40
authored
 @@ -0,0 +1,3 @@ +# TODO + * [] check_convergency function isn't implemented +
72 lab5/action.m
 @@ -0,0 +1,72 @@ +function action() + num = 20; + [A b] = generate_equation(24,16); + start_x = ones(7,1) + + + % + % TODO: Find way to check convergency of matrix + % + % c = check_convergency(A) + + % Iterative method + [iter_solution iter_plot] = iter_method(A,b,start_x,num); + + iter_residual = b - A*iter_solution; + + iter_norm_residual = norm(iter_residual,1); + + + % Seidel method + [seidel_solution seidel_plot] = seidel_method(A,b,start_x,num); + + seidel_residual = b - A*seidel_solution; + + seidel_norm_residual = norm(seidel_residual,1); + + % Exact solution + exact = A \ b; + + iter_error = abs(iter_solution - exact); + + seidel_error = abs(seidel_solution - exact); + + % Relative error + dA = A; + + for i = 1:length(A) + dA(i,i) = A(i,i)*0.9; + end + + dA = norm(dA - A); + + dx = iter_error; + db = norm(b * 0.01); + + rel_error = cond(A)/(1 - cond(A)*dA*norm(A))*(dA/norm(A) + db/norm(b)); + + + % Output plots + subplot(2,1,1); + H = plot(0:num, iter_plot) + legend(H, arrayfun(@(x)(sprintf('x%d',x)),1:7,'UniformOutput',false)); + ylabel('Iterative method','fontsize',20,'fontweight','b'); + + subplot(2,1,2); + H = plot(0:num, seidel_plot) + legend(H, arrayfun(@(x)(sprintf('x%d',x)),1:7,'UniformOutput',false)); + ylabel('Seidel method','fontsize',20,'fontweight','b'); + + A, b, exact + + % Errors + iter_error, norm(iter_error,1), seidel_error, norm(seidel_error) + + % Output iter values + iter_solution, iter_residual, iter_norm_residual + + % Output seidel values + seidel_solution, seidel_residual, seidel_norm_residual + + rel_error +end
16 lab5/check_convergency.m
 @@ -0,0 +1,16 @@ +function b = check_convergency(A) + len = length(A); + + for i = 2:len + acc = sum(abs(A(1:i-1,i))) + + flag = acc > A(i,i) + + if flag + b = false + return + end + end + + b = true +end
56 lab5/generate_equation.m
 @@ -0,0 +1,56 @@ +function [A b] = generate_equation(group, num) + seed = group + num; + + rand('seed',seed); + + A = rand(7); + + b = 10 * rand(7,1); + + Au=triu(A); + + Al=tril(A); + + Ad=diag(diag(A)); + + su=sum(sum(abs(Au))); + sl=sum(sum(abs(Al))); + Su=sum(abs(Au)); + Sl=sum(abs(Al)); + + sigma=5; + + O=ones(7); + + Ou=triu(O); + + S6=diag(sigma*(Su./Sl)); + + At=sigma*((Al+Ou)./(Au'+Ou)); + + neo=rem(num,2); + + mode=rem(num,3); + + if (mode==0) + Al=(sigma*su/sl)*Al; + end + + if (mode==1) + Al=S6*Al; + end + + if (mode==2) + Al=At.*Al; + end + + A=Al+Ad+Au; + + ma=max(max(abs(A))); + + if (ma>1000) + A=0.08*A; + end + + A=0.5*A+2.75*diag(diag(A)); +end
17 lab5/iter.m
 @@ -0,0 +1,17 @@ +function r = iter(A,b,x) + % + % x = B*x + c + % + + len = length(A); + + B = zeros(len); + c = b ./ diag(A); + + for i = 1:len + B(i,:) = - A(i,:) / A(i,i); + B(i,i) = 0; + end + + r = B * x + c; +end
12 lab5/iter_method.m
 @@ -0,0 +1,12 @@ +function [solution serial] = iter_method(A,b,x,n) + r = x; + serial = []; + serial(1,:) = x; + + for i = 1:n + r = iter(A,b,r); + serial(i+1,:) = r; + end + + solution = r; +end
14 lab5/seidel_method.m
 @@ -0,0 +1,14 @@ +function [solution serial] = seidel_method(A,b,x,n) + serial = []; + len = length(x); + serial(1,:) = x; + + for iter = 1:n + for i = 1:len + 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); + end + serial(iter+1,:) = x; + end + + solution = x; +end
BIN  lab5/лр 5.doc
Binary file not shown