How to code this problem?
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti
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 Commenti
James Tursa
il 10 Mag 2019
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
il 10 Mag 2019
Risposte (1)
James Tursa
il 10 Mag 2019
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 Commenti
John Amitage
il 10 Mag 2019
James Tursa
il 10 Mag 2019
Modificato: James Tursa
il 10 Mag 2019
What is the A variable you are using? What version of MATLAB are you using?
John Amitage
il 10 Mag 2019
James Tursa
il 10 Mag 2019
And A?
John Amitage
il 10 Mag 2019
James Tursa
il 10 Mag 2019
Look at your original post, you have this variable
A=[ 1 2 4 5 1 .......]
I am asking you what "A" variable you are using with the code I have given you. The code works for me as I have demonstrated.
John Amitage
il 10 Mag 2019
James Tursa
il 10 Mag 2019
Modificato: James Tursa
il 10 Mag 2019
So, what is the problem? It looks like the code works for you. What does this show after you run the code:
X{1}
X{2}
X{3}
X{4}
Aren't those the polynomials you want?
John Amitage
il 10 Mag 2019
James Tursa
il 10 Mag 2019
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.
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!.png)
