Advertisement
juaniisuar

Numeric Methods: notes

Oct 6th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.02 KB | None | 0 0
  1. // minimos cuadrados
  2. // A'A x = A' b
  3. // si rang(A) = n, R no singular
  4. // Rx = Q'b, se resuelve por sust. regresiva
  5.  
  6.  
  7. function [Qu, R]= factorizacionQR(A)
  8.     [m, n] = size(A)
  9.     Qu = zeros(A)  
  10.     R = zeros(n,n)
  11.    
  12.     Qu(:, 1) = A(:, 1)
  13.     R(1,1) = norm(A(:, 1), 2)
  14.     Qu(:, 1) = Qu(:, 1) / R(1,1)
  15.        
  16.  
  17.     for j = 2:n
  18.         //Lleno la columna j de R
  19.         for i = 1:j-1
  20.             R(i,j) = A(:, j)' * Qu(:, i)
  21.         end
  22.    
  23.         //Completo la columna j de Q.
  24.         Qu(:, j)=  A(:, j) - Qu(:, 1:j-1) * R(1:j-1, j)
  25.        
  26.         //Completo la posición de la diagonal de R
  27.         R(j,j) = norm(Qu(:, j), 2)
  28.         Qu(:, j) = Qu(:, j) / R(j,j)
  29.     end
  30. endfunction
  31.  
  32. function sol = f1(x)
  33.     sol = 1 + x(1)**2 - x(2)**2 + %e**x(1) * cos(x(2))
  34. endfunction
  35.  
  36. function sol = f2(x)
  37.     sol = 2*x(1)*x(2) + %e**x(1) * sin(x(2))
  38. endfunction
  39.  
  40. function vec = F(x)
  41.     vec(1) = f1(x)
  42.     vec(2) = f2(x)
  43. endfunction
  44.  
  45. function sol = Newton(f, vecini)
  46.     eps = 10**-5
  47.     iter = 0
  48.    
  49.     vec = vecini - inv(numderivative(f, vecini)) * f(vecini)
  50.    
  51.     while iter < 20 & (f(vecini) ~= eps)
  52.         iter = iter + 1
  53.         vec = vecini - inv(numderivative(f, vecini)) * f(vecini)
  54.         vecini = vec
  55.     end
  56.    
  57.     sol = vec
  58. endfunction
  59.  
  60. function sol = f6(x)
  61.     sol = x + (1/2.5)*(x**2 - 5)
  62. endfunction
  63.  
  64. function sol = ptofijo6(g, x)
  65.     Eps = 10**-5
  66.     Iter = 0
  67.  
  68.     while (Iter < 100) & (abs(g(x)-Eps) > 0)
  69.         x = g(x)
  70.         Iter = Iter + 1
  71.     end
  72.    
  73.     sol = x
  74.    
  75. endfunction
  76.  
  77. function sol = secante(f, x0, x1)
  78.     Eps = 10**-6
  79.     x2 = 0
  80.    
  81.     while abs(x1 - x0) > Eps
  82.         x2 = x1 - (x1-x0)/(f(x1)-f(x0)) * f(x1)
  83.         x0 = x1
  84.         x1 = x2
  85.     end
  86.     sol = x2
  87.    
  88. endfunction
  89.  
  90. function sol = biseccion(f, a, b)
  91.     Eps = 10**-3
  92.     mid = (a-b)/2 + a
  93.    
  94.     while (b-a)/2 > Eps
  95.         mid = (b-a)/2 + a
  96.         if(f(a)*f(mid) <= 0) b=mid
  97.         else a=mid
  98.         end
  99.     end
  100.  
  101.     sol = mid
  102. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement