Sie sind auf Seite 1von 15

Introduction to Computational Engineering

Min Long

OUTLINE OF THIS CLASS AND NEXT


Solution to Simultaneous Linear Equations
Simultaneous Linear equations
Direct Methods
Gaussian Elimination (straight forward, fast)
LU Decomposition (not very straight forward, fast)

Indirect/Iterative Methods
Not required
But good supplements

Introduction to Computation

SOLUTION TO LINEAR EQUATIONS


Simultaneous sets of linear equations
Widely used in science and engineering. Example: Circuit of
resistors, Lorentz Trans
Example:

Goal of this class: solve such eqns analytically and

numerically. In-class Example:


Introduction to Computation

SOLUTION TO LINEAR EQUATIONS


Example

Matrix Form

Solution:
Step 1: elimination
Eqn (2)-0.5 x Eqn (1)

Step 2: back substitution


Substitute y=0 back to Eqn (1)

Introduction to Computation

SOLUTION TO LINEAR EQUATIONS


General form of simultaneous linear equations

Uniqueness of Solution
A system of n linear equations in n unknowns has a unique
solution, if the determinant of the coefficient matrix is nonsingular.
It means: no row is a linear combination of other rows.

Introduction to Computation

SOLUTION TO LINEAR EQUATIONS


Direct Methods of Solution
Solution:
Step 1: elimination
Step 2: back substitution

Transform the eqns into equivalent equations


Solution unchanged under the operations:
Reordering: exchange 2 equations
Multiplying: multiply an eqn by a nonzero number
Subtracting: subtract 2 equations

Gauss Elimination
Elimination phase: obtain an upper triangular matrix U
Back substitution phase: Ux=c can be easily solved by back substitution
Introduction to Computation

SOLUTION TO LINEAR EQUATIONS


General algorithm for Gauss Elimination
Matrix A is of n by n elements
Assume the first k rows (k could be from 1 to n-1) of A have already
been transformed to upper triangular format
The current row (kth row) is called the pivot row
Elimination phase: k+1 th row to nth row need to be transformed

To be eliminated

Introduction to Computation

SOLUTION TO LINEAR EQUATIONS


Coding

for k = 1:n-1
for i= k+1:n
if A(i,k) ~= 0
lambda = A(i,k)/A(k,k);
A(i,k:n) = A(i,k:n) - lambda*A(k,k+1:n);
b(i)= b(i) - lambda*b(k);
end
end
end

To be eliminated

Introduction to Computation

SOLUTION TO LINEAR EQUATIONS


Back Substitution
After elimination

Solution to last eqn


Solution to kth eqn

for k = n:-1:1
b(k) = (b(k) - A(k,k+1:n)*b(k+1:n))/A(k,k);
end
Introduction to Computation

SOLUTION TO LINEAR EQUATIONS


Complete Algorithm for Gauss Elimination
A=[2,1;1,2];
b=[2,1]';

% set of equations

n=length(b)

% number of equations

for k = 1:n-1
for i= k+1:n % Elimination Phase
if A(i,k) ~= 0
lambda = A(i,k)/A(k,k);
A(i,k:n) = A(i,k:n) - lambda*A(k,k:n);
b(i)= b(i) - lambda*b(k);
end
end
end
for k = n:-1:1
% Back subsitution
b(k) = (b(k) - A(k,k+1:n)*b(k+1:n))/A(k,k);
end
disp(b)

% solution
Introduction to Computation

10

SOLUTION TO LINEAR EQUATIONS


Complete Algorithm for Gauss Elimination: Another way
Example and Exercise:
A=[8 -6 2
-4 11 -7
4 -7 6];
b=[28, -40, 33]';
[x, det]=gauss(A,b)

gauss is a user defined function provided by the textbook (p38),

saved as a .m file in your current working folder so that you can


use it (think why)
Read the gauss.m
Write a main code to call the gauss.m, to solve the Ax=b equations

Introduction to Computation

11

SOLUTION TO LINEAR EQUATIONS


One more step: Row Pivoting (Reordering)
Gauss Elimination fails in solving

But works in solving

Reordering:
Find the s=abs(maximum element) for each row, the relative size of any

element would be divided by s


Find the row with largest relative size of rows to be processed,
interchange it with the current pivot row
Introduction to Computation

12

SOLUTION TO LINEAR EQUATIONS


One more step: Row Pivoting (Reordering)
In-class Exercise:

Use your previous Gauss Elimination code (no pivoting) to solve the

above equations. Whats error information?


Read the textbook Ch2.5
Save gaussPiv function and swapRows functions into some .m files in an
appropriate way
Write a main code to call the above functions to solve the above equations.
Confirm Pivoting works.

Introduction to Computation

13

SOLUTION TO LINEAR EQUATIONS


MATLAB function
A\b
Example:
A=[8 -6 2
-4 11 -7
4 -7 6];
b=[28, -40, 33]';
x=A\b

Introduction to Computation

14

READING ASSIGNMENTS
This note
Ch2.1-2.3, 2.5

Introduction to Computation

15

Das könnte Ihnen auch gefallen