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
Ifeqn
is 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
是包含二次方程的两个溶液的符号向量。万博 尤文图斯如果输入eqn
is an expression and not an equation,solve
求解方程eqn == 0
。
To solve for a variable other thanx
, specify that variable instead. For example, solveeqn
forb
。
solb = solve(eqn,b)
solb = -(a*x^2 + c)/x
If you do not specify a variable,solve
用途symvar
选择要解决的变量。例如,solve(eqn)
solveseqn
forx
。
将完整解决方案返回到方程式
solve
does not automatically return all solutions of an equation. Solve the equationcos(x)== -sin(x)
。这solve
function 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')
solx
contains the solution forx
, which ispi*k - pi/4
。这param
variable specifies the parameter in the solution, which isk
。这cond
variable specifies the condition在(k,'整数')中
上the solution, which meansk
must be an integer. Thus,solve
returns a periodic solution starting atpi/4
which repeats at intervals ofpi*k
, wherek
is an integer.
使用完整解决方案,参数和条件
You can use the solutions, parameters, and conditions returned bysolve
在间隔或其他条件万博 尤文图斯下找到解决方案。
To find values ofx
in the interval-2*pi
solx
fork
within that interval under the conditioncond
。假设条件cond
usingassume
。
assume(cond) solk = solve(-2*pi
solk = -1 0 1 2
To find values ofx
对应于这些值k
, 利用潜艇
to substitute fork
insolx
。
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
使用的前面部分solve
to solve the equationcos(x)== -sin(x)
。这solution to this equation can be visualized using plotting functions such asfplot
和scatter
。
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')
Calculate the values of the functions at the values ofx
, and superimpose the solutions as points usingscatter
。
yvalues = cos(xvalues)
yvalues =
scatter(xvalues, yvalues)
As expected, the solutions appear at the intersection of the two plots.
Simplify Complicated Results and Improve Performance
If results look complicated,solve
is stuck, or if you want to improve performance, see,Troubleshoot Equation Solutions from solve Function。