"Invalid second data argument" error while using "plot" function (Lagrange Interpolation)

19 ビュー (過去 30 日間)
I want to take the graph of x and y points with plot function, pointx is okay, but there is a error with pointy1, and i think because it is a function respect to x.
When i delete @(x), it says "x is a undefined function or variable."
Can you help me?
clc;
clear all;
pointx = [-2 -1 0 1 2];
pointy1 =@(x) (2 - atan(x));
x = linspace(-5,5);
n = size(pointx, 2);
L = ones(n, size(x,2));
for i=1:n
for j=1:n
if (i~=j)
L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j));
end
end
end
y = 0;
for i=1:n
y = y + pointy1(i)*L(i,:);
end
y1 = y;
plot(pointx, pointy1, 'r', x, y1, 'c--o');
hold on;

採用された回答

Tommy
Tommy 2020 年 5 月 25 日
You are correct, it is complaining because pointy1 is a function handle. You can obtain the output of pointy1 when evaluated at the x values within pointx by calling pointy1(pointx):
clc;
clear all;
pointx = [-2 -1 0 1 2];
pointy1 =@(x) (2 - atan(x));
x = linspace(-5,5);
n = size(pointx, 2);
L = ones(n, size(x,2));
for i=1:n
for j=1:n
if (i~=j)
L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j));
end
end
end
y = 0;
for i=1:n
y = y + pointy1(i)*L(i,:);
end
y1 = y;
plot(pointx, pointy1(pointx), 'r', x, y1, 'c--o');
hold on;
This works because your pointy1 function is vectorized.
  2 件のコメント
Yigit Apaydin
Yigit Apaydin 2020 年 5 月 25 日
編集済み: Yigit Apaydin 2020 年 5 月 25 日
thank you, it helped very much.
Tommy
Tommy 2020 年 5 月 25 日
Happy to help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by