You are on page 1of 10

CHE555 NUMERICAL METHODS & OPTIMIZATION

LAB 6: ORDINARY DIFFERENTIAL EQUATIONS

Task 1: CONDITIONAL STATEMENT

MATLAB conditional statements enable us to write programs that make decisions.


a) If end statement

Example:
i)

Write in m-file:

ii)

Save & Run

iii)

Answer in command window:

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 1

b) If else end statement

Example:
i)

Write in m-file:

ii)

Save & Run

iii)

Answer in command window:

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 2

c) If elseif end statement

Example:
i)

Write in m-file:

ii)

Save & Run

iii)

Answer in command window:

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 3

Task 2: LOOP STRUCTURES


A loop is a structure for repeating a calculation a number of times.
a) For loop - when the number of passes is known ahead of time

Example:
i)

Write in m-file:

ii)

Save & Run

iii)

Answer in command window:

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 4

b) While loop - when the looping process must terminate when a specified condition is
satisfied. The number of passes is not known in advance

Example:
i)

Write in m-file:

ii)

Save & Run

iii)

Answer in command window:

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 5

Task 3: TO SOLVE ORDINARY DIFFERENTIAL EQUATIONS USING EULER


METHOD
Procedure with example:
Solve the following numerical integration from x=0 to x=2 with 4 segment. The initial
condition is x=0 and y=1.

dy 1 x 1

dx 4 y
i)

Write in m-file:

ii)

Save & Run

iii)

Appear in command window file name eg: opseuler

iv)

Then, write in command window: file name('equation name',a,b,y0,n)


Eg: opseuler('feu',0,2,1,4)

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 6

v)

Answer in command window:

Task 4: TO SOLVE ORDINARY DIFFERENTIAL EQUATIONS USING RUNGEKUTTA METHOD


Procedure with example:
Solve the following numerical integration from x=0 to x=2 with 4 segment. The initial
condition is x=0 and y=1.

dy 1 x 1

dx 4 y
i)

Write in m-file:

ii)

Save & Run

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 7

iii)

Appear in command window file name eg: opskut2H

vi)

Then, write in command window: file name('equation name',a,b,y0,n)


Eg: opskut2H('kut2H',0,2,1,4)

iv)

Answer in command window:

Task 5: TO SOLVE ORDINARY DIFFERENTIAL EQUATIONS USING COMMAND


IN MATLAB
Procedure with example:
Solve the following numerical integration from x=0 to x=2 with 4 segment. The initial
condition is x=0 and y=1.

dy 1 x 1

dx 4 y
i)

Write in m-file:

ii)

Save & Run

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 8

iii)

Appear in command window file name eg: prog1

iv)

Then, write in command window:


- ode23(@file name,[a b],y0) for 2nd and 3rd order of Runge-Kutta
- ode45(@file name,[a b],y0) for 4th and 5th order of Runge-Kutta
Eg: ode23(@prog1,[0 2],1)

v)

Answer in command window:

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 9

vi)

Plotted graph:

Prepared by Madam Rabiatul Adawiyah Abdol Aziz (2016)

Page 10

You might also like