I am writing a code that plots Lagrange-Polynomial-Interpolation and the part that's messing up is the plot(X,P,'b');
I have inserted a % to make it a comment and test the rest of the code and the rest of the code works. But when I remove the % from the plot(X,P,'b'); line, an error pops up at this line. What am I doing wrong?
Here's the entire code:
f = @(x) 3*exp(-x)+2*sin(x);
X = 0:3;
Y = f(X);
n = length(X);
P = 0;
syms x;
L = sym(zeros(size(X)));
for i = 1:n
L(i) = 1;
for j = 1:n
if (j ~= i)
L(i) = L(i)*(x-X(j))/(X(i)-X(j));
end
end
P = P + Y(i)*L(i);
end
disp(P)
plot(X,P,'b');
hold on;
grid on;
fplot(f,[0,3],'r');
hold on;
plot(X,Y,'or');

 채택된 답변

KSSV
KSSV 2022년 2월 8일

0 개 추천

f = @(x) 3*exp(-x)+2*sin(x);
X = 0:3;
Y = f(X);
n = length(X);
P = 0;
syms x;
L = sym(zeros(size(X)));
for i = 1:n
L(i) = 1;
for j = 1:n
if (j ~= i)
L(i) = L(i)*(x-X(j))/(X(i)-X(j));
end
end
P = P + Y(i)*L(i);
end
disp(P)
P = double(subs(P,X));
plot(X,P,'b');
hold on;
grid on;
fplot(f,[0,3],'r');
hold on;
plot(X,Y,'or');

댓글 수: 1

Paul  Jones
Paul Jones 2022년 2월 8일
It worked! what is the reason to use double(subs as opposed to what I was doing?

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

2022년 2월 8일

댓글:

2022년 2월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by