How to create a matrix for plotting from a roots matrix in a loop?
2 次查看(过去 30 天)
显示 更早的评论
Hello,
So my problem is that currently I have a script that solves a polynomial of 4th order and gives me all the roots for it. However since it is for an engineering application I only need the logical value given by one of the roots.
My code goes something like this
while (condition)
code;
r = roots(x);
end
and this runs as long as the condition is true.
Because the user ends up producing many values of r, I was wondering how I can take for example the 4th position of r or r(4) and make a matrix of all the values of r(4) produced by the loop so that I can plot them.
If it helps the problem asks the user to input a ratio of Oxygen to MEthane and r(4) is the temperature of the reaction.
I need to plot this temperature vs the ratio.
Also I looked at things like
for i = 1:10
y(i) = 1 + rand
end
but can't seem to get that working with what I want.
Thank You in advance and feel free to ask for clarifications if any.
0 个评论
采纳的回答
Yoav Livneh
2014-4-8
You can store the data into a vector:
results = [];
while (condition)
code;
r = roots(x);
results(end+1) = r(4);
end
This solution isn't ideal, since the variable results keeps changing size. If you know approximately how many iteration your while loop is going to have you can preallocate the variable. For example, if you never have more than 100 runs:
results = zeros(100,1); % pre allocate
n=0;
while (condition)
code;
r = roots(x);
n = n+1;
results(n) = r(4);
end
if n == 0 % no iterations
results = [];
else % keep only real results
results = results(1:n);
end
After the while loop we only keep the true results.
Hope this helps.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!