Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #Comparison
  2. #As mentioned in Holmes, cubic B-splines reduce the problem down to having to solve for (approximately) n unknowns.
  3. #The advantage of B-splines is that they are easier to code.
  4. #This procedure is faster than the algorithm presented in the pre-class work, and requires
  5. #minimal storage, which means finding the coefficients for a cubic spline is
  6. #fairly easy even for a large number of interpolation points.
  7. #They are also very useful for least squares fitting of data,
  8. #as well when solving differential equations numerically [Holmes, 20016].
  9. #The only problem that can present is that if the matrix A is not invertible, the algorithm will not converge.
  10. #The algorithm presented in the pre class work is easy to understand since is based on the primary conditions
  11. #and assumptions of the theory. However, this hard-coded algorithm is just for input vectors of size 3.
  12. #Time and space complexity will depend on the functions to solve.
  13.  
  14. function k= B_cubic(x0, y0)
  15.  
  16. #We will use the same assumptions presented in the Assignment, also considering that h=1.
  17. #We will conside the form: Ck = z
  18.  
  19. C = []; # coefficients of k
  20. z = []; #
  21.  
  22. n = size(x0)(1);
  23.  
  24. for i = 1:(n-2)
  25. disp(i);
  26.  
  27. #d: equation derived equation for the unknown ks in terms of the known ys.
  28. Y = [Y; 6.* (y0(i+2)-2.* y0(i+1) - y0(i))];
  29.  
  30. #appending the rows
  31. c = zeros(1, n);
  32. c(i) = 1;
  33. c(i+1) = 4;
  34. c(i+2) = 1;
  35.  
  36. C = [C; c];
  37.  
  38. endfor
  39.  
  40. #as mentioned before, we are considering the natural spline conditions k1=kn=0
  41. Y = [0; Y; 0];
  42.  
  43. c = zeros(1,n);
  44. c(1) = 1;
  45.  
  46. C = [c; C];
  47. c = zeros(1,n);
  48. c(n) = 1;
  49.  
  50. C = [C; c];
  51.  
  52. #Obtaining the values of ks
  53. k = C^(-1)*z;
  54.  
  55. return
  56. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement