# Algorithms and Computer Programming with MATLAB

First Semester 2010-2011

COMPUTER PROJECT#1

Due on Tuesday, Spetember 13th, 2011

As this is the first program, the solution is provided. Most of the code details will be covered in class, as we progress. Therefore, at this point don’t worry if you don’t understand completely all the statements. The objective of this project is just to be able to edit, compile and run a full MATLAB program while developing the skill of writing code without syntax errors. The code must be written as a new M-file. Please note that there are two problems with two solutions each and that the solutions using array variables are more elaborated.

RUNGE-KUTTA METHOD WITH MATLAB

The Runge-Kutta method is one of the most popular methods for solving ordinary differential equations (ODEs) by means of numerical approximations. There are several version of the method depending on the desired accuracy. The simplest Runge-Kutta method is of second order, which in simpler terms means that the error is of the order of h2 ,abbreviated as O(h2).

Considering that the student has a limited background on differential equations, in this project we will attempt to solve a simple differential equation with the simplest Runge-Kutta method available. Even if the student does not have yet a formal course on differential equations, he will be able to understand the numerical solution process with his background on integral calculus. The formulas for the second-order Runge-Kutta are

[pic]Eq#1

[pic]Eq#2

[pic]Eq#3

where

h=(b-a)/n, the step size of the independent variableEq#4

n=the number of values of the independent variable where we wish to calculate the approximate solution [a,b] = the limits of the x interval.

k1, k2, Runge-Kutta functions

f(xi,yi)=function evaluated at xi and yi ,like wise

[pic]function evaluated at [pic]

Note that in order to calculate the solution yi+1 you need the previous solution yi and k2; to calculate k2 you need first k1. Then, having the initial condition yi , the calculation sequence is k1, k2, and then yi+1.

Example

Consider the differential equation [pic] on [pic] where

[pic] Eq’s #5

For simplicity and in order for the student to understand better the calculation process, first we find the approximate solution for n=2 (i.e., for only two points of the independent variable x). This small value of n produces a very large step size h=(1-0)/2=0.5.

The approximation at (the second point of x) [pic] is

[pic]

[pic]

[pic]

Next, we find the approximate solution for the third point y2 at point [pic] (we can also use [pic]

[pic]

[pic]

[pic]

An Excel program can be implemented for the first 10 points (n=10) and for [pic]. Note that the first point (y1) is given as an initial condition. Therefore there will be 11 values of x and y. The step size is h=(1-0)/10=0.1.

[pic]

Some inserted values in key cells are shown below:

C2: =0

C3: =1

C4:=10

C8:=0

F8:=2

G8:=3*EXP(C8)-C8-1

H8: =Abs((F8-G8)/G8*100)

Some formulas in key cells are shown below:

C5: =(C3-C2)/C4

C9: =C8+$C$5

D9: =$C$5*(C8+F8)

E9: =$C$5*(C8+$C$5/2+F8+D9/2)

F9: =F8+E9

G9: =3*EXP(C9)-C9-1

H9: =Abs((F9-G9)/G9*100)

The exact solution for this problem is [pic]. This equation was used to compute the solution (y analytical) in the previous to the last column of the above table for the same values of x in the second column. A graph of the computed points of y for the numerical solution and the exact solution versus x is shown below:

[pic]

The MATLAB results for both problems will be reported in external files using a format similar to the table above. After running the codes, these external files can be found in the same folder in which you stored your source program. You can open them with MATLAB or with Word. With MATLAB: when the open window appears, change the “File of...

Please join StudyMode to read the full document