8. Symbolic Toolbox
Solving
You have seen that fzero numerically finds where a function is zero in MATLAB. “Maple” uses the solve facility which can solve n simultaneous algebraic or transcendental equations for n unknowns. It firstly attempts to find an exact analytic solution. If this is not possible, it then attempts to find a numeric solution in variable precision format.
The command is of the form [a1,a2,...,an]=solve(f1,f2,...,fn,v1,v2,...,vn).
Here, f1,f2,...,fn are symbolic expressions that are to be made zero, v1,v2,...,vn are the variables in alphabetical order to be solved for, and a1,a2,...,an are the corresponding answers.
Solving a single equation
To solve a single equation f(x) = 0, this reduces to a=solve(f,x).
Of course there might be more than one solution for a. What happens if you do not specify the variable for which the equation is to be solved? If there is only one symbolic variable in the expression, it will solve for it by default. Otherwise x is the default variable. If there are two or more symbolic variables, none of which is x, and you forget to specify the one for which the solution is required, it appears to choose the last alphabetical one.
Example 1: Solve the equation
10/x2 + 1 = 4− x. This is equivalent to solving 10/x2 + 1 − 4 + x = 0. There are various ways:
clear all
syms x
f=10/(x^2+1)-4+x; % f is now a symbolic expression
soln=solve(f,x) % note the three solutions
or, knowing that x is the default variable,
clear all
syms x
soln=solve(10/(x^2+1)-4+x) % no need to use f or specify x
In earlier versions of MATLAB, you could use
clear all
soln=solve(’10/(x^2+1)=4-x’)
but this is being phased out.
Exercise: Change to 10/(x^2+1)+2+x=0 and solve again.
Example 2: Solve the general quadratic equation ax2 + bx + c = 0 for x.
clear all
syms a b c x
soln=solve(a*x^2+b*x+c)
giving the output soln=[1/2/a*(-b+(b^2-4*a*c)^(1/2))]
[1/2/a*(-b-(b^2-4*a*c)^(1/2))]
However, if for some reason you wanted to solve the same equation for b, you would need
clear all
syms a b c x
soln=solve(a*x^2+b*x+c,b)
giving the ouput soln=-(a*x^2+c)/x.
Example 3: We know sin θ = 0.5 has an infinite number of solutions. Enter
clear all
syms theta
angle=solve(sin(theta)-0.5)
giving the output angle=1/6*pi.
Example 4: Unlike Example 3, the equation sin θ = 0.5 − cos θ, −π ≤ θ ≤ π, does not have obvious solutions. But “Maple” can find two solutions exactly. The answers are hardly in a form that you would use and so it is better to convert them into numeric values.
clear all
syms theta
angle=solve(sin(theta)-0.5+cos(theta)) % hardly usable
angle=double(angle) % looks better in MATLAB
giving the output angle=[atan((1/4-1/4*7^(1/2))/(1/4+1/4*7^(1/2)))]
[atan((1/4+1/4*7^(1/2))/(1/4-1/4*7^(1/2)))+pi]
angle=-0.4240
1.9948
Example 5: By means of a simple sketch you can see that ex = 4− x2 has two solutions.
However, there is no analytic way to find them. Enter
clear all
syms x
soln=solve(exp(x)-4+x^2)
giving the output soln=1.0580064010906363086213874461232. This indicates that a numerical solution process was needed in “Maple”, and the answer is in variable precision format. If you wish, you can follow with soln=double(soln). Note that only the solution nearest to zero was found.
Solving simultaneous equations
Find the points of intersection of the two circles 2x2 − x + 2y2 − 8y = 0 and x2 + 2x + y2 − 6y + 1 = 0.
clear all % file available in M-files folder as file7.m
syms x y
eqn1=2*x^2-x+2*y^2-8*y;
eqn2=x^2+2*x+y^2-6*y+1;
[X,Y]=solve(eqn1,eqn2)
giving the output X = [ 2] Y = [ 3]
[ -14/41] [ 3/41]
which implies the two points (x, y) = (2, 3) and (x, y) = (−14/41, 3/41).