1

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
Daniel
  • 36,610
  • 3
  • 36
  • 69
Hamed
  • 193
  • 2
  • 9

2 Answers2

2

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
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Thank you very much dear Daniel for help and corrections. It works very well. The only challenge was the order of the equations which produces 2 answers but I managed to divide them. – Hamed Mar 14 '16 at 08:48
1

You just need to define the functions once, then use a for loop to get the values.

function [R_out,t_out] = glassair(Tau_in, Roh_in)

syms R t Tau Roh
eq1 = Tau-((1-R)^2*t)/(1-R^2*t^2);
eq2 = Roh-R-((1-R)^2*R*t^2)/(1-R^2*t^2);

R_out = zeros(1,6); % Given it will be always 6
t_out = zeros(1,6);

for i=1:6
    Tau = Tau_in(i);
    Roh = Roh_in(i);
    sol = solve( subs( [eq1;eq2] ) );
    R_out(i) = double(sol.R);
    t_out(i) = double(sol.t);
end

end

Matlab is very smart in that defines the types for you. When you solve the equations it detects which variables are needed. The zero allocation is for speed up.

Luis
  • 127
  • 1
  • 11