Help plotting interpolation ploynomial.

Hi, I was asked to plot psi_n(x) = (x-x_0)(x-x_1)...(x-x_n) for n = 3,7,10,17 evenly distributed nodes in [-5,5] with resolution 0.01. Below is my code:
x=-5:0.01:5;
n=[3 7 10 17];
for j=1:4
node=-5;
psi=1;
while (node <= 5)
psi=psi.*(x-node);
node=node + 10/n(j);
end
subplot(2,2,j);
plot(psi)
hold on
end
Which doesn't quite yield the desired output. Or does it? In any case, it doesn't seem to match what I am getting via using Wolfram. My question is why and, of course, how may I correct it? I'd sincerely appreciate your assistance. Thanks in advance!

댓글 수: 2

Simon
Simon 2013년 11월 1일
Hi!
What is x_0, x_1, ...?
Yuval
Yuval 2013년 11월 1일
The nodes. x_0 is x index zero.

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

답변 (1개)

Matt J
Matt J 2013년 11월 1일
편집: Matt J 2013년 11월 1일

0 개 추천

node=linspace(-5,5,n);
p=poly(node);
f=@(z) polyval(p,z);
x=-5:0.01:5;
plot(x,f(x));

댓글 수: 4

Yuval
Yuval 2013년 11월 1일
Hi Matt, Thank you for your reply. Would you kindly care to explain your code? Also, did you realise I wasn't trying to actually plot the interpolation polynomial but merely the product function psi? I just tried using your code and it yielded the following error message: ??? Error using ==> mrdivide Matrix dimensions must agree.
Error in ==> linspace at 22 y = [d1+(0:n-2)*(d2-d1)/(floor(n)-1) d2]; Error in ==> psierror at 2 node=linspace(-5,5,n);
Matt J
Matt J 2013년 11월 1일
편집: Matt J 2013년 11월 1일
Also, did you realise I wasn't trying to actually plot the interpolation polynomial but merely the product function psi?
From your description of psi_n(x), it appears to be a polynomial function of x with roots at the nodes x_0,...,x_n. That is what I have given you.
I just tried using your code and it yielded the following error message:
In my code, n is assumed to be a scalar. It runs error-free for me when this is the case.
Victor McNstein
Victor McNstein 2021년 3월 20일
Hello Matt. How does on combine your code with Yuval's initial work, for better results?
Matt J
Matt J 2021년 3월 21일
I do not see what Yuval thought was incomplete about my code. I think my code already fulfills the entire task.

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

카테고리

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

질문:

2013년 11월 1일

댓글:

2021년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by