The "solve" and "dsolve" functions seek analytic solutions to algebraic and ordinary differential equations.
The first example is a simple quadratic. You can either find the zeros of a symbolic expression, without quotes:
syms a b c x x = solve(a*x^2 + b*x + c);
Or, you can find the roots of an equation, given in quotes:
x = solve('a*x^2 + b*x + c = 0');
Both of these produce the same result:
x
x = 1/2/a*(-b+(b^2-4*a*c)^(1/2)) 1/2/a*(-b-(b^2-4*a*c)^(1/2))
The solution to a general cubic is:
x = solve('a*x^3 + b*x^2 + c*x + d')
pretty(x)
x =
1/6/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-2/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-1/3*b/a
-1/12/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+1/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-1/3*b/a+1/2*i*3^(1/2)*(1/6/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+2/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3))
-1/12/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+1/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-1/3*b/a-1/2*i*3^(1/2)*(1/6/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+2/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3))
[ 1/3 2 ]
[ %1 3 a c - b ]
[1/6 ----- - 2/3 ---------- - 1/3 b/a]
[ a 1/3 ]
[ a %1 ]
[ 1/3 2
[ %1 3 a c - b
[- 1/12 ----- + 1/3 ---------- - 1/3 b/a
[ a 1/3
[ a %1
/ 1/3 2\]
1/2 | %1 3 a c - b |]
+ 1/2 I 3 |1/6 ----- + 2/3 ----------|]
| a 1/3 |]
\ a %1 /]
[ 1/3 2
[ %1 3 a c - b
[- 1/12 ----- + 1/3 ---------- - 1/3 b/a
[ a 1/3
[ a %1
/ 1/3 2\]
1/2 | %1 3 a c - b |]
- 1/2 I 3 |1/6 ----- + 2/3 ----------|]
| a 1/3 |]
\ a %1 /]
2 3
%1 := 36 b c a - 108 d a - 8 b
1/2 3 2 2 2 2 3 1/2
+ 12 3 (4 a c - c b - 18 b c a d + 27 d a + 4 d b ) a
The statement
x = solve('p*sin(x) = r');
chooses 'x' as the unknown and returns
x
x = asin(r/p)
A system of two quadratic equations in two unknowns produces solution vectors.
[x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0')
x =
1
3
y =
1
-3/2
The solution can also be returned in a structure.
S = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0') S.x S.y
S =
x: [2x1 sym]
y: [2x1 sym]
ans =
1
3
ans =
1
-3/2
The next example regards 'a' as a parameter and solves two equations for u and v.
[u,v] = solve('a*u^2 + v^2 = 0','u - v = 1')
u = 1/2/(a+1)*(-2*a+2*(-a)^(1/2))+1 1/2/(a+1)*(-2*a-2*(-a)^(1/2))+1 v = 1/2/(a+1)*(-2*a+2*(-a)^(1/2)) 1/2/(a+1)*(-2*a-2*(-a)^(1/2))
Add a third equation and solve for all three unknowns.
[a,u,v] = solve('a*u^2 + v^2','u - v = 1','a^2 - 5*a + 6')
a = 2 2 3 3 u = 1/3+1/3*i*2^(1/2) 1/3-1/3*i*2^(1/2) 1/4+1/4*i*3^(1/2) 1/4-1/4*i*3^(1/2) v = -2/3+1/3*i*2^(1/2) -2/3-1/3*i*2^(1/2) -3/4+1/4*i*3^(1/2) -3/4-1/4*i*3^(1/2)
If an analytic solution cannot be found, "solve" returns a numeric solution.
digits(32) [x,y] = solve('sin(x+y)-exp(x)*y = 0','x^2-y = 2')
x = -.33129879499763797066864098166363 -.66870120500236202933135901833637 y = -1.8902411084331130499424622177919 -1.5528386984283889912797441811191
Similar notation, with "D" denoting differentiation, is used for for ordinary differential equations by the "dsolve" function.
y = dsolve('Dy = -a*y')
y = C1*exp(-a*t)
Specify an initial condition.
y = dsolve('Dy = -a*y','y(0) = 1')
y = exp(-a*t)
The second derivative is denoted by "D2'.
y = dsolve('D2y = -a^2*y', 'y(0) = 1, Dy(pi/a) = 0')
y = cos(a*t)
A nonlinear equation produces two solutions in a vector.
y = dsolve('(Dy)^2 + y^2 = 1','y(0) = 0')
y = -sin(t) sin(t)