Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Numerical Approximation of Ordinary Differential Equations using Explicit Euler Method, Assignments of Mathematics

The solution and analysis of two problems related to the implementation of the explicit euler method for approximating the solution of ordinary differential equations. The problems involve finding the numerical approximation of the solution of the initial value problem defined by y′ = 3y and the analytical solution of the given differential equation y = vx. The document also includes the computer code for implementing the explicit euler method.

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-rmo
koofers-user-rmo 🇺🇸

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Math 5620 Homework #1 Solutions
1 Problems Assigned in Class
1.1 Problem 1. Implementation of Explicit Euler Method
In this problem, you were asked to create a routine that provides a numerical approximation of the
solution of the initial value problem defined by
y0= 3y
on the interval [1,3] with y(1) = 3.0. The analytic solution of the initial value problem is
y(t) = 3 e3t
You were asked to compute the approximation for a number of steps sizes. The numerical method
that was supposed to be used was the Explicit Euler method.
Note that in the following the initial condition is actually y(1) = 2.0 instead of that given in the
homework problem. I left this in the code to see if you are paying attention or not. If you notice
this comment, I will know that you are going through the solution materials.
The code written for this part of the assignment is in the second appendix at the end of this
document. The two parts in the simulation are summarized in the following sections and the results
from running the computer code are also given the first appendix.
1.1.1 Solution: Problem 1a.
In this part of the problem results were generated as the step size was decreased towards zero. The
results are shown in the first appendix. Since the solution is an increasing exponential function of
the form
y(t) = Ce3t
the worst error will occur at the end. For the initial condition used in the code the solution is of
the form
y(t) = 2
e3e3t
and when t= 3 the exact solution is approximately equal to
y(3) = 2
e3e9806.8576
The results in this case show that (at least at the end point) the solutions are converging to the
exact value as h decreases towards zero.
1.1.2 Solution: Problem 1b.
In this case, it is clear that as the time step increases the results mean virtually nothing. The
idea in this case was to show that choosing h small enough is important and that in order to get
anywhere with a globally first order method like Explicit Euler, the step size must be very small on
this type of problem.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Numerical Approximation of Ordinary Differential Equations using Explicit Euler Method and more Assignments Mathematics in PDF only on Docsity!

Math 5620 Homework #1 Solutions

1 Problems Assigned in Class

1.1 Problem 1. Implementation of Explicit Euler Method

In this problem, you were asked to create a routine that provides a numerical approximation of the solution of the initial value problem defined by

y′^ = 3y

on the interval [1, 3] with y(1) = 3.0. The analytic solution of the initial value problem is

y(t) = 3 e^3 t

You were asked to compute the approximation for a number of steps sizes. The numerical method that was supposed to be used was the Explicit Euler method. Note that in the following the initial condition is actually y(1) = 2.0 instead of that given in the homework problem. I left this in the code to see if you are paying attention or not. If you notice this comment, I will know that you are going through the solution materials. The code written for this part of the assignment is in the second appendix at the end of this document. The two parts in the simulation are summarized in the following sections and the results from running the computer code are also given the first appendix.

1.1.1 Solution: Problem 1a.

In this part of the problem results were generated as the step size was decreased towards zero. The results are shown in the first appendix. Since the solution is an increasing exponential function of the form

y(t) = Ce^3 t

the worst error will occur at the end. For the initial condition used in the code the solution is of the form

y(t) =

e^3 e^3 t

and when t = 3 the exact solution is approximately equal to

y(3) =

e^3 e^9 ≈ 806. 8576

The results in this case show that (at least at the end point) the solutions are converging to the exact value as h decreases towards zero.

1.1.2 Solution: Problem 1b.

In this case, it is clear that as the time step increases the results mean virtually nothing. The idea in this case was to show that choosing h small enough is important and that in order to get anywhere with a globally first order method like Explicit Euler, the step size must be very small on this type of problem.

1.2 Problem 2: Chapter 1, Problem 1

The following problems were assigned to help review the methods required to solve ordinary differ- ential equations analytically.

1.2.1 1a.

The first equation that was assigned is the following.

y′^ = − 2 x y^2

with the initial condition y(0) = 1.

1.2.2 Solution:

