Advertisement
adityass

Untitled

Jun 26th, 2024
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.23 KB | None | 0 0
  1. %% GAUSS elimination
  2.  
  3. A = input("Enter matrix") ;
  4. B = input("Enter source vector");
  5. N = length(B);
  6. X = zeros(N,1);
  7. Aug = [A B];
  8.  
  9. for j = 1:N-1
  10.     for i = j+1:N
  11.         m = Aug(i,j)/Aug(j,j);
  12.         Aug(i,:) = Aug(i,:) - m*Aug(j,:);
  13.     end
  14. end
  15.  
  16. Aug
  17.  
  18. X(N) = Aug(N, N+1)/Aug(N,N) ;
  19.  
  20. for k = N-1:-1:1
  21.     X(k) = (Aug(k , N+1) - Aug(k, k+1:N)*X(k+1:N))/ Aug(k,k);
  22. end
  23.  
  24. X
  25.  
  26.  
  27. %% GAUSS SEIDEL
  28.  
  29. A = input("Enter matrix") ;
  30. B = input("Enter source vector");
  31. N = length(B);
  32. X = zeros(N,1);
  33. n = 10 ;
  34. Y = zeros(N,1);
  35. P = input('initial giess vector ');
  36. e = 0.0001;
  37.  
  38.  
  39. for j = 1:n
  40.     for i = 1:N
  41.         X(i) = (B(i)/A(i,i)) - (A(i, [1:i-1, i+1:N]) * P([1:i-1, i+1:N])) / A(i,i);
  42.         P(i) = X(i);
  43.     end
  44.     if abs(Y-X)<e
  45.         break
  46.     end
  47.     Y=X;
  48.  
  49. end
  50.  
  51. X
  52.  
  53.  
  54. %% POWER METHOD
  55.  
  56. A = input("Enter matrix") ;
  57. v = input("Enter guess");
  58. n = 30 ;
  59. e = 0.001 ;
  60. M = 0 ;
  61. vO = v ;
  62.  
  63. for j = 1 :n
  64.     v = A*vO ;
  65.     M = max(v);
  66.     v = v/M ;
  67.     if abs(v-vO)<e
  68.         break
  69.     end
  70.     vO = v ;
  71. end
  72.  
  73. B
  74.  
  75. %% NEWTON
  76.  
  77. clc
  78. clear all
  79.  
  80. f = @(x) 2^x - 5*x + 2 ;
  81. df = @(x) log(2)*(2^x) - 5 ;
  82. e = 0.00000000000001 ;
  83. xO = 0 ;
  84. n = 10 ;
  85.  
  86. for i = 1:n
  87.     x1 = xO - f(xO)/df(xO);
  88.     if abs(x1-xO) < e
  89.         break ;
  90.     end
  91.     xO = x1 ;
  92.     disp(xO)
  93. end
  94.  
  95. %% SECANT
  96.  
  97.  
  98. f = @(x) x - 5*x^2 + 2 ;
  99. e =0.000000000000000000001 ;
  100. x0 = 0 ;
  101. x1 = 1 ;
  102. x2 = 0 ;
  103. n = 10 ;
  104.  
  105. for i = 1:n
  106.     x2 = (x0*f(x1) - x1*f(x0))/(f(x1) - f(x0));
  107.     if abs(x2-x1)<e
  108.         break
  109.     end
  110.     disp(x2)
  111.     x1 = x2;
  112.     x0 = x1;
  113.  
  114. end
  115.  
  116. %% FIXED POINT
  117.  
  118. f = @(x) 2^x -5*x + 2 ;
  119. g = @(x) (2^x + 2 )/5 ;
  120.  
  121. e = 0.00001 ;
  122. xO = 0 ;
  123. n = 10 ;
  124. x1 = 0 ;
  125.  
  126.  
  127. for i = 1:n
  128.     x1 = g(xO);
  129.     if abs(xO-x1)<e
  130.         disp(x1)
  131.         break
  132.     end
  133.     xO = x1 ;    
  134. end
  135.  
  136.  
  137.  
  138. %%BISECTION
  139.  
  140.  
  141. clc
  142. clear ALL
  143.  
  144. f = @(x) 2^x - 5*x + 2 ;
  145. a = 0 ;
  146. b =1 ;
  147. n = 30 ;
  148. e =0.0001 ;
  149.  
  150. c = 0 ;
  151.  
  152. if f(a)*f(b)<0
  153.     for i=1:n
  154.         c = (a+b)/2 ;
  155.         if abs(c-b) < e || abs(c-a) <e
  156.             disp(i)
  157.             break;
  158.         end
  159.         if f(a)*f(c)<0
  160.             b = c ;
  161.         elseif f(b)*f(c)<0
  162.             a = c ;
  163.         end
  164.     end
  165.  
  166. else
  167.     disp('no') ;
  168.  
  169. end
  170.  
  171. disp(c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement