Advertisement
elisim

ass3_q3

Jun 10th, 2017
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.64 KB | None | 0 0
  1. # Eli Simhayev - 2017
  2. # Question 3
  3.  
  4. #==== task b ====
  5. Let Ar := A*r⁽ᵏ⁾. Hence:
  6.     α = <r⁽ᵏ⁾, A*r⁽ᵏ⁾>/<(A*r⁽ᵏ⁾)ᵀ,A*r⁽ᵏ⁾> = <r⁽ᵏ⁾,Ar>/<(Ar)ᵀ, Ar>
  7. So Ar is the only computation of matrix vector multiplication, as we desired. =#
  8.  
  9. # ==== task c ====
  10. function miners(A,b,x,maxIter)
  11.     residual_norms = zeros(maxIter+1)
  12.     r = b-A*x; # r⁽⁰⁾
  13.     residual_norms[1] = norm(r);
  14.  
  15.     for k=1:maxIter
  16.         Ar = A*r; # save the result for the next computations
  17.         alpha = dot(r,Ar)/dot(transpose(Ar),Ar);
  18.         x = x+alpha*r;
  19.         r = r-alpha*A*r;
  20.         residual_norms[k+1] = norm(r); # save residual norm history
  21.     end
  22.  
  23.     return x, residual_norms;
  24. end
  25.  
  26. A = [5 4 4 -1 0;
  27.      3 12 4 -5 -5;
  28.      -4 2 6 0 3;
  29.      4 5 -7 10 2;
  30.      1 2 5 3 10]
  31.  
  32. b = ones(5);  # b = [1,1,1,1,1]ᵀ
  33. x = zeros(5); # x⁽⁰⁾ = [0,0,0,0,0]ᵀ
  34. maxIter = 50;
  35.  
  36. miners_ans, miners_norms = miners(A,b,x,maxIter);
  37. println("A*miners_ans-b: ", A*miners_ans-b);
  38. # A*miners_ans-b: [3.29778e-9,-4.38476e-9,-2.78555e-9,-8.85434e-9,7.79554e-9]
  39. #### Method indeed converge
  40.  
  41. ### plot
  42. using PyPlot;
  43. figure();
  44. y = miners_norms;
  45. semilogy(0:maxIter,y,"-r");
  46. title("Residual norms");
  47. xlabel("0:$maxIter");
  48. ylabel("Residual norm");
  49. show();
  50.  
  51. #==== task d ====
  52. The residual is defined as r⁽ᴷ⁾ = b-Ax⁽ᴷ⁾.
  53. In each iteration, x⁽ᴷ⁾ becomes closer to the real solution x*
  54. because by definition of convergence: lim (k to ∞) x⁽ᴷ⁾ = x*.
  55. Because of the fact that Ax* = b, we can infer that r⁽ᴷ⁾ monotonically decreases to zero,
  56. which explains why the graph of r⁽ᴷ⁾ is monotone. =#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement