Search code examples
matlabequationsymbolic-mathequation-solving

Solving a system with two equations and two variables in a for loop


I would like to solve a system with two equations and two variables.
Tau(i)and Roh(i) are input arrays.

Tau=[0.91411 0.91433 0.91389 0.91399 0.91511 0.915]
Roh=[0.07941 0.07942 0.07952 0.07946 0.07951 0.07947]

I would like to calculate R(i)and t(i)for each istep (for loop). I will be glad to have help to solve the equations. enter image description here

Tau(i)-((1-R(i))^2*t(i))/(1-R(i)^2*t(i)^2)==0
Roh(i)-R(i)-((1-R(i))^2*R(i)*t(i)^2)/(1-R(i)^2*t(i)^2)==0

I have tried the following script but I have difficulty to write the proper code to export the data. I get only "sym" which is not a value.

function [R,t] = glassair(Tau, Roh)

for i=1:6
    syms R(i) t(i)
     eq1(i)=sym('Tau(i)-((1-R(i))^2*t(i))/(1-R(i)^2*t(i)^2)');
     eq2(i)=sym('Roh(i)-R(i)-((1-R(i))^2*R(i)*t(i)^2)/(1-R(i)^2*t(i)^2)');
     sol(i)=solve(eq1(i),R(i), eq2(i),t(i));

end
end

Solution

  • There where multiple issues with your code.

    • Using the R(i) syntax with variables, you mixed in symbolic functions. I think here you only have variables. Same with eq(i), this created a symbolic function, not a list of your equations (as you probably intended)
    • You called solve with the wrong order of arguments
    • You called solve with the wrong order of arguments
    • Passing a string to sym your known constants Tau and Roh where not substituted, you ended up with 4 unknowns in your equations

    .

    Tau=[0.91411 0.91433 0.91389 0.91399 0.91511 0.915]
    Roh=[0.07941 0.07942 0.07952 0.07946 0.07951 0.07947]
    
    syms R t
    for i=1:6
    eq1=Tau(i)-((1-R)^2*t)/(1-R^2*t^2);
    eq2=Roh(i)-R-((1-R)^2*R*t^2)/(1-R^2*t^2);
    sol=solve([eq1,eq2]);
    allsol(i).R=double(sol.R);
    allsol(i).t=double(sol.t);
    end