- Vectorization: To improve the efficiency of the code, I have vectorize the inner loop where possible. This can reduce the execution time significantly, especially for large datasets.
- Prevent Division by Zero: I have added a small check to ensure that the denominator in the interpolation formula does not become zero, which would cause the computation to fail.
Creating a function to calculate polynomial interpolation
76 次查看(过去 30 天)
显示 更早的评论
I'm trying to get my program to provide the best polynomial interpolation fit with the given degree n.
And I'm not allowed by my professor to use High-level curve fitting functions in my function or algorithm.
He said I could use other basic Matlab functions such as fprintf, error, linspace, as well as all linear algebra features, including the
backslash operator.
He wants my code to produce a smooth graph, with interpolation points (shown as circles) and the vector-defined evaluation points (shown as plus-signs).
He says the curve should be generated by my code, and it should use enough points to make a smooth curve.
He also say to ensure that my code will work for non-trivial (non-polynomial) cases.
I need help writing this function. Below is what I've worked on so far.
I have made my inputs:
interpolation point x-values – x
Interpolation point y-values – y
Order of polynomial interpolation – n
Evaluation point location – Ex
And my output is:
Evaluation point levels – Ey
And this is the code I have so far:
function [Ey] = HuckabeeJCurveFit(x,y,n,Ex)
if length (y)~=n
error('x and y must be the same length');
end
s = 0;
for i = 1:n
product = y(i);
for j = 1:n
if i~=j
product = product*(Ex-x(j))/(x(i)-x(j));
end
end
s = s+product;
end
Ey = s;
0 个评论
回答(1 个)
Rupesh
2024-2-22
编辑:Rupesh
2024-2-29
Hi James Huckabee,
I understand that you want to create a MATLAB function that performs polynomial interpolation with basic linear algebra operations like “linspace” and others. Based on the structure of the code snippet you've provided; I am assuming that you may be attempting to implement a form of polynomial interpolation which resembles the “Lagrange interpolation” method. This method is often used when you want to estimate the value of a function at a given set of points, using a known set of data points. Now, based upon the assumption that the method you are implementing is similar to the Lagrange interpolation method, I have made some modifications to the code to provide a more robust solution. Here are the following changes:
Below is a sample function you can refer to.
function [Ey] = HuckabeeJCurveFit(x, y, n, Ex)
% Validate that the input vectors x and y are of equal length
if length(x) ~= length(y)
error('x and y must be the same length');
end
% Ensure that n matches the length of the input vectors
if length(x) ~= n
error('The length of x and y must match the provided n');
end
% Initialize the output vector Ey for the evaluated points
Ey = zeros(size(Ex));
% Compute the interpolated values at points Ex
for k = 1:length(Ex)
s = 0;
for i = 1:n
% Compute the Lagrange basis polynomial for the i-th term
L = 1;
for j = 1:n
if i ~= j
% Ensure the denominator is not zero
if (x(i) - x(j)) == 0
error('Two data points have the same x-value, which leads to division by zero in the interpolation formula.');
end
L = L * (Ex(k) - x(j)) / (x(i) - x(j));
end
end
% Add the i-th term to the sum
s = s + y(i) * L;
end
% Store the computed value in the output vector
Ey(k) = s;
end
end
Here is the sample input which you can modify as per your needs
x = [1, 2, 3, 4, 5]; % Example x data points
y = [5, 2, 1, 2, 5]; % Corresponding y data points
n = length(x); % Number of data points
Ex = 1:0.1:5; % X-values for evaluating the polynomial
% Calling HuckabeeJCurveFit to obtain interpolated values
Ey = HuckabeeJCurveFit(x, y, n, Ex);
% Plot the original data points
plot(x, y, 'o', 'MarkerFaceColor', 'r', 'MarkerSize', 8);
hold on;
% Plot the interpolated curve
plot(Ex, Ey, 'b-', 'LineWidth', 2);
% Add labels for the x and y axes
xlabel('X-values');
ylabel('Y-values');
% Add a title to the graph
title('Polynomial Interpolation using Lagrange Method');
% Add a legend to the graph
legend('Data Points', 'Interpolated Curve', 'Location', 'best');
% Add a grid to the graph for better readability
grid on;
% Hold off to finish the plotting
hold off;
Below is the sample output you can expect after execution of above script.
You can also refer to below documents related to Lagrange interpolation for better understanding.
Hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!