How to keep the values from every loop that solves a system in a single matrix?

clear all for drog=1:60; syms cg1 cg2 cp1 cp2 x1 eqn1=cg1+cp1==0.05*drog; eqn2=cg2+cp2==0.15*drog; eqn3=cg1==(132.24/(0.082*298))*x1; eqn4=cg2==(132.24/(0.082*298))*(1-x1); eqn5=x1==(cp1/132.24)/((cp1/132.24)+(cp2/132.24)); s=solve(eqn1, eqn2, eqn3, eqn4, eqn5, cg1, cg2, cp1, cp2, x1) a=s.cp1; end
In every loop i want to keep the solution cp1 in a matrix, so that at the end of all the loops i will have the results in a single matrix. How can i do that? can you please help me???

 Accepted Answer

Add an index to the badly-named "a":
a(drog, :) = s.cp1;
Make sure you reallocate a in advance of the for loop with zeros and the number of columns s.cpl will have
a = zeros(60, numColumns); % s.cp1 = numel(s.cp1).
Also read this link.

More Answers (1)

syms cg1 cg2 cp1 cp2 x1 drog
eqn1=cg1+cp1==0.05*drog;
eqn2=cg2+cp2==0.15*drog;
eqn3=cg1==(132.24/(0.082*298))*x1;
eqn4=cg2==(132.24/(0.082*298))*(1-x1);
eqn5=x1==(cp1/132.24)/((cp1/132.24)+(cp2/132.24));
s=solve(eqn1, eqn2, eqn3, eqn4, eqn5, cg1, cg2, cp1, cp2, x1);
a=matlabFunction(s.cp1);
drog = (1:60)';
out = a(drog)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!