Equations and Systems
MATLAB offers certain commands that allow you to solve equations and systems. Among them are the following:
5-1. Special Commands
In addition, equations and systems can be solved using the following commands (all of them must be preceded by the maple command):
Here are some examples. First, we solve an equation in exact and approximate form and check one of the solutions.
>> pretty(sym(maple('eq := x^4-5*x^2+6*x=2: solve(eq,x)')))
1/2 1/2
-1 + 3 , -1 - 3 , 1, 1
>> pretty(sym(maple('sols := [solve(eq,x)] : evalf(sols,10)')))
[.732050808 -2.732050808 1. 1.]
>> pretty(simple(sym(maple('subs(x=sols[1],eq )'))))
2 = 2
The previous equation also can be solved as follows:
>> solve('x^4-5*x^2+6*x=2')
ans =
[- 1 + 3 ^(1/2)]
[^(1/2) - 1-3]
[ 1]
[ 1]
Another way to solve the same equation would be as follows:
>> syms x
>> solve(x^4-5*x^2+6*x-2)
ans =
[- 1 + 3 ^(1/2)]
[^(1/2) - 1-3]
[ 1]
[ 1]
Next we solve a system and check its solutions.
>> maple('eqns:= {u+v+w=1, 3*u+v=3, u-2*v-w=0}:sols:= solve(eqns)')
ans =
sols := {w = -2/5, v = 3/5, u = 4/5}
>> maple('subs(sols,eqns)')
ans =
{1 = 1, 0 = 0, 3 = 3}
The previous system can also be solved in the following way:
>> syms u v w
>> [u,v,w] = solve(u+v+w-1, 3*u+v-3, u-2*v-w, u, v, w)
u =
4/5
v =
3/5
w =
-2/5
The same system can also be solved in another way:
>> [u,v,w] = solve('u+v+w=1', '3*u+v=3', 'u-2*v-w=0', 'u','v','w')
u =
4/5
v =
3/5
w =
-2/5
Finally, we can solve the system in the following way:
>> [u,v,w] = solve('u+v+w=1, 3*u+v=3, u-2*v-w=0', 'u,v,w')
u =
4/5
v =
3/5
w =
-2/5
Next we solve some systems, subject to certain conditions.
>> pretty(sym(maple('solve({x^2*y^2=0, x-y=1})')))
{x = 0, y = - 1}, {x = 0, y = - 1}, {x = 1, y = 0}, {x = 1, y = 0}
>> pretty(sym(maple('solve({x^2*y^2=0, x-y=1, x<>0})')))
{x = 1, y = 0}, {x = 1, y = 0}
>> pretty(sym(maple('solve({x^2*y^2-b, x^2-y^2-a},{x,y})')))
>> pretty(sym(maple('solve({x^2*y^2-b, x^2-y^2-a},{x,y})')))
{y = 1/2 %4, x = 1/2 %3}, {y = 1/2 %4, x = - 1/2 %3},
{y = - 1/2 %4, x = 1/2 %3}, {y = - 1/2 %4, x = - 1/2 %3},
{y = 1/2 %1, x = 1/2 %2}, {x = - 1/2 %2, y = 1/2 %1},
{y = - 1/2 %1, x = 1/2 %2}, {x = - 1/2 %2, y = - 1/2 %1}
2 1/2 1/2
%1 := (-2 a - 2 (a + 4 b) )
2 1/2 1/2
%2 := (2 a - 2 (a + 4 b) )
2 1/2 1/2
%3 := (2 a + 2 (a + 4 b) )
2 1/2 1/2
%4 := (-2 a + 2 (a + 4 b) )
Next we find the integer solutions of an equation:
>> pretty(sym(maple('isolve(3*x-4*y=7)')))
{y = 2 + 3 _N1, x = 5 + 4 _N1}
Now we solve system and an equation approximately.
>> maple('f: = sin(x + y) - exp(x) * y = 0: ' g: = x ^ 2 - y = 2:'),
>> pretty(sym(maple('fsolve({f,g},{x,y},{x=-1..1,y=-2..0})')))
{y = -1.552838698, x = -.6687012050}
>> maple('f:=10-(ln(v+(v^2-1)^(1/2))-ln(3+(3^2-1)^(1/2)))'),
>> pretty(sym(maple('fsolve(f,v)')))
64189.82535
>> pretty(sym(maple('fsolve(f,v,1..infinity)')))
64189.82535
In the two following equations, instead of solving for x, we solve for sin(x) in the first and x2 in the second.
>> pretty(sym(maple('readlib(isolate):isolate(4*x*sin(x)=3,sin(x))')))
sin(x) = 3/4 x
>> pretty(sym(maple('isolate(x^2-3*x-5,x^2)')))
2
x = 3 x + 5
Now we test whether two expressions are not equal but probabilistically equivalent.
>> maple('a: = (sin(x) ^ 2 - cos(x) * tan(x)) * (sin(x) ^ 2 + cos(x) * tan(x)) ^ 2:)
b: = 1/4 * sin(2*x) ^ 2 - 1/2 * sin(2*x) * cos(x) - 2 * cos(x) ^ 2
(+ 1/2 * sin(2*x) * cos(x) ^ 3 + 3 * cos(x) ^ 4 - cos(x) ^ 6:'),
>> pretty(sym(maple('evalb(a = b)')))
false
>> pretty(sym(maple('evalb(expand(a) = expand(b))')))
false
>> pretty(sym(maple('testeq(a = b)')))
true
In the following example, we eliminate a variable from a system:
>> pretty(sym(maple('readlib(eliminate):eliminate({x^2+y^2-1,x^3-y^2*x+x*y-3},x)')))
3 6 4 5 3 2
[{x = - ------------}, {4 y - 7 y - 4 y + 6 y + 4 y - 2 y + 8}]
2
2 y - y – 1
EXERCISE 5-1
Find solutions to the following equations:
sin(x) cos(x) = 0, sin(x) = acos(x), ax ^ 2 + bx + c = 0 and sin(x) + cos(x) = sqrt(3) / 2
>> solve('sin(x) * cos(x) = 0')
ans =
[ 0]
[1/2 * pi]
[-1/2 * pi]
>> solve('sin(x) = a * cos(x)','x')
ans =
atan (a)
>> solve('a*x^2+b*x+c=0','x')
ans =
[1/2/a * (-b + (b ^ 2-4 * a * c) ^(1/2))]
[1/2/a * (-b-(b^2-4*a*c) ^(1/2))]
>> solve('sin(x)+cos(x)=sqrt(3)/2')
ans =
[1/2 * 3 ^(1/2)]
[1/2 * 3 ^(1/2)]
EXERCISE 5-2
Find at least two solutions for each of the following trigonometric and exponential equations:
x sin (x) = 1/2 and 2x ^ 3 = 4 (23 x)
Initially, we use the fsolve command:
>> maple ('fsolve(x * sin(x) = 1/2)')
ans =
-.74084095509549062101093540994313
>> maple('fsolve(2^(x^3)=4*2^(3*x))')
ans =
2.0000000000000000000000000000000
For both equations we get a single solution. For more solutions, we can plot a graphical representation of the functions (see Figure 5-1) to determine the approximate intervals where possible solutions fall:
>> fplot('[x * sin (x) - 1/2.0]', [0, 4 * pi])
We can see that there is a solution between 0 and 2, another between 2 and 4, another between 4 and 8, and so on. We can calculate three of them as follows:
>> (('s1=maple('fsolve(x*sin(x)=1/2,x,0..2)')
s1 =
.7408409550954906
>> s2=maple('fsolve(x*sin(x)=1/2,x,2..4)')
s2 =
2.972585490382360
>> s3=maple('fsolve(x*sin(x)=1/2,x,4..8)')
S3 =
6.361859813361645
We repeat the process for the second equation, starting with the plotted graph (see Figure 5-2):
>> subplot(2,1,1)
>> fplot('[2^(x^3),4*2^(3*x)]',[-3,1,-1/4,3/2])
>> subplot(2,1,2)
>> fplot('[2^(x^3),4*2^(3*x)]',[1,3,100,400])
Two areas where there are intersections for different values of the variables x and y have been represented on the same graph. There are possible solutions between –4 and 0, and between 0 and 3. We try to find these solutions:
>> maple('fsolve(2 ^(x^3) = 4 * 2 ^(3*x), x, -4.. 0)')
ans =
-1.00000000000
>> maple('fsolve(2^(x^3)=4*2^(3*x),x,0..3)')
ans =
2.00000000000
We see that x =-1 and x = 2 are exact solutions of the equation.
EXERCISE 5-3
Solve each of the two following logarithmic and surd equations:
x3/2 log(x) = x log(x3/2), sqrt[1-x]+sqrt[1+x] = a
>> maple('fsolve(x^(3/2)*log(x)=x*log(x)^(3/2))')
ans =
1.
We first generate the corresponding graphs (see Figure 5-3) to determine the intervals in which possible solutions are found. This reveals that x = 1 is the only real solution.
>> fplot('[x^(3/2) * log(x), x * log(x^(3/2))]', [0,3,-1,6])
Now, let's solve the surd equation:
>> pretty(sym(solve('sqrt(1-x)+sqrt(1+x)=a','x')))
2 1/2
[- 1/2 a (-a + 4) ]
[ ]
[ 2 1/2 ]
[1/2 a (-a + 4) ]
EXERCISE 5-4
Solve the following two equations:
x5 + 16 x4+ 7 x3+ 17 x2+ 11 x + 5 = 0 and x4 - 1 = 0
In addition, solve the first equation modulo 19 and the second modulo 5.
>> s1=solve('x^5 +16*x^4+7*x^3+17*x^2+11*x+5=0')
[ -15.61870451719182]
[-.3867059805744952-.3977796861292117*i]
[-.3867059805744952+.3977796861292117*i]
[.1960582391704047-1.000858165543796*i]
[.1960582391704047+1.000858165543796*i]
>> s2=solve('x^4-1=0')
s2 =
[1]
[-1]
[i]
[-i]
Now, we solve the first equation modulo 19:
>> maple('msolve(x^5 +16*x^4+7*x^3+17*x^2+11*x+5=0,19)')
ans =
{x = 1}, {x = 18}, {x = 3,} {x = 7}, {x = 12}
Next, we solve the second equation modulus 5:
>> maple('msolve(x^4-1=0,5)')
ans =
{x = 1}, {x = 2}, {x = 3}, {x = 4}
Because we have two polynomial equations, there is also the option of solving the two equations with the roots command, as shown here:
>> roots([5,11,17,7,16,5])
ans =
-1.2183 + 1.3164i
-1.2183 - 1.3164i
0.2827 + 0.9302i
0.2827 - 0.9302i
-0.3289
>> roots([-1,0,0,0,1])
ans =
-1.0000
0.0000 + 1.0000i
0.0000 - 1.0000i
1.0000
EXERCISE 5-5
Solve the following system of two equations:
cos(x/12) /exp(x2/16) = y
-5/4 + y = sin(x3/2)
>> [x,y] = solve('cos(x/12)/exp(x^2/16) = y','- 5/4 + y = sin(x^(3/2))')
x =
2.412335896593778
y =
.6810946557469383
EXERCISE 5-6
Find the intersection of the hyperbolas of equations x2 - y2= r2 and a2x2- b2y2= a2b2 with the parabola z2 = 2px.
>> [x,y,z] = solve('a^2*x^2-b^2*y^2=a^2*b^2','x^2-y^2=r^2','z^2=2*p*x', 'x,y,z')
x =
[1/2*RootOf((a^2-b^2)*_Z^4+4*b^2*r^2*p^2-4*a^2*b^2*p^2)^2/p]
[1/2*RootOf((a^2-b^2)*_Z^4+4*b^2*r^2*p^2-4*a^2*b^2*p^2)^2/p]
y =
[1/2*(RootOf((a^2-b^2)*_Z^4+4*b^2*r^2*p^2-4*a^2*b^2*p^2)^4-4*r^2*p^2)^(1/2)/p]
[-1/2*(RootOf((a^2-b^2)*_Z^4+4*b^2*r^2*p^2-4*a^2*b^2*p^2)^4-4*r^2*p^2)^(1/2)/p]
z =
[RootOf((a^2-b^2)*_Z^4+4*b^2*r^2*p^2-4*a^2*b^2*p^2)]
[RootOf((a^2-b^2)*_Z^4+4*b^2*r^2*p^2-4*a^2*b^2*p^2)]
Now, we simplify the RootOf expressions in the above solutions:
>> [simple(allvalues(x)), simple(allvalues(y)), simple(allvalues(z))]
ans =
[b*(-r^2+a^2) ^(1/2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*b^(1/2)*p^(1/2)*(-r^2+a^2)^(1/4)/(a^2-b^2)^(1/4)]
Each line of this output (a set of three values) is a solution of the system, that is, a point of intersection of the three curves.
EXERCISE 5-7
Solve the inequality x2 + x > 5.
>> maple('solve(x^2+x>5,x)')
ans =
RealRange(-inf,Open(-1/2-1/2*21^(1/2))),RealRange(Open(-1/2+1/2*21^(1/2)),inf)