Guest User

Untitled

a guest
Sep 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.36 KB | None | 0 0
  1. clear()
  2. tol = 10e-10
  3. MAX_ITERATIONS = 1000
  4.  
  5. function [R]=newtonEigen(A)
  6.     R = []
  7.     err = []
  8.     iter = 1
  9.     if size(A,1) ~= size(A,2) then
  10.         printf(" ERROR: Input matrix must be square!\n")
  11.         return
  12.     end
  13.     x = int(rand(size(A,1),1)*10)
  14.     x = x/norm(x)
  15.     lambda = x'*A*x
  16.     n = length(A)
  17.     while iter ~= MAX_ITERATIONS
  18.         //printf("lambda = %f\n",lambda)
  19.         J = [[A -x]; 2*x' 0]
  20.         for i = 1:3
  21.             J(i,i) = J(i,i) - lambda
  22.         end
  23.         B = [A*x-lambda*x ; x'*x-1]
  24.         s = GaussianElim(J,-B)
  25.         x = x+s(1:size(A,1))
  26.         lambda = lambda + s(size(A,1)+1)
  27.        
  28.         err = [err,norm(s,'inf')]
  29.         if (err($) < tol) then
  30.             q = log10((err($)/err($-1))) / log10((err($-1)/err($-2)))
  31.             printf(" iterations = %d\n q(order of convergence) = %f\n",iter,q)
  32.             disp(lambda,"lambda = ")
  33.             R = x
  34.             return
  35.         end
  36.         iter = iter + 1
  37.     end
  38. endfunction
  39.  
  40. function [G] = GaussianElim(A, b)
  41.     G = []
  42.     if det(A) == 0 then //check the determinant first
  43.         error("Error: The matrix is linearly dependent")
  44.         return
  45.     elseif size(A,1) ~= size(b,1) //check for size consistency
  46.         error("Error: Check your matrix sizes")
  47.         return
  48.     end
  49.     M = [A, b] //augment the matrices
  50.     rowSize = size(M,1)
  51.     colSize = size(M,2)
  52.     for i = 1:1:rowSize
  53.         //find the pivot
  54.         [pivotValue, pivotIndex] = max(abs(M(i:rowSize,i)))
  55.         pivotIndex = pivotIndex + (i - 1)
  56.         //swap
  57.         tempRow = M(i,:)
  58.         M(i,:) = M(pivotIndex,:)
  59.         M(pivotIndex,:) = tempRow
  60.         //perform ero2 first
  61.         M(i,:) = M(i, :) / M(i,i)
  62.         //i-zero mo na
  63.         for j = i+1:1:rowSize
  64.             mul = M(j,i)
  65.             M(j,:) = M(j,:) - (M(i, :) * mul)
  66.         end
  67.      end
  68.      //start back substitution
  69.          for i = rowSize - 1:-1:1
  70.              for j = i + 1:rowSize
  71.                  M(i,$) = M(i,$) - (M(i,j) * M(j,$))
  72.              end
  73.              M(i,$) = M(i,$) / M(i,i)
  74.          end
  75.      G = M(1:rowSize,$)
  76. endfunction
  77.  
  78. A = [4,-0.5,0; 0.6,5,-0.6; 0, 0.5 3]
  79. B = [2.9766 0.3645 0.4198 1.1159;
  80.     0.3945 2.7328 -0.3097 0.1123;
  81.     0.4198 -0.3097 2.5675 0.6079;
  82.     1.1159 0.1129 0.6079 1.7231]
  83. disp(A, "A =")
  84. disp(newtonEigen(B),"Eigenvalue of A using newtons =")
Add Comment
Please, Sign In to add comment