Separating variables results in the equation of differentials:

dy −y^2 = 2x dx

which can be integrated to find ∫ (^) dy −y^2

y = x^2 + C 1 =

∫ 2 x dx

Inverting gives the general solution

y(x) =

x^2 + C 1

and applying the initial condition gives

y(0) = 1 =

(1)^2 + C 1

which implies C 1 = 1 and the particular solution is

y(x) =

x^2 + 1

1.2.3 1d.

The second eqution that was to be solved in Problem 1 in the text was the following.

y′^ =

(1 − 20 y )y 4

with initial condition y(0) = 1.

1.3 Problem 3: Chapter 1, Problem 2

In this problem, the book suggests that there is a simple change of variables that will turn the differential equation into a simpler equation that is straight forward to solve. The problem wants a solutio for the equation

y′^ = y − x y + x

with the initial condition y(0) = 4.

1.3.1 Solution:

We start with the substitution y = vx where v is most likely a function of x. This means

y′^ = dy dx

d dx (vx) = v + x dv dx and

y − x y + x

vx − x vx + x

x(v − 1) x(v + 1)

v − 1 v + 1

Substituting into the differential equation gives

v + x dv dx

v − 1 v + 1

Solving for (^) dxdv results in the following differential equation

x dv dx

v^2 + 1 v + 1

Separating variables gives the following integral equation ∫ (^) v (^2) + 1 v + 1

dv = −

x

dx

Integrating both sides gives the following ∫ (^) v + 1 v^2 + 1 dv =

∫ (^) v v^2 + 1 dv +

v^2 + 1 dv

=

∫ (^2) v v^2 + 1 dv +

v^2 + 1 dv

= ln(v^2 + 1) + arctan(v) + C 1

and

x

dx = −ln(x) + C 2 = ln(x−^1 ) + C 2

Putting this together gives the implicit equation

1 2 ln(v^2 + 1) + arctan(v) = ln(x−^1 ) + C 3

or

1 2 ln(v^2 + 1) + arctan(v) = ln(x−^1 ) + C 3

This is an implicit form of the general solution and may be about as far as we can go based on the logarithms and inverse tangent functions. We need to get back to the original variables and since y(x) = vx we have v = y(x) and the solution becomes

ln(

√ (y/x)^2 + 1) + arctan(y/x) − ln(x−^1 ) = C

Then

ln(

√ (y/x)^2 + 1) + arctan(y/x) + ln(x) = ln(x

√ (y/x)^2 + 1) + arctan(y/x) = ln(

√ y^2 + x^2 ) + arctan(y/x) = C

In the limit as x tends to zero we find out that

C = limx→∞

( ln(

√ y^2 + x^2 ) + arctan(y/x)

) = 2 ln(2) + π 2

and the particular solution is given by

ln(

√ y^2 + x^2 ) + arctan(y/x) = 2 ln(2) + π 2

and we find out that C 2 = 1. Then the first step gives

y(x)′^ =

y^2 + 1

or

(y^2 + 1)dy = 2dx

Integrating gives

1 3

y^3 + y = 2x + C 3

Using the initial condition y(0) = 0 gives C 3 = 0. Finally, the particular solution for this problem is given by y^3 + 3y = 6x

A Results from Problem 1:

B Computer Code:

The code in this appendix represents one way to put together code that can be used to compute the solution of an initial value problem using the Explicit Euler Method. The code is written into Java objects with the idea that the code will be reusable. So, the code is written into a number of objects that allow reusing the objects with other methods that will be constructed down the line.

B.1 Problem 1: Explicit Euler Method Code

This is the simplest method used in the numerical approximations of the solution of initial value problems. These same ideas will be extended when we develop numerical methods for the heat equation and other evolutionary processes of interest.

B.1.1 ExplicitEulerMethod Object

This object is the starting point for the approximate solution. This object given below includes the actual initialization and computation. In the end, the object produces a pair of arrays that define the ordered pairs for the approximations. The code uses a uniform step size and was used to approximate the solution of the initial value problem in Problem 1 using a variety of step sizes to see the effect of the step sizes.

