Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %(1) Plot the original points in the correct part, top left
- %(2) When sorting points, add the "ind" to index y to not change them matching.
- %Index y with "ind" and store in y
- %(3) Change the both floor commands to ceil and also multiply the
- %accuracyFactor instead of adding it because it is a factor.
- %(4) When calling lagrangeInterp, We are finding for yy(i) not xx, and also change yy(i) to xx(i).
- %switch y and x
- %(5)Make the line blue for our spline
- %(6)When defining splineYY, switch the x and xx
- %(7)Add the color of the line to be blue when plotting matlabs spline
- %(8)Make the entire bottem part the graph by telling subplot(2,2,3:4)
- %(9)start interpY at 0 not 1
- %(10)When defining product, change the part after prod(x(i) to -[x(1:i-1), x(i+1:end)]
- function [xx, yy] = mySplinePlot(x, y, accuracyFactor)
- % Plot the original points
- subplot(2,2,1) %%%(1)This should go on the top left
- hold on
- plot(x,y,'ro')
- title('mySplinePlot')
- % Sort the points by increasing x-coordinates
- [x ind] = sort(x); %%%(2)Get the positions before changed
- y = y(ind); %%%(2)index y with ind
- % Find the number of points based on the max difference between x-values
- % and create an xx vector of this number of points, evenly spaced. The max
- % difference between adjacent points should be rounded up, as should the
- % result after applying the accuracy factor.
- pointsPerInterval = ceil(ceil(max(diff(x))).*accuracyFactor); %%%(3)change floor to ceil in both cases and also multiply
- %accuracyFactor instead of adding
- totalPoints = (length(x))*pointsPerInterval + length(x);
- xx = linspace(x(1), x(end), totalPoints);
- % Interpolate to find a yy vector using the Lagrange Interpolation
- % Polynomial. Initialize yy at the right size for improved efficiency.
- yy = zeros(1,totalPoints);
- for i = 1:totalPoints
- 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
- end
- % Plot the vectors just calculated.
- plot(xx, yy, 'b') %%%(5)Make the line blue
- % Call Matlab's spline and plot it as described in the problem statement
- splineYY = spline(x, y, xx); %%%(6)switch the x and xx
- subplot(2,2,2)
- hold on
- plot(x,y,'ro')
- plot(xx, splineYY , 'b') %%%(7)Add the color of the line to be blue
- title('spline')
- % Make a plot as described in the problem statement and as shown in the
- % solution figures comparing mySplinePlot with spline
- subplot(2,2,3:4) %%%(8)Make the entire bottem part the graph
- plot(xx, yy, 'r')
- hold on
- plot(xx, splineYY, 'k--')
- legend('mySplinePlot','spline')
- title('mySplinePlot vs. spline')
- plot(x,y,'ro')
- hold off
- end
- % This function performs the actual Lagrange Interpolation, returning an
- % interpolated y-value for a single given x-value. Do NOT change this
- % function header.
- function interpY = lagrangeInterp(x, y, interpX)
- % Initialize interpY and determine the number of terms in the product The
- % new interpolated Y value will then be increased based on the requirements
- % of the Lagrange Interpolation.
- interpY = 0; %%%(9)start interpY at 0 not 1
- n = length(x);
- % Make an estimation for each known x-coordinate using the formula given.
- for i = 1:n
- % If this makes more sense to you in a for loop, you can include a
- % nested for loop here. This is where the actual product from the
- % Lagrange Interpolation formula comes in.
- % P(x) = y_k*PRODUCT((x-x_k)/(x_i-x_k), where [1<=<=n & i~=k])
- product = prod(interpX - [x(1:i-1), x(i+1:end)])/prod(x(i) - [x(1:i-1), x(i+1:end)]);
- %%%(10)change the part after prod(x(i) to -[x(1:i-1), x(i+1:end)]
- % Update the total for interpY based on the value found for the ith
- % coordinate
- interpY = interpY + product * y(i);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement