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.
0 个评论
采纳的回答
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}
C{3}
syms x A
both_roots = solve(x^2 + 3 * x + A + 60, x)
solutions = double(subs(both_roots, A, -1000:100:1000));
solutions(:,2:3)
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!