Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % A-2 Q-1(a)
  2. A = [5 -1 1
  3.     2 8 -1
  4.     -1 2 4];
  5. b = [10
  6.     11
  7.     3];
  8.  
  9. D = diag(diag(A));
  10. LU = D - A;
  11.  
  12. f = @(x) (D\LU)*x + D\b;
  13.  
  14. x0 = [0;0;0];
  15. x = f(x0);
  16. while norm(x-x0) > 0.0001
  17.     x0 = x;
  18.     x = f(x);
  19. end
  20.  
  21. disp(x)
  22.  
  23.  
  24. % A-2 Q-1(b)
  25. A = [5 -1 1
  26.     2 8 -1
  27.     -1 2 4];
  28. b = [10
  29.     11
  30.     3];
  31.  
  32. D = diag(diag(A));
  33. L = D - tril(A);
  34. U = D - triu(A);
  35.  
  36. f = @(x) ((D-L)\U)*x + (D-L)\b;
  37.  
  38. x0 = [0;0;0];
  39. x = f(x0);
  40. while norm(x-x0) > 0.0001
  41.     x0 = x;
  42.     x = f(x);
  43. end
  44.  
  45. disp(x)
  46.  
  47.  
  48. % A-2 Q-1(c)
  49. A = [5 -1 1
  50.     2 8 -1
  51.     -1 2 4];
  52. b = [10
  53.     11
  54.     3];
  55.  
  56. D = diag(diag(A));
  57. L = D - tril(A);
  58. U = D - triu(A);
  59.  
  60. f = @(x) ((D-L)\U)*x + (D-L)\b;
  61.  
  62. x0 = [0;0;0];
  63. x = f(x0);
  64. w = 0.5;
  65. while norm(x-x0) > 0.0001
  66.     t = x0;
  67.     x0 = x;
  68.     x = (1-w)*t + w*f(x);
  69. end
  70.  
  71. disp(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement