hey can any one help me with this error?

조회 수: 1 (최근 30일)
zaid ghazal
zaid ghazal 2022년 5월 25일
편집: DGM 2022년 5월 25일
% 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
zaid ghazal
zaid ghazal 2022년 5월 25일

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2022년 5월 25일
편집: DGM 2022년 5월 25일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2007b

Community Treasure Hunt

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

Start Hunting!

Translated by