EM1 Ex8 Matlab Theory
EM1 Ex8 Matlab Theory
Linear systems of equations can be solved efficiently with Matlab, both numerically and
symbolically. This can be done using (at least) two standard approaches.
You can solve the system by entering the corresponding matrix equation and using the
inverse matrix, similar to how it’s done in Mathcad. For example, the system
x + 2y =4
2x − y
(1.1)
=3
is solved by
In Matlab, there is a built-in solver for linear systems of equations in matrix form, which
is the "matrix division". The last line of the previous example using it would be x=A\b.
Generally, it might be smarter to use this since it works even if the system has infinitely
many solutions or none at all. In the first case, x=A\b gives the "least costly" solution in
a certain sense, and in the latter case, the best compromise solution. The division solver
is also computationally more efficient, which is significant when solving very large systems.
For example, the system
x + 2y + z = 4
2x − y − z = 3
1
Be careful to use the backslash, i.e., \, not the regular slash /.
Matlab can handle complex numbers. The imaginary unit can be written as i or, if preferred,
j, as often used in electrical engineering. The number is then entered normally, e.g., 1+2*i.
Try the following if you wish
1 >> (1+2*i)^2/(55−i)
2
3 ans =
4
5 −0.0558 + 0.0717i
You can perform symbolic calculations in Matlab by defining symbolic variables. You can
also perform calculations with numbers symbolically, resulting in exact values instead of
approximations as in regular numerical calculations. Symbolic variables are defined with the
syms command, followed by the desired variables. If you want numbers to be treated sym-
bolically, they should be written inside the sym command, e.g., sym(pi) or sym(2^(1/2)).
Try the following if you wish
1 >> syms x y
2 >> x+x+4*y+x+−3*y
3
4 ans =
5
6 3*x + y
7
8 >> sin(sym(pi)/4)
9
10 ans =
11
12 2^(1/2)/2
To solve an equation/system of equations, first define the variables using the syms command,
then use the solve command with the specified equations, following Matlab’s syntax pre-
cisely. There are several options for this, and detailed instructions for different methods can
be found at
@https://www.mathworks.com/help/symbolic/solve.html
https://www.mathworks.com/help/symbolic/solve-a-system-of-algebraic-equations.html
If you solve single-variable equations with solve, you can do so by applying the following
example.
1 >> syms x
2 >> solve(4*x−5==4,x)
3
4 ans =
5
6 9/4
7
8 >> syms x
9 >> solve(x^2+7*x−2==4,x)
10
11 ans =
12
13 − 73^(1/2)/2 − 7/2
14 73^(1/2)/2 − 7/2
For systems of equations, the syntax is more complex. The following method works in
both Matlab and Octave. First, define the variables with the syms command, then create a
symbolic matrix in the form
1 \begin{lstlisting}
2 >>syms x y;
3 >>S=solve( [x+y==4, y−x==17],[x,y] )
4
5 S =
6
7 struct with fields:
8
9 x: [1x1 sym]
10 y: [1x1 sym]
11
12 >> S.x
13
14 ans =
15
16 −13/2
17
18 >> S.y
19
20 ans =
21
22 21/2
The following method, which displays all solutions easily, seems to work reliably only in
Matlab: The command is given in the form
If you want to solve the system (1.1), you can do so with the following commands
1 >> syms x y
2 >> [x, y]=solve(x+2*y==4,2*x −y == 3, x,y)
3
4 x =
5
6 2
7
8
9 y =
10
11 1
The solve function uses the so-called comparative equality, i.e., two equal signs in a row ==.
The solve function can also solve non-linear systems of equations, but its use can be initially
cumbersome and very sensitive to syntax. It might also be wise to define the equations as
variables first rather than inputting them directly, for example
1 >> syms x y
2 >> eq1=x+2*y==4; eq2=2*x −y == 3;
3 >> S=solve([eq1,eq2],[x,y])
4
5 S =
6
7 struct with fields:
8
9 x: [1x1 sym]
10 y: [1x1 sym]
11
12 >> S.x
13
14 ans =
15
16 2
17
18 >> S.y
19
20 ans =
21
22 1
1 >> syms x y
2 >> e1=x+2*y==4;
3 >> e2=2*x −y == 3;
4 >> [x, y]=solve(e1,e2, x,y)
5
6 x =
7
8 2
9
10
11 y =
12
13 1
1.3 Numerical Solve Function
The previously presented solve command always attempts to solve the equation symbolically.
This isn’t always possible, but approximate solutions might still be found numerically. The
appropriate command in Matlab is vpasolve. Its syntax is very similar to that of the
symbolic solver. Detailed instructions with examples can be found at
https://www.mathworks.com/help/symbolic/sym.vpasolve.html
1 >> syms x
2 S = vpasolve(2*x^4 + 3*x^3 − 4*x^2 − 3*x + 2 == 0, x)
3
4
5 S =
6
7 −2.0
8 −1.0
9 0.5
10 1.0
The system
v 3 + 2u, = v
v 2 = u.
can be solved (and the solutions accessed) as follows:
1 >> syms u v
2 Y = vpasolve([v^3 + 2*u == v, v^2==u], [u,v])
3
4
5 Y =
6
7 struct with fields:
8
9 u: [3x1 sym]
10 v: [3x1 sym]
11
12 >> Y.u
13
14 ans =
15
16 0
17 5.8284271247461900976033774484194
18 0.1715728752538099023966225515806
19
20 >> Y.v
21
22 ans =
23
24 0
25 −2.4142135623730950488016887242097
26 0.4142135623730950488016887242097
Now Y.u gives all possible values for u, and Y.v gives all possible values for v in such a way
that the order corresponds to the solution pair u.
A numerical solver can be given an initial guess. When solving an equation with one un-
known, the syntax is
vpasolve(equation,variable,initial guess)
For example, if you want to find a solution to the equation sin(x) = 2 cos(x) numerically
that is close to 1000, you can do so as follows:
1 >> syms x
2 >> vpasolve(sin(x)==2
3
4 *cos(x),1000)
5
6 ans =
7
8 1000.1336125593483403341376613431