For loop for roots function

5 vues (au cours des 30 derniers jours)
David
David le 9 Nov 2018
Commenté : madhan ravi le 9 Nov 2018
How do I create a for loop for the roots function? Here is a hypothetical data set:
x=[1:10:120]
for i=1:12
a(i) = x(i)*1
b(i) = x(i)*2
c(i) = x(i)*3
d(i) = x(i)*4
end
I now have a 1x12 vector for a, for b, c, and d.
I am not sure how to create a for loop that would take the first number in a, the first number in b, c, and d and then find then find the roots. Next the loop would use the second number in a, the second number in b, c, and d to find their roots. I run the loop and only the solutions for the first set of numbers is created.
Currently I am using:
for i=1:12
p = [a(i) b(i) c(i) d(i)]
r = roots(p)
end
Thanks for any help.

Réponse acceptée

madhan ravi
madhan ravi le 9 Nov 2018
r=cell(1,12);
for i=1:12
p = [a(i) b(i) c(i) d(i)]
r{i} = roots(p);
end
celldisp(r)
  3 commentaires
David
David le 9 Nov 2018
Thanks. That works.
madhan ravi
madhan ravi le 9 Nov 2018
Anytime :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by