You are on page 1of 13

CHAPTER 1 INTRODUCTION TO NUMERICAL METHODS In engineering, it is often necessary to solve complicated mathematical problems.

Many of these problems do not have known analytical solutions. Numerical solutions are usually needed in these situations. This chapter introduces the motivation, the major areas, and the implementation of numerical methods. I. Motivation There are many complicated mathematical problems with no easy analytical solution using exact mathematical derivations. We usually encounter the following types of problems: A. Transcendental equations It is difficult to solve an equation that cannot be decomposed to simple algebraic operations. For example, we know how to solve
9 x2 =3 x

and we also know how to solve


tan x = 0. 2

But how about the following equation?


x 9 x2 tan = 2 x

This is called a transcendental equation, which means that its solution cannot be expressed in a simple mathematical form.
EE 3108 Semster B 2008/2009 1-1 S C Chan

B. Large number of variables Simple problems become increasingly difficult when the number of variables increases. We can easily solve the circuit on the left using Kirchoffs circuit laws:

i0 i1 V R1
V R2 V i1 = R1 i0 = i1 + i 2 i2 =

i2 R2

However, the problem gets more difficult as the number of components (thus, the number of current variables) increases. For instance, consider the 741 op-amp on the right. Engineers are eventually forced to use numerical simulation for designing computer processors.

1-2

C. Dynamical equations A system in state x can evolve over time t. The evolution is referred to as the dynamics of the system, which can be described by the dynamical equation:
dx = f (x ) dt

for a given function f(x). Consider a discharging capacitor:


dx = i dt dx V = dt R dx = ( RC ) 1 x dt
i Charge: R V

Here,

f ( x ) = ( RC ) 1 x

is a simple linear function of x. The


t RC

solution is simply x = x(0)e

In other systems, f(x) can be nonlinear. For example, some nonlinear problems are:
dx x 2 x3 x 4 x5 dt = + 2 6 + 24 120 dx = x + (e x 1) dt dx = sin x dt

Nonlinear dynamical equations are difficult problems for analytical methods.

1-3

D. Wave equations
E
Uniform medium

Source

Varying medium y g

Source

Another common type of equations in engineering is the wave equation. It describes water waves, sound waves, quantum mechanical electron/matter waves, and electromagnetic waves. It takes the form of:
d 2 E ( x) = k 2 E ( x ) , 2 dx

where k is called the propagation constant and it is a property of the medium. The solution can be verified to be:
E ( x ) = E0 cos( kx + 0 )

but the problem becomes extremely complicated if k is a function of x. The equation is difficult when the parameters vary or when there are boundary conditions.

1-4

This course is about numerical methods. Numerical methods are required to solve the complicated problems such as theose listed above. In general: Numerical methods are mathematical techniques for solving problems that have no analytical solution. Numerical methods produce solutions in numbers, instead of symbols and formulae. Numerical methods are implemented in precise steps called algorithms. Numerical methods can be studied under the realm of mathematics called numerical analysis, where the nature of numerical stability, convergence, and error are studied.

1-5

II. Topics to be covered Numerical errors the limitations of numerical methods Root finding search for roots of a given function Linear systems deal with linear but large systems Curve fitting obtain a function from some given points Numerical calculus Differentiation and integration Ordinary differential equations Equation with derivatives of one variable Partial Differential Equations Equation with derivatives of multiple variables Optimization Optimization of engineering designs

1-6

III. Tools for calculation Matlab (Matrix Laboratory)

Developed since the late 1970s Efficient implementation of matrix and vector manipulations Available: a) In CS-lab machines, student terminal room... b) Student version (http://www.mathworks.com) Similar products: Octave (http://www.octave.org) The following is a list of commonly used Matlab commands. It is difficult to remember all of them at once, but they are helpful for future reference.
1-7

A. Matrix assignment Variables are treated as matrices in Matlab. Examples:


A = [1 2 3] a = [2 3 4] B = [5; 6; 7] %This is a row vector, a 13 matrix %MATLAB is case-sensitive

%This is a column vector, or a 31 %matrix B = [5; 6; 7] %This gives the same result C = [0: 2: 10] %A row vector from 0 to 10 in steps %of 2 D = linspace(3, 5, 20) %Linearly spaced row vector from 3 %to 5 containing 20 elements. M = [1 2; 4 5] %A 22 matrix N = ones(3, 2) %A 32 matrix of 1s N = [6 7; 8 9] %Overwrite N

Note that we can use ; to stop the echoes. The % signs are for comments, which are ignored by Matlab.

1-8

B. Matrix operations -variables in Matlab follow the rules of matrix operation, but we can specify element-by-element operation when using the dot .. Examples:
M+N M*N N*M %Simple addition %N is pre-multiplied by M %M is pre-multiplied by N. %Note that the resultants are %equal. %Element-by-element operation %This squares each element of M. %This is an equivalent method %This squares the whole matrix M %This also does the same %Basic functions can be %operated/applied on a matrix %Square root

not

M.*N M.*M M.^2 M*M M^2 sin(M) sqrt(M)

1-9

C. Scalar operations Scalars are treated as 11 matrices in Matlab, but they can also operate directly on matrices. There are also several built-in scalars, such as pi, i and j. Examples:
pi i j i*i sqrt(-1) sqrt(1+i) 2*M 2+M %This gives the value of pi = 3.14 %i denotes the imaginary i = 1 %same as i %gives -1 %gives i %complex operation %The matrix is doubled %Element-by-element addition %(all increase by 2)

Note that the built-in scalars can be overwritten. For example, j = 2008 will overwrite j so that it is not 1 anymore.

1-10

D. Plotting Curves are plotted using row or column vectors. Examples:


x = [0: 0.01: 5*pi]; y1 = sin(x); y2 = cos(x).*exp(-x/5) plot(x, y1) ; hold on; plot(x, y2, r); ; %Blue color is the default %Hold the previous curve %Plot in red

To plot in multiple panels, we can use subplot(m, n, k) to divide the screen into mn subpanels. The curves will be plotted in the k-th subpanel. Other commonly used commands for plotting include:
title(This is a XY plot); xlabel(This is the X-axis); ylabel(This is the Y-axis); axis([0 5*pi -1 1]);

For 3-dimensional plots, try the function mesh. Also, try to find out how to use it by typing: help mesh

1-11

E. Script files Script files of Matlab are also called m-files (their file extension is .m). You have to save the file first, then type the file name in Matlab. F. Function files User-defined functions can be written in to following syntax. Remember to use the filename as the function name. Example:
%A user defined function that %calculates g(x) = cos(x)-sin(x) function out = g(x) temp1 = cos(x); temp2 = sin(x); out = temp1-temp2;

G. For-loops For-loops can be used in repeated execution. Example:


%The following sums up integers %from 1 to 100: sum = 0; for counter = 1:100 sum = sum + counter end

H. Conditions The typical ifelseend construct can be used.

1-12

Summary (Chapter 1)

This course is about solving engineering problems using numerical methods: 1. Numerical methods are necessary because analytical solutions are often too difficult. 2. Numerical methods deal with problems such as finding roots, curve fitting, calculus and differential equations. 3. Numerical methods can be implemented on a computer and Matlab is a convenient choice of software.

1-13

You might also like