# Solving 2 linear equations for 2 unknowns using arrays? - Part II, perhaps with syms?

2 views (last 30 days)
Jesse on 22 Apr 2015
Answered: Nalini Vishnoi on 24 Apr 2015
Greetings all,
On the continuing saga of solving these equations, I am trying to solve the following equation:
sigma_a_instrument1 = sigma1*[1-R_h(Zeta_instrument1)]+sigma2*R_h(Zeta_instrument1);
sigma_a_instrument2 = sigma1*[1-R_h(Zeta_instrument2)]+sigma2*R_h(Zeta_instrument2);
where sigma1 and sigma2 are my unknowns. I tried using syms ans solve as such (I'll explain the code in a second):
sigma1=sym('sigma1');
sigma2=sym('sigma2');
[sigma1 sigma2] = solve('sigma1*[1-R_h_i_EM31]+sigma2*(R_h_i_EM31)=EM31CondMidSwathWest','sigma1*[1-R_h_i_GSSI]+sigma2*(R_h_i_GSSI)=GSSICond10kHzMidSwathWest')
where
R_h_i_EM31=interp1(Response_values_H,R_h,Zeta_EM31);
R_h_i_GSSI=interp1(Response_values_H,R_h,Zeta_GSSICoil);
where the first two arguments are defined as:
Response_values_H= 0:0.01:9;
for n=1:length(Response_values_H)
rValue_H = Response_values_H(n);
if rValue_H < 9.1
R_h(n)=sqrt(4*rValue_H.^2+1)-2*rValue_H;
else
R_h(n)=0;
end
end
and the last argument is defined as:
Zeta_EM31 = Zval/EM31Coil;
Zeta_GSSICoil = Zval/GSSICoil;
where Zval = (0.1:0.1:9)';
EM31CondMidSwathWest and GSSICond10kHzSwathWest are arrays of values (aka sigma_a_intrument1 and sigma_a_instrument2 in the first two equations of this post, respectively).
Error in solve (line 295)
sol = eng.feval('solve', eqns, vars, solveOptions);
So, I've never used the symbolic toolbax (and I do have it installed and looked at the documentation) - but is there a way to give me sigma1 and sigma2 based on inputs that are arrays such as these? Can sigma1 and sigma2 also return arrays?
This is why this is a Part II, because I don't know if my strategy is correct, but I am trying.
Thanks!
-J

Nalini Vishnoi on 24 Apr 2015
Hi,
Looks like you have an over-determined system with 2 unknowns 'sigma1' and 'sigma2' and a number of equations. Simplifying the details, you have equations of the following form:
A1x + B1y = C1
A2x + B2y = C2
where x and y are the unknowns, and A1, A2, B1, B2 and C1, C2 are vectors of known observations.
As I understand, there can be two ways to solve for x and y:
1. You solve x and y as vectors: in that case, you can take one observation from each vector giving you:
a1x + b1y = c1
a2x + b2y = c2
where a1, a2, b1, b2, c1, and c2 are scalar values. If we create a matrix P = [a1 b1; a2 b2] and Q = [c1 c2], the left divide operator '\' solves the system of linear equations using least square errors:
[x, y] = P\Q;
Repeating the above for all 'n' values (assuming the vectors Ai, Bi and Ci are of length 'n'), we can find n values for x and y.
2. You solve only one value of x and one value of y: in that case, let's say the above system of linear equation becomes:
Ax + By = C
where A = [A1; A2], B = [B1; B2] and C = [C1; C2].
P = [A B] would be now 2nx2 matrix where A1 and A2 are each nx1 vector (same with Bi).
Now the solution would be:
[x, y] = P\Q; % Q = C
You can find more information on left divide (\) or mldivide in the following documentation link: http://www.mathworks.com/help/matlab/ref/mldivide.html