Professional Documents
Culture Documents
University of UMSA
La Paz
April 08-26, 2013
Mesfin Belayneh
UoS, Norway
Contents
1 INTRODUCTION ................................................................................................................... 3
2 VECTOR ALGEBRA ............................................................................................................. 4
2.1 Addition of vectors ......................................................................................................... 4
2.2 Multiplication by a scalar ............................................................................................... 4
2.3 Resolving vectors into components ................................................................................ 5
2.4 Dot product ..................................................................................................................... 7
2.5 Cross product .................................................................................................................. 8
3 MATRIX ALGEBRA ........................................................................................................... 15
3.1 Properties of matrices ................................................................................................... 15
3.2 Procedure to solve simultaneous equation with a matrix system ................................. 20
3.3 Determinants ................................................................................................................ 21
3.4 Minors and Cofactors ................................................................................................... 21
3.5 Adjoint matrix .............................................................................................................. 23
3.6 Inverse matrix ............................................................................................................... 23
4 MODELING VIA DIFFERENTIAL EQUATIONS ............................................................ 26
4.1 First order differential equation .................................................................................... 26
4.2 Second order equations and systems ............................................................................ 30
4.3 Numerical solution methods ......................................................................................... 33
4.3.1 Newton-Raphons non-linear root finding ........................................................ 33
4.3.2 Numerical intergration ..................................................................................... 36
4.3.2.1 Simpson ............................................................................................................. 36
4.3.2.2 Trapezium rule................................................................................................... 37
1 Introduction
All fields of studies (Science and Engineering) use mathematics. The language of
mathematics is a tool to formulate theories based on experimental observations. Therefore, in
this section we will learn the basic of mathematics along with the applications. The topics to
be reviewed are:
2 Vector algebra
A vector is used to represents a physical quantity that has both a direction and magnitude, for
example, displacement and force. A vector is generally represented by the symbols
U or U
On a diagram, it is generally represented by an arrow. The direction of the arrow represents its
direction and the length represents its magnitude.
Commutative law: U + V = V + U
Associative law: (U + V) + W = U + (V + W)
U
V
V
W
U
U+V=V+U=W
Fig. 1 Parallelogram rule
2.2 Multiplication by a scalar
The product of a scalar p and a vector U can be written as pU. It follows that:
p(qU) = (pq)U
(p + q)U = pU + qU
p(U + V) = pU + pV
A vector -U is called "the negative of the vector U" and it has the same magnitude as vector U
but its direction is in the opposite direction.
2U
-U
U U 2x U 2y U 2z
Ux=Uxi
Uy=Uyj
U=Uxi +Uyj
When there are two concurrent forces applied to a body a single resultant force can be used to
represent the two forces by applying the addition law for vectors (i.e. parallelogram law). The
use of trigonometry based on the law of sine and cosine can also be helpful in solving
problems involving two force vectors.
C
b
a
Law of sines:
a
b
c
Law of cosine:
b 2 a 2 c 2 2ac cos
Fig. 4 Law of sines and cosine
However, if there are more than two forces present, the standard method to solve a problem is
reduce each vector into its rectangular components so that it they are easy to add and subtract.
A force can be resolved into its rectangular components in the x and y axis as shown in the
figure on the left. The resolved force can be written in Cartesian form:
F = Fxi + Fyj
F = Fcosi + Fsinj
F Fx2 Fy2
A force that is resolved (decomposed) into its rectangular components in the x, y, and z-axis
can be expressed in the following Cartesian form:
F = Fxi + Fyj + Fzk
F = Fcosxi + Fcosyj + Fcoszk
Commutative law:
Associative law:
Distributive law:
U .V = V .U
q(U . V) = (qU) . V = U . (qV)
U .(V + W) = U . V + U . W
F1
S
W= F1 .S +F2 .S = |F1| |S| cos +|F2| |S| cos
2.5 Cross product
Let W be the cross product of two vectors U and V such that U x V = W. We define W to
have the following properties:
V x U = -W
U x V = -(V x U)
Satisfies associative law:
q(U x V) = (q U) x V = U x (qV)
Satisfies distributive law:
U x (V + W) = U x V + U x W
Example: Vector Representation of a Moment
When an arbitrarily-shaped body is acted upon by a force, there exists a tendency for the
force to rotate the body about a point or axis. This tendency is known as a moment. The point
or axis about which the rotation takes place is referred to as the moment center. Moments are
usually described with reference to a point or axis on the body.
The magnitude of a moment, M, can be described as the product of the perpendicular
distance, d, measured from the point or axis where the force is applied. The perpendicular
distance is also known as the moment arm.
In practice, the moment is considered to be positive if the resultant rotation is
counterclockwise. Negative moments generate clockwise rotation. A moment is a vector
quantity, having both magnitude and direction.
F
d
Moment = Fd
Referring to the figure shown on the left, the moment about point O caused by the applied
force F, can be described by the cross product of the distance vector, r, and the force vector,
F.
Mo = r x F
= r F sin
0 1800
Mo=dF
The direction of the rotation can be determined using the Right Hand Rule
ixj=k
i x k = -j
j x i = -k
jxj=0
jxk=i
kxi=j
k x j = -i
kxk=0
For a vector U = Uxi + Uyj + Uzk and a vector V = Vxi + Vyj + Vzk, the determinant of U and
V can be written as:
i
UxV U x
Vx
j
Uy
Vy
k
Uz
Vz
Fig. 10: The force applied at point 2 generates a moment about point 1
In a two dimensional case, there are no components in the k direction. The force and vector r
are given in the following:
r = rxi + ryj
F = Fxi + Fyj
Mo = r x F
i
M o rxF rx
Fx
j
ry
Fy
k
0
0
= (rxFy - ryFx)k
= Mzk
j
ry
Fy
k
rz
Fz
Fy = 0
Fz = 0
Mx = 0
My = 0
Mz = 0
Example 1
Determine the moments about point O caused by the two applied forces.
i
M o rxF rx
Fx
j
ry
Fy
k
0
0
= (rxFy - ryFx)k
= Mzk
j
ry
Fy
k
rz
Fz
3 Matrix algebra
In mathematics we have many ways of expressing numbers and each method use its own rules
of manipulation (algebra). For example, vector algebra is quite different but logical and very
useful in the solution of problems. Matrix theory is another tool that helps us to solve
problems of a particular kind in Engineering and Science and it is also linked to vector theory.
The theory is quite difficult to understand and the rules used to manipulate a matrix are quite
complex. It will help if you have studied vector theory. In order to fully understand matrix
theory, you will need to have studies to quite an advanced level. This section is only a
beginning.
In this section you will be introduced to some of the basics terms and operations that we can
perform with them. We shall also look at more advanced ideas and how they are used to solve
simultaneous equations
On the completion of this chapter, you should be able to do the following
4 8 3
2 4
A 5 8
7 3
T
Add
To show this a matrix operation we would write the following. In other words we simply add
or subtract the elements in the same position in each array. So it follows that the arrays must
be the same size.
x
x
x
2 3 4 y 3 4 2 y 5 7 2 y
z
z
z
We add and subtract two matrices, we dont need to write in the column vector as it is the
same for each part so we only need to write
3 4 3 4 2 5 7 2
Example
Add and subtract the two arrays as shown
3.1.4 Multiplication
Scalar
If an equation is multiplied by a constant (scalar) every coefficient will be multiplied by that
constant so a matrix may be easily multiplied or divided by a scalar
a 11 a 12 a 13 a 11 a 12 a 13
a 21 a 22 a 23 a 21 a 22 a 23
a 31 a 32 a 33 a 31 a 32 a 33
Vector
Now we will consider how to multiply a matrix by a column vector X to obtain a column
vector b. We are trying to find AX = b.
Column vectors
A=
X
It will be seen next that this is a special case of multiplication of arrays in general. For two
arrays the rule is more complex.
Array multiplication
Consider how we multiply an array A by another B to obtain a new array C
a
a
A 11 12
a 21 a 22
b
b
B 11 12
b 21 b 22
c
c
AB C 11 12
c 21 c 22
a 11
b
a 12 11 a 11b11 a 12 b 21
b 21
For element c12, take the first row of A and multiply by the second column of B
a 11
b
a 12 12 a 11b12 a 12b 22
b 22
For element c21, take the second row of A and multiply by the first column of B
a 21
b
a 22 11 a 21b11 a 22b 21
b 21
For element c22, take the second row of A and multiply by the second column of B
a 21
b
a 22 12 a 21b12 a 22b 22
b 22
Collecting all
a b a b
a b a b
C 11 11 12 21 11 12 12 22
a 21b11 a 22b 21 a 21b12 a 22b 22
Row.1xCol .1 Row.1xCol .2
C
Row.2xCol .1 Row.2xCol .2
Other rules
It can be shown that the normal rules of multiplication may be observed if the orders of
matrices A, B, C are mentioned on the left side of the equality sign
A(BC) = (AB)C
(A+B)C = AC+BC
A(B+C)= AB+AC
It can also be shown that (AB)T= BTAT
0 1
A
1 0
2
B
1
C 0 1 5
Evaluate (AB)C and A(BC). Check that you get the same matrix.
Answer: We have
1
AB
2
so
1
0 1 5
(AB)C 0 1 5
2
0 2 10
On the other hand, we have
0 2 10
(BC)
0
so
0 1 0 2 10 0 1 5
A(BC)
1 0 0 1 5 0 2 10
2
1
1 2
B
1
1
The identity matrix behaves like the number 1 not only among the matrices of the form nxn.
Indeed, for any nxm matrix A, we have
InA = A and AIm =A
3.2 Procedure to solve simultaneous equation with a matrix system
An important use of matrix theory is the solution problem with more than two unknown
variables and because the method is based on strict rules, it is suitable for use in computer
programs. Consider how three simultaneous equations are presented as a vector. The Matrix A
is called the Coefficient matrix and it is formed from an array of numbers made from the
coefficients a11, a12,
The column vector b is made from the coefficients b1, b2,.and the column vector X is made
up from the variables, x, y, and z
The column vector b is the product of matrix A and X
Column vectors
a11x+a12y+a13z =b1
a21x+a22y+a23z =b2
a31x+a32y+a33z =b3
We write this as AX = b
A
Definition:
Suppose that we had a matrix B such that AB = I (The unit matrix), we then could state as:
ABX = IX = Bb ,
X = Bb
This gives a numerical solution for X. the problem is finding the matrix B such that AB = I.
This matrix is called the inverse matrix and we need to understand the following section in
order to determine the inverse matrix.
3.3 Determinants
The determinant of a matrix is a single number that results from performing a specific
operation on the array. It will be used later to solve simultaneous equations. The determinant
of a matrix A is denoted by det A or A. The rule for finding the determinant can only be
applied to a square matrix and the following is an explanation for it.
For a 2x2 array the determinant is found as follows.
a
det A 11
a 21
a 12
a 22
A=a11*a22 - a12*a21
a 11
a
21
a 31
a 12
a 22
a 32
a 13
a 23
a 33
a
M11 A11 det 22
a 32
a 23
a 33
The cofactor is numerically the same as the minor but it changes sign for every position in the
raw or column and the change in sign is indicated by the pattern shown. This is designated
with a letter corresponding to the elements so in this case the minor would be A32 and from
the sign patter we find A32 = M32
STEP 1
Put a line through row 1 and column 1 leaving the elements shows.
Find the determinant of the 2x2 array enclosed in the square. This
is called the MINOR of a11 and designated by M11
The COFACTOR is A11 = M11
a 11 a 12 a 13
A a 21 a 22 a 23
a 31 a 32 a 33
a 11 a 12 a 13
a
21 a 22 a 23
a 31 a 32 a 33
STEP 2
Put a line through row 1 and column 2 leaving the elements shown.
From these into a 2x2 array and find the determinant. The result is
the Minor M12. The COFACTOR is A12 = -M12
a 11
a
21
a 31
STEP 3
Put a line through row 1 and column 3 leaving the elements shown.
From these into a 2x2 array and find the determinant. The result is
the Minor M13.
The COFACTOR is A13 = M13
The determinant of the whole array is now found from:
A=a11A11+a12A12+a13A13
a 11
a
21
a 31
a 12
a 22
a 32
a 12
a 22
a 32
a 13
a 23
a 33
a 13
a 23
a 33
For larger arrays the method is the same but the process is repeated until we left with a 2x2
array. The cofactors takes one the sign as indicated by the elements position shown below
Another concept used in matrix methods is the adjoint or adjugate matrix. This has very
useful properties in the solution of problems. This is a matrix formed from all the cofactors of
the original matrix and then transposed. We designate this adj
If we had 3x3 matrix designated A, the adjoint is given as
a 11 a 12 a 13
A11 A12
A a 21 a 22 a 23 , adjA A 21 A 22
a 31 a 32 a 33
A 31 A 32
A13
A 23
A 33
We are in the last stage now and next we will be able to solve system of equations. Suppose
we had two matrices A and B such that the product is the unit matrix, i.e
AB = I and it follows that BA = I
Matrix B is the inverse of matrix A so we denote it A-1 and replace B with this, so AA-1 =I
We have already used the relationship A adj A =AI
So equating we have A adj A = AAA-1
This result:
a b d c d b
adj
c d b a c a
T
which gives
1
a b
1 d b
ad bc c a
c d
2 3
1 3 / 2
and B
A
2
2
1
1 0
I 2
AB BA
0 1
1 1
A
1 2
Write
Since
a b
A 1
c d
bd
ac
I 2
AA 1
a 2c b 2d
we get
a c 1
a 2c 0
bd0
b 2d 1
Algebraic manipulations give
a
2
3
1
3
or
1
2
3
A 1 3
1
1
3 3
1
3
1
3
The inverse matrix is unique when it exists. So if A is invertible, then A-1 is also invertible and
1 1
The following basic property is very important: If A and B are invertible matrices, then AB is
also invertible and
AB1 B1A 1
Examples of first order differential linear equations with constant coefficients included:
exponential growth, modelling examples including radioactive decay and time delay equation.
A first order linear differential equation has the following form:
dy
p( x ) y q ( x )
dx
u(x)q(x)dx C
u(x)
where
called the integrating factor. If an initial condition is given, it can be used to find the
constant C.
Here are some practical steps to follow:
1. If the differential equation is given as
a (x)
dy
b( x ) y c( x )
dx
where
p( x )
b( x )
c( x )
q( x )
a ( x ) and
a (x)
u(x) e p( x )dx
3. Evaluate the integral
u(x)q(x)dx
4. Write down the general solution
u(x)q(x)dx C
u(x)
5. If you are given an IVP, use the initial condition to find the constant C.
Example: Find the particular solution of:
y ' tan(x) y cos 2 (x) ,
y(0) 2
p(x) tan(x)
sec(x) cos
sin( x ) C
sin( x ) C cos( x )
sec( x )
Step 5: In order to find the particular solution to the given IVP, we use the initial
condition to find C. Indeed, we have
y(0) = C =2
Therefore the solution is
y sin( x) 2 cos(x)
Application
1 Radioactive decay modeling
Many radioactive materials disintegrate at a rate proportional to the amount present. For
example, if X is the radioactive material and Q(t) is the amount present at time t, then the rate
of change of Q(t) with respect to time t is given by
dQ
rQ
dt
where r is a positive constant (r>0). Let us call Q(0) = Q0 the initial quantity of the material
X, then we have
Q(t ) Q 0 e rt
Clearly, in order to determine Q(t) we need to find the constant r. This can be done using
what is called the half-life T of the material X. The half-life is the time span needed to
disintegrate half of the material. So, we have Q(T) = Qo. This gives rT = ln(2).
Therefore, if we know T, we can get r and vice-versa. For example, the half-life of Carbon-14
is 5568 +/-30years. Therefore, the constant r associated with Carbon-14 is r =1.24410-4. As a
side note, Carbon-14 is an important tool in the archeological research known as radiocarbon
dating.
Example: A radioactive isotope has a half-life of 16 days. You wish to have 30 g at the end
of 30 days. How much radioisotope should you start with?
Solution: Since the half-life is given in days we will measure time in days. Let Q(t) be the
amount present at time t and Qo the amount we are looking for (the initial amount). We know
that
Q(t ) Q 0 e rt
where r is a constant. We use the half-life T to determine r. Indeed, we have
r
1
1
ln( 2) ln( 2)
T
16
Hence, since
Q(30) 30 Q 0 e r 30
30
we get Q 0 30e 16
ln(2 )
110.04g
Given that the rate of change of the population is proportional to the existing population. In
other words, if P(t) measures the population, we have
dP
kP
dt
where the rate k is constant. It is fairly easy to see that if k > 0, we have growth, and if k <0,
we have decay. This is a linear equation which solves into
P( t ) P0 e kt
Where Po is the initial population, i.e. P(0) = Po. Therefore, we conclude the following:
if k>0, then the population grows and continues to expand to infinity, that is,
o Lim P(t) (as r goes to infinity) = +infinity
if k<0, then the population will shrink and tend to 0. In other words we are facing
extinction.
2.1 If the equation has real solutions 1 = 2, then the general solution is
y = c1e1x+ c2e2x
2.2 If the equation has complex solutions 1,2 = u iv, then the general solution is
y = c1eux cos(vx)+ c2eux sin(vx).
2.3 If the equation has only one real solution , then the general solution is
y = c1ex+ c2xex
k
m
lo
m. x kx 0
x ( t 0) x o
.
x ( t 0) o
This equation of motion is a second order, homogeneous, ordinary differential equation
(ODE). If the mass and spring stiffness are constants, the ODE becomes a linear
homogeneous ODE with constant coefficients and can be solved by the Characteristic
Equation method. The characteristic equation for this problem is,
m.s 2 k 0
which determines the 2 independent roots for the undamped vibration problem. The final
solution (that contains the 2 independent roots from the characteristic equation and satisfies
the initial conditions) is,
x(t ) d 1 cos n t d 2 sin n t
x ( t ) x o cos n t
o
sin n t
n
k
m
and depends only on the system mass and the spring stiffness (i.e. any damping will not
change the natural frequency of a system).
Alternatively, the solution may be expressed by the equivalent form,
x( t ) A o cos( n t o )
A o x o
n
o tan 1 o
x o n
2
o
Please note that an assumption of zero damping is typically not accurate. In reality, there
almost always exists some resistance in vibratory systems. This resistance will damp the
vibration and dissipate energy; the oscillatory motion caused by the initial disturbance will
eventually be reduced to zero. This type of problem will not be studied here.
x1 x 0
f (x 0 )
f ' (x n )
x n 1 x n
f (x n )
f ' (x n )
Geometrically, xn+1 can be interpreted as the value of x at which a line, passing through the
point (xn,f(xn)) and tangent to the curve f(x) at that point, crosses the y axis. Figure provides a
graphical interpretation of this
In fact, looking at the graphs we can see that this equation has one solution.
3
2
x
cos (x)
1
0
-3
-2
-1
-1
-2
-3
This solution is also the only zero of the function f(x) = x-cos(x) . So now we see how
Newton's method may be used to approximate r. Since r is between 0 and /2, we set x1 = 1.
The rest of the sequence is generated through the formula
x n 1 x n
f (x n )
x cos(x n )
xn n
'
1 sin( x n )
f (x n )
We have
x1
x2
x3
x4
x5
x6
x7
x8
1
0,75036386784024400
0,73911289091136200
0,73908513338528400
0,73908513321516100
0,73908513321516100
0,73908513321516100
0,73908513321516100
x n 1 x n
f (x n )
x 2n 5
n
2x n
f ' (x n )
It is quite remarkable that the results stabilize for more than ten decimal places after only 5
iterations!
6
4
2
0
0
0.5
1.5
-2
-4
-6
x1
x2
x3
x4
x5
x6
2
2,25
2,23611111111111000
2,23606797791580000
2,23606797749979000
2,23606797749979000
2.5
f ( x)dx
a
(b a)
a b
f (a) 4 f
f (b) .
6
2
2.15
1
2
x2
2
dx
ba
a b
f (a ) 4f
f ( b)
6
2
a 2.15
b 2.9
ab
0.37500
2
f ( x)
f 2.15
x2
2
1
2
2.152
2
0.039550
f 2.9
1
2
2.9 2
2
0.0059525
f 0.375
1
2
0.3752
2
0.37186
f
ba
a b
f (a ) 4f
f ( b)
6
2
(2.9 2.15
5.05
1.2902
4.3.2.2 Trapezium rule
The trapezoidal rule (also known as the trapezoid rule or trapezium rule) is an
approximate technique for calculating the definite integral
b
f (x)dx
a
The trapezoidal rule works by approximating the region under the graph of the function f(x) as
a trapezoid and calculating its area. It follows that
b
f (x)dx (b a)
a
f (a ) f ( b)
2
In general, suppose we need to integrate from x0 to x1. We shall subdivide this interval into n
steps of size x=(x1-x0)/n as shown in figure.
6.73x 4.3025 10 7
dx
11
1.2210 6
2
.
316
10
x
0.6110 6
a) Use single segment Trapezoidal rule to find the time required for 50% of the oxygen
to be consumed.
b) Find the true error,
Solution
a)
f a f b , where
f b a
a 1.22 10 6
b 0.61 10 6
6.73x 4.3025 10 7
f ( x)
2.316 10 11 x
6.731.22 10 4.3025 10
f 1.22 10
2.316 10 1.22 10
11
11
3.0581 10
f 0.61 10
1.22 10
1.9119 10 5 s
1.2210 6
2.316 10 11 x
0.6110 6
dx
1.9014 10 5 s
1056.2
c) The absolute relative true error, t , would then be
t
True Error
100
True Value
1056.2
100
1.9014 10 5
0.55549 %
subject to some boundary/initial condition f(t=t0) = c. The finite difference solution of this
equation proceeds by discretising the independent variable t to t0, t0+t, t0+2t, t0+3t,
We shall denote the exact solution at some t = tn = t0+nt by yn = y(t=tn) and our approximate
solution by Yn. We then look to solve
Y'n = f(tn,Yn)
The Euler method is the simplest finite difference scheme to understand and implement. By
approximating the derivative in as
Y'n ~ (Yn+1 - Yn)/t
in our differential equation for Yn we obtain
Yn+1 ~ Yn + tf(tn,Yn).
Given the initial/boundary condition Y0 = c, we may obtain Y1 from Y0 + tf(t0,Y0), Y2 from
Y1 + tf(t1,Y1) and so on, marching forwards through time. This process is shown graphically
in figure.
Sketch of the function y(t) (dark line) and the Euler method solution (arrows). Each arrow is
tangental to to the solution of passing through the point located at the start of the arrow. Note
that this point need not be on the desired y(t) curve.
Example 1
The concentration of salt
dx
37.5 3.5 x
dt
At the initial time, t 0 , the salt concentration in the tank is 50 g/L. Using Eulers method
and a step size of h 1.5 min , what is the salt concentration after 3 minutes?
Solution
dx
37.5 3.5 x
dt
f t, x 37.5 3.5x
For i 0 , t0 0 , x0 50
x1 x0 f t 0 , x0 h
50 f 0,501.5
50 37.5 3.5(50)1.5
50 137.51.5
156.25 g/L
x2
Figure 1 compares the exact solution with the numerical solution from Eulers method for the
step size of h 1.5 .
The problem was solved again using smaller step sizes. The results are given below in Table
1.
Table 1 Concentration of salt at 3 minutes as a function of step size, h .
step
size,
h
x3
Et
|t | %
3483.0
6622.2
2556.5
0.023249
0.010082
Figure 2 shows how the concentration of salt varies as a function of time for different step
sizes.
Figure 2 Comparison of Eulers method with exact solution for different step sizes
This section presents both the theory of curve fitting and compute methods using Excel
4.3.4.1 Linear , polynomical, exponential and power
Curve fitting - capturing the trend in the data by assigning a single function across the entire
range.The example below uses a straight line function
A straight line is described by f(x) = ax+b
The goal is to identify the coefficients a and b such that f(x) fits the data well
Quantifying the error in the curve fit
Assumption
1) Positive or negative error have the same value (data point is above or below the line)
(x4 , f(x4))
(f(x) = ax+b
(x2 , f(x2))
(x3 , f(x3))
(x1 , f(x1))
error
#data.po int s
(d )
i 1
( y1 f ( x 1 )) 2 ( y 2 f ( x 2 )) 2 ( y 3 f ( x 3 )) 2 ( y 4 f ( x 4 )) 2
error
# data. po int s
( y f ( x ))
i 1
# data. po int s
( y (ax b))
i 1
The best fit line has a minimum error between the line and data points. This is called the least
square approach, since we minimize the square of the error.
Minimizing the error
n
(error )
2 x i ( y i ax i b) 0
a
i 1
n
(error )
2 ( y i ax i b) 0
b
i 1
Solve for the a and b so that the previous two equations both =0
Rewrite these two equations
a x i2 b x i x i y i
a x i b * n yi
n
x i
x
x
i
2
i
b y i
a x i y i
We have the data points (xi, yi) for all i = 1..n, so we have all the summation terms in the
matrix form.
The unknowns are a and b
We already knows how to solve matrix
n
A
x i
x
x
i
2
i
b
X
a
yi
B
x i y i
AX = B
Using matlab, the coefficients a and b can be solved using matrix inversion
>> X = A-1*B
Note: A, B and X are not the same as a, b and x
Let us test this with an example:
I
X
Y
1
0
0
2
0.5
1.5
3
1
3
4
1.5
4.5
5
2
6
6
2.5
7.5
7.5 b 22.5
6
7.5 13.75 a 41.25
7.5 22.5
b
6
a inv 7.5 13.75 * 41.25
The solution is
b 0
a 3
f(x) = 3x+0
This fits the data exactly. That is, the error is zero. Usually this is not the outcome. Usually
we have data that doesnt exactly fit a straight line.
Example 2
Here is an example with some noisy data
x = [ 0 .5 1 1.5 2 2.5],
y = [-0.4326 -0.1656
3.1253
4.7877
7.5 b 20.8593
6
7.5 13.75 a 41.6584
7.5 20.8593
b
6
a inv 7.5 13.75 * 41.6584
b 0.975
a 3.561
4.8535 8.6909]
10
y = 3.5621x - 0.9761
R = 0.9334
6
x
Linear (x)
2
0
0
0.5
1.5
-2
2.5
Figure: Linear regression (trend) fitting for the data given in Example 2
10
8
6
x
Poly. (x)
2
0
0
-2
0.5
1.5
2.5
Figure: Polynomial (second order) regression fitting for the data given in Example 2