Vous êtes sur la page 1sur 8

Solving LINEAR EQUATIONS in

MATLAB

Contents

Introduction ................................................................................................................... 2
Solving Linear Equations ............................................................................................... 2
Determined linear systems ........................................................................................ 2
Underdetermined equation system............................................................................ 4
Symbolic solution

Redundant equation systems ..................................................................................... 6

Introduction
Matlab's strengths include cutting-edge algorithms, enormous data handling abilities, and powerful programming
tools. Matlab was designed for numeric computation, but does include a MUPAD kernel for symbolic computations.
This tool will be unsed in NMST as well as AMN.

Solving Linear Equations1


Determined linear systems
In many cases it is possible to find an explicit solution for an equation system. E.g. the linear equation system:

x1 + 5 - 2 x2 + x3 = 0
5 x1 - x2 = 0

(1)

3 x3 x1 = 0
This system has three variables (x1. x2, x3) and three equations. Thus, if there is no redundant equation, the system
should have exactly one solution:

x1 = 0.8, x = 4, x3 = 2.2

(2)

How can you use MatLab to find this solution? As being the Matrix Laboratory, linear algebra is a strength of MatLab.
Thus, the system in Eq. (1) is reformulated as matrix operation:

1 2 1 x1 5


5 1
x2 = 0
1
1 x3 3

x
b
A

(3)

The solution to this system is:

Ax = b

(4)

x = A -1b

In this primer no details about the mathematical background are given. Students interested in the mathematics are
referred to an additional document on blackboard, or take a look at standard literature, e.g. Lay, Linear Alegra and its
applications, Addison-Wesley, 1997.

LM3571 MATLAB - 2015/16, S.A. Wahl

Page 2 of 8

The complete steps are now performed in MatLab:


% linear algebra example
A = [ 1 -2 1 ;...
5 -1 0 ;...
-1 0 -1 ];
b = [-5; 0; -3];
x = A^-1 * b

Output:
x=
0.8000
4.0000
2.2000

Thus x1=0.8, x2=4, x3 = 2.2.


To check if that indeed solves A x equals b, we can display the residuals (should be zeros).
% check
A * x - b
ans =
0
0
0
Indeed, the calculated solution is correct, all residuals are zero.

LM3571 MATLAB - 2015/16, S.A. Wahl

Page 3 of 8

Underdetermined equation system


If there is an equation less, e.g.

x1 + 5 - 2 x2 + x3 = 0
5 x1 - x2 = 0

(5)

This system has three variables (x1. x2, x3) and (at least) 1 degree of freedom. Choosing x3 as the independent
variable you can write the solution as:

Which can be evaluated for a given x3 directly.


How can you use MatLab to find this solution. As being the Matrix Laboratory, linear algebra is a strength of MatLab.
Thus, lets have a closer look at the system in Eq. (5).
a) Variables: x1, x2, x3
b) Two equations
c) At least one degrees of freedom.

Lets reformulate that system in matrix notation, using a vector x = (x1, x2, x3) and a matrix A to generate the
equations:

x1
1 2 1 5

x2 =
5 1 0
x3
b
A
x

(6)

Fast check on A are there dependent rows?


rank( A ) = 2

no redundancy, all equations are independent

To solve the system, one variable has to be chosen as independent one:


As an example, x3. The system is rewritten in terms of dependent and independent variables

1 2 x1 1
5

+ ( x3 ) =
5 1 x 2 0 x
0
f
xd
b
Af
Ad

(7)

The solution for this system is

