# Solving linear system 2 equations, 2 unknowns, not A = b*C form

2 views (last 30 days)
noMathWiz on 22 May 2020
Commented: Ameer Hamza on 24 May 2020
N00b questions: I would like to solve this for a linear system of two equations with two unknowns, per below: I started with the below code, but the linsolve documentation is confusing to me. Can somebody help?
syms af a0 % af and a0 are the two unknowns I need to solve for
yf = (5e-3)/2; %constant
y0 = (34e-3)/2; %constant
A = [yf; af]; % 1x2 matrix with one constant and one unknown
M = [ 1 2; 3 4]; % 2x2 matrix of constants
C = [y0; a0]% [y0;a0]
% ... ?? HELP!

Ameer Hamza on 22 May 2020
Edited: Ameer Hamza on 22 May 2020
syms af a0 % af and a0 are the two unknowns I need to solve for
yf = (5e-3)/2; %constant
y0 = (34e-3)/2; %constant
A = [yf; af]; % 1x2 matrix with one constant and one unknown
M = [ 1 2; 3 4]; % 2x2 matrix of constants
C = [y0; a0];% [y0;a0]
eq = A == M*C;
sol = solve(eq, [af a0]);
Result
>> sol.a0
ans =
-29/4000
>> sol.af
ans =
11/500
To get result in floating-point format:
>> double(sol.a0)
ans =
-0.0073
>> double(sol.af)
ans =
0.0220

noMathWiz on 22 May 2020
Thank you!
Ameer Hamza on 22 May 2020
I am glad to be of help!

Stephen Cobeldick on 23 May 2020
Edited: Stephen Cobeldick on 23 May 2020
Using a numeric solver or the symbolic toolbox is like using a sledge-hammer to crack open a tiny walnut!
It is very simple to rearrange those equations and get the actual algebraic solution, which works for any data set (as opposed to using a numeric solver or Ameer Hamza's method, which you will need to run for every set of data).
First, using the basic definition of matrix multiplication we can write out your equations in full:
yf = M11*y0 + M12*a0
af = M21*y0 + M22*a0
We note that the first equation can be rearranged to give a0 entirely in terms of known values:
a0 = (yf - M11*y0) / M12
which we can then simply substitute into the second equation to get that last unknown af.
Lets try it with some numbers:
>> M = [1,2;3,4];
>> yf = (5e-3)/2;
>> y0 = (34e-3)/2;
>> a0 = (yf - M(1,1)*y0) / M(1,2)
a0 = -0.0072500
>> af = M(2,1)*y0 + M(2,2)*a0
af = 0.022000
So we get exactly the same output as Ameer Hamza's answer shows, just much more efficiently. And because this is the actual algebraic solution this works for any values that you have, there is no need to run inefficient solvers again and again and again and again...
"Computers are useless. They can only give you answers." Pablo Picasso

Ameer Hamza on 23 May 2020
It is possible to call solve() once even for that case
syms af a0 yf y0 % af and a0 are the two unknowns I need to solve for
A = [yf; af]; % 1x2 matrix with one constant and one unknown
C = [y0; a0]; % [y0;a0]
M = sym('M', [2,2]);
eq = A == M*C;
sol = solve(eq, [a0 af]);
sol = struct2array(sol);
sol_fun = matlabFunction(sol, 'Vars', {y0 yf M});
yf_num = (5e-3)/2; % constant
y0_num = (34e-3)/2; % constant
M_num1 = [1 2; 3 4]; % 2x2 matrix of constants
sol_fun(y0_num, yf_num, M_num1)
M_num2 = [2 1; 7 8]; % 2x2 matrix of constants
sol_fun(y0_num, yf_num, M_num2)
Stephen Cobeldick on 24 May 2020
@Ameer Hamza: could you do some timing comparisons?: your general solution vs. what I showed in my answer.
Ameer Hamza on 24 May 2020
It seems that the solve() version is faster. However, both versions can be made equally faster.
Version 1
M = [1,2;3,4];
yf = (5e-3)/2;
y0 = (34e-3)/2;
t1 = timeit(@() sol_fun(M, y0, yf))
function sol = sol_fun(M, y0, yf)
sol(1) = (yf - M(1,1)*y0) / M(1,2);
sol(2) = M(2,1)*y0 + M(2,2)*sol(1);
end
Result:
t1 =
3.4226e-06
Version 2
syms af a0 yf y0 % af and a0 are the two unknowns I need to solve for
A = [yf; af]; % 1x2 matrix with one constant and one unknown
C = [y0; a0]; % [y0;a0]
M = sym('M', [2,2]);
eq = A == M*C;
sol = solve(eq, [a0 af]);
sol = struct2array(sol);
sol_fun = matlabFunction(sol, 'Vars', {y0 yf M});
yf_num = (5e-3)/2; % constant
y0_num = (34e-3)/2; % constant
M_num = [1 2; 3 4]; % 2x2 matrix of constants
t2 = timeit(@() sol_fun(yf_num, y0_num, M_num))
Result
Warning: The measured time for F may be inaccurate because it is running too
fast. Try measuring something that takes longer.
> In timeit (line 158)
In test (line 14)
t2 =
1.4806e-07
Version 1 can also be made this much faster by defining the function like this
function sol = sol_fun(M, y0, yf)
sol = [(yf - M(1,1)*y0) / M(1,2); M(2,1)*y0 + M(2,2)*((yf - M(1,1)*y0) / M(1,2))];
end
However, version 1 timing does not consider the time to write these equations on a piece of paper, simplify and write all the dependent in terms of independent terms.