主要内容

Solve Algebraic Equation

符号数学工具箱™ offers both symbolic and numeric equation solvers. This topic shows you how to solve an equation symbolically using the symbolic solversolve。To compare symbolic and numeric solvers, seeSelect Numeric or Symbolic Solver

Solve an Equation

Ifeqnis an equation,solve(eqn, x)solveseqn对于符号变量x

Use the==operator to specify the familiar quadratic equation and solve it usingsolve

syms a b c x eqn = a*x^2 + b*x + c == 0; solx = solve(eqn, x)
solx =  - (b +(b^2-4*a*a*c)^(1/2))/(2*a) - (b  - (b^2-4*a*a*c)^(1/2)/(2*a)

solx是包含二次方程的两个溶液的符号向量。万博 尤文图斯如果输入eqnis an expression and not an equation,solve求解方程eqn == 0

To solve for a variable other thanx, specify that variable instead. For example, solveeqnforb

solb = solve(eqn,b)
solb = -(a*x^2 + c)/x

If you do not specify a variable,solve用途symvar选择要解决的变量。例如,solve(eqn)solveseqnforx

将完整解决方案返回到方程式

solvedoes not automatically return all solutions of an equation. Solve the equationcos(x)== -sin(x)。这solvefunction returns one of many solutions.

syms x solx = solve(cos(x)== -sin(x),x)
solx = -pi/4

To return all solutions along with the parameters in the solution and the conditions on the solution, set theReturnConditions选项真的。求解相同的方程式以进行完整解决方案。提供三个输出变量:用于解决方案x,对于解决方案中的参数以及解决方案的条件。

syms x [solx,param,cond] = solve(cos(x)== -sin(x),x,'returnconditions',true)
solx = pi*k - pi/4 param = k cond = in(k, 'integer')

solxcontains the solution forx, which ispi*k - pi/4。这paramvariable specifies the parameter in the solution, which isk。这condvariable specifies the condition在(k,'整数')中上the solution, which meanskmust be an integer. Thus,solvereturns a periodic solution starting atpi/4which repeats at intervals ofpi*k, wherekis an integer.

使用完整解决方案,参数和条件

You can use the solutions, parameters, and conditions returned bysolve在间隔或其他条件万博 尤文图斯下找到解决方案。

To find values ofxin the interval-2*pi, 解决solxforkwithin that interval under the conditioncond。假设条件condusingassume

assume(cond) solk = solve(-2*pi
               
solk = -1 0 1 2

To find values ofx对应于这些值k, 利用潜艇to substitute forkinsolx

xvalues = subs(solx, solk)
xvalues = -(5*pi)/4 -pi/4 (3*pi)/4 (7*pi)/4

To convert these symbolic values into numeric values for use in numeric calculations, usevpa

xvalues = vpa(xvalues)
xvalues = -3.9269908169872415480783042290994 -0.78539816339744830961566084581988 2.3561944901923449288469825374596 5.4977871437821381673096259207391

Visualize and Plot Solutions Returned by solve

使用的前面部分solveto solve the equationcos(x)== -sin(x)。这solution to this equation can be visualized using plotting functions such asfplotscatter

Plot both sides of equationcos(x)== -sin(x)

fplot(cos(x))保持网格fplot(-sin(x)) title('Both sides of equation cos(x) = -sin(x)') legend('cos(x)','-sin(x)','地点','best','自动更新','off')

Figure contains an axes object. The axes object with title Both sides of equation cos(x) = -sin(x) contains 2 objects of type functionline. These objects represent cos(x), -sin(x).

Calculate the values of the functions at the values ofx, and superimpose the solutions as points usingscatter

yvalues = cos(xvalues)
yvalues =

( - 0.70710678118654752440084436210485 0.70710678118654752440084436210485 - 0.70710678118654752440084436210485 0.70710678118654752440084436210485 )

scatter(xvalues, yvalues)

Figure contains an axes object. The axes object with title Both sides of equation cos(x) = -sin(x) contains 3 objects of type functionline, scatter. These objects represent cos(x), -sin(x).

As expected, the solutions appear at the intersection of the two plots.

Simplify Complicated Results and Improve Performance

If results look complicated,solveis stuck, or if you want to improve performance, see,Troubleshoot Equation Solutions from solve Function