x d = A d1 ( b A f x f

LM3571 MATLAB - 2015/16, S.A. Wahl

(8)

Page 4 of 8

In MatLab:

% linear algebra example


A = [ 1 -2 1 ;...
5 -1 0 ];
b = [-5; 0 ];
Af = A(:,3);
Ad = A(:,[1,2]);
xf = 2;
xd = Ad^-1 * (b - Af * xf );
% check
Ad * xd + Af * xf - b

The result is indeed (0 0) the equations are fulfilled, for all values of x3.

Symbolic solution
For many tasks it is relevant to keep the free variable visible and express the solution as a function of this variable
without entering a value.
MATLAB has an extension called the symbolic toolbox that can express symbolic solutions instead of a numeric
solution. Therefore, the variable that should be treated as symbolic needs to be defined by:

% symbolic linear algebra example


syms x3
A = [ 1 -2 1 ;...
5 -1 0 ];
b = [-5; 0 ];
Af = A(:,3);
Ad = A(:,[1,2]);
xf = x3;
xd = Ad^-1 * (b - Af * xf );
The solution is now expressed with x3 as symbolic variable:

xd =
x3/9 + 5/9
(5*x3)/9 + 25/9

LM3571 MATLAB - 2015/16, S.A. Wahl

Page 5 of 8

Redundant equation systems


In case that there are more than three equations available for the same variables x1, x2, x3 we speak of an
redundant system. Usually, there will be no solution fulfilling all equations; Especially, when the coefficients are
derived from measurements (see task on Linear Algebra) there will be deviations.
To take advantage of all data, the solution can be found by looking for a best estimate of x1, x2, x3 that fulfills the
criteria of the lowest error. We speak of the least-square-solution. For linear systems, an explicit solution can be
derived (see e.g. chapter 6.5 & 6.6 in Lay, 1997). Here an example:

1 2 1
5

x1
5 1
x2 = 0
1
1 3

x3
1

x
3.8
b
A

(9)

Looking at the solution obtained in Eq. (2) you can immediately see, that the last equation x2=3.8 conflicts with the
previous calculation x2=4. The least-square solution is calculated using the standard multiple regression formula (see
explanatory notes in linear regression on how to derive the equation):
1

x = ( AT A ) AT b

(10)

In MatLab:

% linear algebra example


A = [ 1 -2 1 ;...
5 -1 0 ;...
-1 0 -1 ; ...
0 1 0 ];
b = [-5; 0; -3; 3.8];
x = (A'*A)^-1 * A' * b
% check
A * x - b
The result is
x =
0.7867
3.9333
2.1467
ans =
0.0667
0.0000
0.0667
0.1333
The values of x change slightly compared to the first example with three equations.
Equation nr. 2 can be fulfilled ans(2)=0, for the three equations deviations of 0.067 resp. 0.133 are observed. This
estimate of x is the best solution, in especial with lowest sum of squared deviations.

LM3571 MATLAB - 2015/16, S.A. Wahl

Page 6 of 8

Exercise 1

Glucose is fermented by Streptococcus mutans to lactate according to the following proposed


stoichiometric equation:

aC 6H 12O6 + b NH 4+ + c H 2O + d H + + e HCO3 + f C 3H 5O3 + CH 1.8O0.5N 0.2 = 0


1 Formulate the charge and element (C, H, O, N) balances for this reaction and put them into the matrixvector form needed for solution with MATLAB. Write and execute the MATLAB script that solves
symbolically the system of equations as a function of coefficient f (for lactate).
2 Knowing the degrees of reduction for all compounds ( C 6H12O6 = 24 , C

3H 5O3

= 12 ,

NH + = H 2O = H + = HCO3 = 0 ), formulate an additional balance equation and add it to the


4

system. Try to solve the new system numerically and explain the result, even if it fails. Is the
extended system of equations determined now? Explain your calculations.
3 Use the symbolic solution from 3.1 to plot coefficients a (glucose) and e (bicarbonate) as a function of f,
for f values in the interval [1,2]. Label the plot axes and include a legend.
(commands: plot, xlabel, ylabel, legend).

Exercise 2

Glucose is fermented by Streptococcus mutans to lactate according to the following proposed


stoichiometric equation:

a CO2 + b NH 4+ + d O2 + g Fe 2+ + e H + + 1CH1.8O0.5 N 0.2 + c Fe3+ + f H 2O = 0

1 Formulate the charge and element (C, H, O, N, Fe) balances for this reaction and put them into the
matrix-vector form needed for solution with MATLAB. Write and execute the MATLAB script that
solves symbolically the system of equations as a function of coefficient g (for Fe2+).
2 Try to formulate the solution as a function of coefficient a. Explain why it does not work.
3 Use the symbolic solution from 1 to plot coefficients d (O2) and f (water) as a function of g, for g
values in the interval [1,5]. Label the plot axes and include a legend.
(commands: plot, xlabel, ylabel, legend).

LM3571 MATLAB - 2015/16, S.A. Wahl

Page 7 of 8

Exercise 3:
Task now is to apply this knowledge to find stoichiometric coefficients for the Herbert-Pirt relation based on a series
of steady state experiments from an micro-aerobic ethanol production plant with S. cerevisiae.

Growth rate

D=0.1
D=0.05
D=0.2
D=0.12
D=0.03

0.1
0.05
0.2
0.12
0.03

Substrate uptake
- qS
0.558013
0.349866
0.837589
0.623872
0.23716

Production rate

qP
0.65625
0.4375
0.875
0.715909
0.302885

Tasks
1. The Herbert-Pirt equation reads:

qS = a + b qP + mS

(11)

a) Which variables are known (free, independent),


b) Which ones are to be determined (dependent)?
c) Formulate the given problem as a matrix equation system
d) Bring the problem into the standard format for regression

Ax =b

(12)

with the vectors/matrix


x: Vector of dependent variables
b:Right hand side
A: Matrix generating the equation system
e) Generate the respective vectors/matrices
f) How many of the experiments are required to achieve a determined system?
g) What if you use all?
2. Write a script to solve the system
a. For a determined case (choose your favorite set of experiments)
Test two different ones.. what do you expect, what do you observe comparing two sets?
b. For a redundant system
c. For an underdetermined system if that works
(e.g. assume, m is known from another experiment with the same organism, what if the assumption is
wrong?)

LM3571 MATLAB - 2015/16, S.A. Wahl

Page 8 of 8