How to for loop that callsout a cell of a matrix, put it another matrix, roots that matrix, and repeat until all of the cells are called out

1 次查看(过去 30 天)
Case:
I'm trying to solve for roots of a quadratic equation that changes it's last term by adding whatever is in each called out matrix cell using a for loop function. I'm not very experienced in Matlab. How do I solve the issue below?
Current code:
A = -1000:100:1000;
for i=1:length(A)
B(i)=[1 3 A(i)+60]; %like 1*x^2 + 3*x + A+60
C(i)=roots(B(i)); %solves for the root of each B(i)
i=i+1; %repeats until the goes through each cell of matrix A
end
Result: Unable to perform assignment because the indices on the left side are not compatible with the size of the right size.

采纳的回答

Walter Roberson
Walter Roberson 2021-1-23
A = -1000:100:1000;
for i=1:length(A)
B{i}=[1 3 A(i)+60]; %like 1*x^2 + 3*x + A+60
C{i}=roots(B{i}); %solves for the root of each B(i)
end
C{2}
ans = 2×1
-30.5215 27.5215
C{3}
ans = 2×1
-28.7443 25.7443
syms x A
both_roots = solve(x^2 + 3 * x + A + 60, x)
both_roots = 
solutions = double(subs(both_roots, A, -1000:100:1000));
solutions(:,2:3)
ans = 2×2
-30.5215 -28.7443 27.5215 25.7443

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by