hey can any one help me with this error?

1 view (last 30 days)
zaid ghazal
zaid ghazal on 25 May 2022
Edited: DGM on 25 May 2022
% Clear workspace & window
clear; clc;
% Create a vector for x values
x = [0, 1, 2, 3, 4, 5, 6, 7];
% Create a vector for y values
y = [0.5, -2, 3, 4, -1, 7, 5, 2];
% Set the value at which interpolated value has to be found
xp = 2.5;
% Call function to find the interpolated value at xp = 2.5
yp = Lagrange_IP(x, y, xp);
% Print the interpolated value
fprintf('The interpolated value at x = %.1f is %.6f\n', xp, yp);
% Create a vector for more x values
xp = linspace(x(1), x(end));
% Create a vector to store the interpolated values for each x values
yp = zeros(1, length(xp));
% Loop for each x values in vector
for i = 1 : length(xp)
% Call function to find the ith interpolated value
yp(i) = Lagrange_IP(x, y, xp(i));
end
% Plot the original values & interpolated values
plot(x, y, 'ro', xp, yp);
% Set x-axis label
xlabel('x');
% Set y-axis label
ylabel('y');
% Set title
title('Lagrange Interpolation');
% Function used to find the interpolated value using Lagrange Interpolation
function yp = Lagrange_IP(x, y, xp)
% Set the intial value of interpolated value
yp = 0;
% Loop for each value of x vector
for i = 1 : length(x)
% Used to find the interpolated value
p = 1;
% Loop for each value of x vector
for j = 1 : length(x)
% If the i and j both ae not equal means not same point
if i ~= j
% Compute the interpolated value for i & j
p = p * (xp - x(j))/(x(i) - x(j));
end
end
% Compute the interpolated value for ith value of y
yp = yp + p * y(i);
end
end
  5 Comments

Sign in to comment.

Accepted Answer

DGM
DGM on 25 May 2022
Edited: DGM on 25 May 2022
Okay, then that's what I suspected. Move the function section into its own file and save it. You can't have the function inside the script if you're running a version older than R2016b.
See the attached files.

More Answers (0)

Products


Release

R2007b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!