// // Author: Joe Koebbe // A-Number: A00xxxxxxx // Creatation Date: 1/15/ // Last Modification Date: 1/16/ // // Purpose: This object will apply the first order explicit Euler method for the // approximate solution of initial value problems of the form: // // y’(t) = f(t,y(t)) // y(t0) = y // // Input data for the object: // // f(t,y) - this function provides the definition in the IVP // given above and must provide an output for each // t and y in the domain of the function. // t0 - the initial point must be specified for the IVP // tf - the end point in the definition of the IVP // y0 - the initial solution value. // h - this is the step size specified by the user. // public class ExplicitEulerMethod extends Object { public ExplicitEulerMethod(FunctionObject f, double t0, double tf,

double y0, double h) { function = f; if(f == null) function = new Problem1Function(); tleft = t0; tright = tf; yinit = y0; stepSize = h; computeApproximateSolution(); } public double [] computeApproximateSolution() { // // initialize the temporary variables for the domain in t and y // double tval = tleft; double yval = yinit; // // compute the number of steps to take based on the input step size // FixedNumberOfSteps fnos = new FixedNumberOfSteps(tleft, tright, stepSize); int n = fnos.getNumberOfSteps(); // // create enough storage for the arrays // yapprox = new double[n+1]; grid = new double[n+1]; // // store the initial values for the approximate solution and initial point // yapprox[0] = yinit; grid[0] = tleft; // // compute the approximation using the explicit euler method // for(int i=1; i<= n; i++) { yapprox[i] = yapprox[i-1]

  • stepSize * function.evaluate(grid[i-1], yapprox[i-1]); grid[i] = grid[i-1] + stepSize; if(grid[i] > tright) grid[i] = tright; } // return the approximate value - this is a bit redundant in this object, // but might be helpful return yapprox; } // // for printing and error calculations, the user may need to use the grid or // mesh used for the independent variable

// local variables used in the approximate solution process - these are set to // values that will avoid problems in the code // // the following variables set the data needed from the definition of the // initial value problem including the function, left and right endpoints of // the domain on which the approximation will be obtained. the // private FunctionObject function = null; // // indepndent variable domain values private double tleft = 0.0; private double tright = 1.0; // // local storage for the initial value for the problem // private double yinit = 0.0; // // the following are variables associated with the numerical method being // applied. this includes the desired step size, along with grid and // arrays containing the approximate solution produced. // private double stepSize = 0.01; private double [] grid = null; private double [] yapprox = null; // }

B.1.2 Other Objects used in the Assignment

// // Object Name: Problem1Function // // Author: Joe Koebbe // A-Number: A00xxxxxxx // Creatation Date: 1/15/ // Last Modification Date: 1/16/ // // Purpose: This object provides the function values on the right hand side of // of the initial value problem defined by // // y’(t) = f(t,y(t)) // y(t0) = y // public class Problem1Function extends FunctionObject { public double evaluate(double t, double y) { // // change this value when changing the differential equation - do not // forget to change the analytic solution to match

// // the value here is for the homework problem in the first assignment in // Math 5620 // double val = 3.0 * y; return val; } public double derivative(double t, double y) { // // change this value when changing the differential equation - do not // forget to change the analytic solution to match // // the value here is for the homework problem in the first assignment in // Math 5620 // double val = 3 * y * y; return val; } }

// // Class Name: FixedNumberOfSteps // Class Type: Java Object // Extends : NumberOfStepsObject // // Author: Joe Koebbe // A-Number: A00xxxxxxx // Creatation Date: 2/7/ // Last Modification Date: 2/7/ // // Purpose: This object will be used to set a fixed number of steps in the // solution of the initial value problem // // y’(t) = f(t,y(t)) // y(t0) = y // // Input data for the object: // // t0 - the initial point must be specified for the IVP // tf - the end point in the definition of the IVP // h - this is the step size specified by the user. // // Output data produced by the object: // // nsteps - an int value that contains the number of steps // to use for the numerical methods being used. //

// // the right endpoint of the interval of interest in the problem. this must // be a finite value even thought the initial value problem solution is likely // to be defined for all t > 0 // private double tright; // // the number of steps provided by the object that instantiates this object // private int nsteps; // // the contant time step determined when the object is instantiated // private double stepSize; // }