Advertisement
Guest User

a

a guest
May 6th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.39 KB | None | 0 0
  1. Laborator SD
  2.  
  3. http://geeksquiz.com/binary-search-tree-set-2-delete/
  4.  
  5.  
  6. MN:
  7. Neville:
  8.  
  9. function [yi, P] = neville(x, y, xi)
  10.  
  11. n = length(x);
  12. yi = zeros(1, length(xi));
  13. P = zeros(n, n);
  14. P(:, 1) = y;
  15.  
  16. for k = 1 : length(xi)
  17.     for i = 1 : n - 1
  18.         for j = 1 : (n - i)
  19.             P(j, i + 1) = ((xi(k) - x(j)) * P(j + 1, i) + (x(j + i) - xi(k)) * P(j, i))/(x(j + i) - x(j));
  20.         end
  21.     end
  22.  
  23.     yi(k) = P(1, n);
  24.    
  25. end
  26.  
  27. end
  28.  
  29.  
  30. Natural Spline:
  31.  
  32. function [s0,s1,s2,s3]=cubic_spline(x,y)
  33.  
  34. n = length(x);
  35.  
  36. h = x(2:n) - x(1:n-1);
  37. d = (y(2:n) - y(1:n-1))./h;
  38.  
  39. lower = h(1:end-1);
  40. main  = 2*(h(1:end-1) + h(2:end));
  41. upper = h(2:end);
  42. if n==2
  43.     m=0;
  44. else
  45. T = spdiags([lower main upper], [-1 0 1], n-2, n-2);
  46. rhs = 6*(d(2:end)-d(1:end-1));
  47.  
  48. m = T\rhs;
  49. end
  50.  
  51. % Use natural boundary conditions where second derivative
  52. % is zero at the endpoints
  53.  
  54. m = [ 0; m; 0];
  55.  
  56. s0 = y;
  57. s1 = d - h.*(2*m(1:end-1) + m(2:end))/6;
  58. s2 = m/2;
  59. s3 =(m(2:end)-m(1:end-1))./(6*h);
  60.  
  61. end
  62.  
  63. Plot Natural Spline:
  64.  
  65. function yy = plot_cubic_spline(x,y,xx)
  66.  
  67. n = length(xx);
  68. [s0, s1, s2, s3] = naturalspline(x', y');
  69. yy = zeros(1, n);
  70. for j = 1 : n
  71.     for k = 1 : length(x)
  72.         if xx(j) < x(k)
  73.             break;
  74.         end
  75.     end
  76.    
  77.     i = k - 1;
  78.    
  79.     yy(j) = s0(i) + s1(i) * (xx(j) - x(i)) + s2(i) * (xx(j) - x(i)).^2 + s3(i) * (xx(j) - x(i)).^3;
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement