Main Content

求解符号方程

此示例说明求解符号方程的基础知识。

求解二次方程

使用 solve 函数求解二次方程。

求解二次方程而不指定要求解的变量。solve 函数选择 x 返回解。

disp('Solve a quadratic equation without specifying which variable to solve for. The solve function chooses x to return a solution.')
disp('>> syms a b c x')
disp('>> eqn = a*x^2 + b*x + c == 0')
disp('>> S = solve(eqn)')
syms a b c x
eqn = a*x^2 + b*x + c == 0
S = solve(eqn)
Solve a quadratic equation without specifying which variable to solve for. The solve function chooses x to return a solution.
>> syms a b c x
>> eqn = a*x^2 + b*x + c == 0
>> S = solve(eqn)
 
eqn =
 
a*x^2 + b*x + c == 0
 
 
S =
 
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
 

指定要求解的变量,并求解 a 的二次方程。

disp('Solve for the variable a')
disp('Sa = solve(eqn,a)')
Sa = solve(eqn,a)
Solve for the variable a
Sa = solve(eqn,a)
 
Sa =
 
-(c + b*x)/x^2
 

求解多元方程并将输出赋给结构体

当求解多个变量时,将输出存储在结构体数组中比存储在各单独变量中更方便。当您指定单个输出参量并且存在多个输出时,solve 函数将返回一个结构体。

求解方程组,以结构体数组形式返回解。

disp('Solve a system of equations to return solutions in a structure array')
disp('>> eqns = [2*u + v == 0, u - v == 1];')
disp('>> S = solve(eqns,[u v])')
syms u v
eqns = [2*u + v == 0, u - v == 1];
S = solve(eqns,[u v])
Solve a system of equations to return solutions in a structure array
>> eqns = [2*u + v == 0, u - v == 1];
>> S = solve(eqns,[u v])

S = 

  struct with fields:

    u: 1/3
    v: -2/3

通过寻址结构体的元素来访问解。

disp('Access the solutions within the structure')
disp('>> S.u')
S.u
disp('>> S.v')
S.v
Access the solutions within the structure
>> S.u
 
ans =
 
1/3
 
>> S.v
 
ans =
 
-2/3
 

使用结构体数组允许您方便地将解代入其他表达式中。使用 subs 函数将解 S 代入其他表达式中。

disp('Use the subs function to substitute the solutions into other expressions')
disp('>> e1 = subs(u^2, S)')
e1 = subs(u^2,S)
disp('>> e2 = subs(3*v + u, S)')
e2 = subs(3*v + u,S)
Use the subs function to substitute the solutions into other expressions
>> e1 = subs(u^2, S)
 
e1 =
 
1/9
 
>> e2 = subs(3*v + u, S)
 
e2 =
 
-5/3
 

如果 solve 函数返回空对象,则不存在任何解。

disp('The solve function returns an empty object if no solutions exist')
disp('>> solve([3*u+2, 3*u+1],u)')
S = solve([3*u+2, 3*u+1],u)
The solve function returns an empty object if no solutions exist
>> solve([3*u+2, 3*u+1],u)
 
S =
 
Empty sym: 0-by-1
 

以数值方式求解方程

solve 函数无法以符号形式求解方程时,它会尝试使用 vpasolve 函数求数值解。vpasolve 函数返回求得的第一个解。

尝试求解以下方程。solve 函数返回一个数值解,因为它找不到符号解。

disp('The following equation returns a numeric solution because the solve function cannot find a symbolic solution')
syms x
disp('>> eqn = sin(x) == x^2 - 1;')
eqn = sin(x) == x^2 - 1;
disp('>> solve(eqn,x)')
S = solve(eqn,x)
The following equation returns a numeric solution because the solve function cannot find a symbolic solution
>> eqn = sin(x) == x^2 - 1;
>> solve(eqn,x)
Warning: Unable to solve symbolically. Returning a numeric solution using <a
href="matlab:web(fullfile(docroot, 'symbolic/vpasolve.html'))">vpasolve</a>. 
 
S =
 
-0.63673265080528201088799090383828
 

绘制方程的左右两侧。注意此方程还有一个正数解。

disp('Plot the left and right sides of the equation to see that the equation also has a positive solution')
disp('>> fplot([lhs(eqn) rhs(eqn)], [-2 2])')
fplot([lhs(eqn) rhs(eqn)], [-2 2])
Plot the left and right sides of the equation to see that the equation also has a positive solution
>> fplot([lhs(eqn) rhs(eqn)], [-2 2])

通过直接调用数值求解器 vpasolve 并指定间隔,求得另一个解。

disp('Find the other solution by calling the numeric solver vpasolve')
disp('>> V = vpasolve (eqn,x,[0,2])')
V = vpasolve(eqn,x,[0 2])
Find the other solution by calling the numeric solver vpasolve
>> V = vpasolve (eqn,x,[0,2])
 
V =
 
1.4096240040025962492355939705895