MATLAB Answers


How to code this problem?

John Amitage さんによって質問されました 2019 年 5 月 10 日
最新アクティビティ James Tursa
さんによって コメントされました 2019 年 5 月 10 日
For example,
I got
A=[ 1 2 4 5 1 .......]
I want to calculate:
And so on
So how can I code it on Matlab?
Thank you very much!

  2 件のコメント

James Tursa
2019 年 5 月 10 日
What have you done so far? What specific problems are you having with your code? Are you after a symbolic result, or a numeric result for a specific value of x?
John Amitage 2019 年 5 月 10 日
Hi, thank you for your answer
I've tried
syms x
X=[1 2 3]
for i=2:n
And it gives me A=(x-2)(x-3)
So I'm finding a way to make the program also gives me B=(x-1)(x-3) and C=(x-1)(x-2) and generalize it so when I input any vector X with any n=size(X,2) it can give me the full answer.
Specifically, I'm doing Lagrange Interpolation
Thank you
(sorry for my English)

サインイン to comment.




1 件の回答

James Tursa
回答者: James Tursa
2019 年 5 月 10 日

syms x
A = [1 2 4 5];
n = numel(A);
P = prod(x-A);
X = cell(n,1);
for k=1:n
X{k} = P / (x-A(k));
This gives a cell array
>> X{1}
ans =
(x - 2)*(x - 4)*(x - 5)
>> X{2}
ans =
(x - 1)*(x - 4)*(x - 5)
>> X{3}
ans =
(x - 1)*(x - 2)*(x - 5)
>> X{4}
ans =
(x - 1)*(x - 2)*(x - 4)

  10 件のコメント

James Tursa
2019 年 5 月 10 日
So, what is the problem? It looks like the code works for you. What does this show after you run the code:
Aren't those the polynomials you want?
John Amitage 2019 年 5 月 10 日
ahh I understand now, i'm so sorry for taking your time :D
Thank you and have a good day !
James Tursa
2019 年 5 月 10 日
No problem! Note that it is easy to generalize this to an arbitrary sized "A" variable by using a cell array. It would be horrendous to try and implement this with named variables X1, X2, etc.

サインイン to comment.

Translated by