简体   繁体   中英

Solving a linear system of equation with two variables in MATLAB

It might seem a simple question. I need it, though. Let's assume we have two equations:

2 * y + x + 1 = 0

and

y - 2 * x = 0

I would like to find their bisection which can be calculated from this equation:

  |x + 2 * y + 1|        |-2 *x +  y |
------------------- = ----------------- 
(sqrt(2^2 + 1^2))      (sqrt(1^2 + 2^2))

To make the long story short, we only need to solve this below system of equation:

2 * y + x + 1 = -2 *x +  y  
and
2 * y + x + 1 =  2 *x -  y 

However, using solve function of MATLAB:

syms x y
eqn1   = 2 * y + x + 1 == -2 *x +  y ;
eqn2   = 2 * y + x + 1 ==  2 *x -  y ;
[x, y] = solve (eqn1 , eqn2, x, y)   ;

Will give me:

x = -1/5 and y = -2/5

But, I am looking for the result equations, which is:

y = -3 * x - 1 and 3 * y = 2 * x - 1

So, does anyone know how I can get the above line equation instead of the result point? Thanks,

The following should solve both equations with y on the left-hand-side:

y1 = solve(eqn1,y)
y2 = solve(eqn2,y)

Result:

y1 =

- 3*x - 1

y2 =

x/3 - 1/3

As an aside, it would be much faster to solve this system by thinking of it it as a matrix inversion problem Ax=b rather than using MATLAB's symbolic tools:

A = [1 2; -2 1];
b = [-1; 0];
x = A\b

Result:

x =

   -0.2000
   -0.4000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM