Home > Software engineering >  solve and store two unknown variable in an equation in for loop matlab
solve and store two unknown variable in an equation in for loop matlab

Time:08-13

I want to calculate the heat flow equation eqns_N = [HANL*(i-Tw1) == kAN(Tw1-Tw2), kAN(Tw1-Tw2) == H1ANL*(Tw2-T2)]; i is the input values from excel and I want to store each variable of Tw1 and Tw2 in an array. Currently I'm getting only one value in 1X1 sym format Below is the code:-

for j = 2:numel(Tsol_N)
    i=Tsol_N(j);
    eqns_N = [H*AN*L*(i-Tw1) == k*AN*(Tw1-Tw2), k*AN*(Tw1-Tw2) == H1*AN*L*(Tw2-T2)];
    S_N= solve(eqns_N, [Tw1 Tw2],'Real',true);
    res = subs(S_N,{Tw1,Tw2});
    %Tw1(i).Tw1 = double(S_N.Tw1);
    %Tw2(i).Tw2 = double(S_N.Tw2);
    overall_cooling_load = H*AN*(S_N.Tw2 - T2);
    rounded_overall_cooling_load = double(overall_cooling_load);
    overall_cooling_load1 = [overall_cooling_load ; Tsol_N];
    rounded_overall_cooling_load2= [rounded_overall_cooling_load ; Tsol_N];

end

CodePudding user response:

The way to do this is to use j as an index in the loop.

for j = 2:numel(Tsol_N)
   ...
   eqns_N(j-1) = ...
   S_N(j-1) = ...
   res(j-1) = subs(S_N(j-1), {Tw1, Tw2});

and so on. That way you'll have the first result store in res(1), the second in res(2).

Remember to pre-allocate memory for the matrices in order to avoid growing matrices.

If the outputs are not scalars then I suggest you use cells:

for j = 2:numel(Tsol_N)
   ...
   eqns_N{j-1} = ...
   S_N{j-1} = ...
   res{j-1} = subs(S_N{j-1}, {Tw1, Tw2});
  • Related