Solving Symbolic Equations Using MATLAB

In mathematical calculations, we often need to solve equations or equations. Solving equations is an important knowledge point in mathematics. The solve function is provided in MATLAB for solving symbolic expressions. There are many ways to call the solve function, including the following:

  • solve(equ): Solve the equation equ, and the variables to be solved are the system defaults.
  • solve(equ,var): Solve the equation equ, where the variable to be solved is var.
  • solve(eqn1,eqn2,eqn3,...,eqnM,var1,var2,var3,...varN): Solve the equation system, which is composed of N variables and M equations.

The following are the solutions and verifications for the above types respectively.

(1)solve(equ)

solve(equ) is to solve the equation equ, and the variables to be solved are the default values ​​of the system.

For example, use the solve function to solve the following symbolic expressions:

(x+2)/3=6

\frac{1}{x}+2=5

\frac{1}{x}=\frac{1}{a}+\frac{1}{b}

The MATLAB code looks like this:

syms x a b
solve((x+2)/3==6)
solve(1/x+2==5)
solve(1/x==1/a+1/b)

The running results are as follows:

ans =
    16
ans =
    1/3
ans = 
    1/(1/a + 1/b)

It can be seen from the above running results that among the symbolic variables x, a, and b, the system uses x as the variable by default. When there is only one variable in the symbolic expression, MATLAB can accurately solve its value; when there are multiple variables, the system selects the default variable and can also solve it.

(2)solve(equ,var)

When the calling method of the function is solve(equ,var), MATLAB will solve the specific variable var of the equation equ.

For example, use solve to solve the following symbolic expressions:

ax+b=2c

4a+b+c=10

ax+y=4

The MATLAB code looks like this:

syms a b c x y
ans1=solve(a*x+b==2*c,x)
ans2=solve(4*a+b+c==10,a)
ans3=solve(a*x+y==4,a)

The running results are as follows:

ans1 =
    -(b - 2*c)/a
ans2 =
    5/2 - c/4 - b/4
ans3 =
    -(y - 4)/x

It can be seen from the above running results that when the solve function specifies the priority of a specific variable will be higher than the priority of the system default variable.

(3)solve(eqn1,eqn2,eqn3,...,eqnM,var1,var2,var3,...varN)

When the calling method of the function is solve(eqn1, eqn2, eqn3,...,eqnM, var1, var2, var3,...varN), it is possible to solve a specific function equation system, in which the above calling method expresses There are N variables and a system of equations consisting of M equations.

For example, use solve to solve the following groups of symbolic expressions:

\left\{\begin{matrix} 2x+y=9 \\ x-y=3 \end{matrix}\right.

\left\{\begin{matrix} x+y+z=20 \\ x+2y+3z=35 \\ x+2y-z=16 \end{matrix}\right.

\left\{\begin{matrix} x^2+y^2=13 \\ x+y-z=0 \\ x+z=8 \end{matrix}\right.

\left\{\begin{matrix} x+y=2a \\ x-y=a+4 \end{matrix}\right.

The MATLAB code looks like this:

syms x y z a
[x1,y1]=solve(2*x+y==9,x-y==3,x,y)
[x2,y2,z2]=solve(x+y+z==20,x+2*y+3*z==45,x+2*y-z==9,x,y,z)
[x3,y3,z3]=solve(x^2+y^2==13,x+y-z==0,x+z==8,x,y,z)
[x4,y4]=solve(x+y==2*a,x-y==a+4,x,y)

The running results are as follows:

x1 =
    4
y1 =
    1
x2 =
    4
y2 =
    7
z2 =
    9
x3 =
       3
    17/5
y3 =
      2
    6/5
z3 =
       5
    23/5
x4 =
    (3*a)/2 + 2
y4 =
    a/2 - 2

It can be seen from the above formula that this calling method can solve the equation system, and when there are multiple solutions to the functional equation, MATLAB will calculate multiple solutions.

Guess you like

Origin blog.csdn.net/qq_54186956/article/details/128260609