matlab symbolic solve system of linear equations in terms of specific variable

1k Views Asked by At

I would like to solve the following system of equation in terms of g3 enter image description here

The following code returns one possible solution.

syms g0 g1 g2 g3 x mu3 mu4 mu5 mu6 gamma
A = [1, 0, 1, mu3; 0, 1, mu3, mu4; 1, mu3, mu4, mu5];
B = [0; 0; gamma];
X = linsolve(A,B);

But I would like to have a solution for g0, g1 and g2 in terms of g3.

How can I specify that?

3

There are 3 best solutions below

0
On BEST ANSWER

Transform your problem into the following equivalent formulation under the form of a square system :

$$\begin{pmatrix}1&0&1\\0&1&\mu_3\\1&\mu_3&\mu_4\end{pmatrix}\begin{pmatrix}g_0\\g_1\\g_2\end{pmatrix}=\begin{pmatrix}-\mu_3g_3\\-\mu_4g_3\\\gamma-\mu_5g_3\\\end{pmatrix}$$

giving the following Matlab formulation:

syms g0 g1 g2 g3 x mu3 mu4 mu5 mu6 gamma
A = [1, 0, 1; 0, 1, mu3; 1, mu3, mu4];
B = [ -mu3*g3; -mu4*g3; gamma-mu5*g3];
X = linsolve(A,B)

giving:

g0 = (- g3*mu3^3 + 2*g3*mu4*mu3 + gamma - g3*mu5)/D
g1 = (gamma*mu3 - g3*mu4 + g3*mu3^2 + g3*mu4^2 - g3*mu3*mu5)/D
g2 = -(gamma + g3*mu3 - g3*mu5 + g3*mu3*mu4)/D
with D:=(mu3^2 - mu4 + 1)
0
On

Append a fourth row to $A$, with values $(0,0,0,1)$.

Also add a fourth element to $B$, with value the desired $g_3$.

Then when you solve $$ Ax = B $$ the solution will give you $g_0, g_1, g_2$ read off from the first three elements of $x$, and you will notice that in the solution, the last element of $x$ is $g_3$.

Why does that work? Because the fourth equation you have added is $x_3=g_3$.

1
On

It also works if solve instead of linsolve is employed

syms g0 g1 g2 g3 x mu3 mu4 mu5 mu6 gamma

eqn1 = g0 + g2 + g3*mu3 == 0;
eqn2 = g1 + g2*mu3 + g3*mu4 ==0;
eqn3 = g0 + g1*mu3 + g2*mu4 + g3*mu5 == gamma;

g = solve([eqn1, eqn2, eqn3], [g0, g1, g2]);