Info
此问题已关闭。 请重新打开它进行编辑或回答。
How to code this problem?
1 次查看(过去 30 天)
显示 更早的评论
For example,
I got
A=[ 1 2 4 5 1 .......]
I want to calculate:
X1=(x-2)(x-4)(x-5)(x-1).....
X2=(x-1)(x-4)(x-5)(x-1)....
X3=(x-1)(x-2)(x-5)(x-1)....
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?
回答(1 个)
James Tursa
2019-5-10
E.g.,
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));
end
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!