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

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
Ameer Hamza 2020 年 5 月 22 日
編集済み: Ameer Hamza 2020 年 5 月 22 日

0 投票

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

2 件のコメント

noMathWiz
noMathWiz 2020 年 5 月 22 日
Thank you!
Ameer Hamza
Ameer Hamza 2020 年 5 月 22 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 5 月 23 日
編集済み: Stephen23 2020 年 5 月 23 日

1 投票

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

5 件のコメント

Ameer Hamza
Ameer Hamza 2020 年 5 月 23 日
This solution will be much more efficient, but if the system of equation is complicated than such simplification might not be possible by hand. In that case, solve() can be called once.
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
M = [ 1 2; 3 4]; % 2x2 matrix of constants
C = [y0; a0]; % [y0;a0]
eq = A == M*C;
sol = solve(eq, [a0 af]);
sol = struct2array(sol);
sol_fun = matlabFunction(sol, 'Vars', [y0 yf]);
yf_num = (5e-3)/2; % constant
y0_num = (34e-3)/2; % constant
sol_fun(y0_num, yf_num)
Stephen23
Stephen23 2020 年 5 月 23 日
編集済み: Stephen23 2020 年 5 月 23 日
"In that case, solve() can be called once" for each matrix M, so this is still not a general solution.
Ameer Hamza
Ameer Hamza 2020 年 5 月 23 日
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)
Stephen23
Stephen23 2020 年 5 月 24 日
@Ameer Hamza: could you do some timing comparisons?: your general solution vs. what I showed in my answer.
Ameer Hamza
Ameer Hamza 2020 年 5 月 24 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2020 年 5 月 22 日

コメント済み:

2020 年 5 月 24 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by