Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. %(1) Plot the original points in the correct part, top left
  2. %(2) When sorting points, add the "ind" to index y to not change them matching.
  3. %Index y with "ind" and store in y
  4. %(3) Change the both floor commands to ceil and also multiply the
  5. %accuracyFactor instead of adding it because it is a factor.
  6. %(4) When calling lagrangeInterp, We are finding for yy(i) not xx, and also change yy(i) to xx(i).
  7. %switch y and x
  8. %(5)Make the line blue for our spline
  9. %(6)When defining splineYY, switch the x and xx
  10. %(7)Add the color of the line to be blue when plotting matlabs spline
  11. %(8)Make the entire bottem part the graph by telling subplot(2,2,3:4)
  12. %(9)start interpY at 0 not 1
  13. %(10)When defining product, change the part after prod(x(i) to -[x(1:i-1), x(i+1:end)]
  14. function [xx, yy] = mySplinePlot(x, y, accuracyFactor)
  15.  
  16. % Plot the original points
  17. subplot(2,2,1) %%%(1)This should go on the top left
  18. hold on
  19. plot(x,y,'ro')
  20. title('mySplinePlot')
  21.  
  22. % Sort the points by increasing x-coordinates
  23. [x ind] = sort(x); %%%(2)Get the positions before changed
  24. y = y(ind); %%%(2)index y with ind
  25.  
  26.  
  27. % Find the number of points based on the max difference between x-values
  28. % and create an xx vector of this number of points, evenly spaced. The max
  29. % difference between adjacent points should be rounded up, as should the
  30. % result after applying the accuracy factor.
  31. pointsPerInterval = ceil(ceil(max(diff(x))).*accuracyFactor); %%%(3)change floor to ceil in both cases and also multiply
  32. %accuracyFactor instead of adding
  33. totalPoints = (length(x))*pointsPerInterval + length(x);
  34. xx = linspace(x(1), x(end), totalPoints);
  35.  
  36.  
  37. % Interpolate to find a yy vector using the Lagrange Interpolation
  38. % Polynomial. Initialize yy at the right size for improved efficiency.
  39. yy = zeros(1,totalPoints);
  40. for i = 1:totalPoints
  41. yy(i) = lagrangeInterp(x, y, xx(i)); %%%(4)We are finding for yy(i) not xx, and also change yy(i) to xx(i). switch y and x
  42.  
  43. end
  44.  
  45. % Plot the vectors just calculated.
  46. plot(xx, yy, 'b') %%%(5)Make the line blue
  47.  
  48.  
  49.  
  50.  
  51. % Call Matlab's spline and plot it as described in the problem statement
  52. splineYY = spline(x, y, xx); %%%(6)switch the x and xx
  53. subplot(2,2,2)
  54. hold on
  55. plot(x,y,'ro')
  56. plot(xx, splineYY , 'b') %%%(7)Add the color of the line to be blue
  57. title('spline')
  58.  
  59.  
  60. % Make a plot as described in the problem statement and as shown in the
  61. % solution figures comparing mySplinePlot with spline
  62. subplot(2,2,3:4) %%%(8)Make the entire bottem part the graph
  63. plot(xx, yy, 'r')
  64. hold on
  65. plot(xx, splineYY, 'k--')
  66. legend('mySplinePlot','spline')
  67. title('mySplinePlot vs. spline')
  68. plot(x,y,'ro')
  69. hold off
  70.  
  71. end
  72.  
  73.  
  74. % This function performs the actual Lagrange Interpolation, returning an
  75. % interpolated y-value for a single given x-value. Do NOT change this
  76. % function header.
  77. function interpY = lagrangeInterp(x, y, interpX)
  78.  
  79. % Initialize interpY and determine the number of terms in the product The
  80. % new interpolated Y value will then be increased based on the requirements
  81. % of the Lagrange Interpolation.
  82. interpY = 0; %%%(9)start interpY at 0 not 1
  83. n = length(x);
  84.  
  85. % Make an estimation for each known x-coordinate using the formula given.
  86. for i = 1:n
  87.  
  88. % If this makes more sense to you in a for loop, you can include a
  89. % nested for loop here. This is where the actual product from the
  90. % Lagrange Interpolation formula comes in.
  91. % P(x) = y_k*PRODUCT((x-x_k)/(x_i-x_k), where [1<=<=n & i~=k])
  92.  
  93. product = prod(interpX - [x(1:i-1), x(i+1:end)])/prod(x(i) - [x(1:i-1), x(i+1:end)]);
  94. %%%(10)change the part after prod(x(i) to -[x(1:i-1), x(i+1:end)]
  95. % Update the total for interpY based on the value found for the ith
  96. % coordinate
  97. interpY = interpY + product * y(i);
  98. end
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement