MATH1052 Semester 1 2001

Practical Exercise 7

 

Euler’s method can be coded in MATLAB using for loops. While Euler’s method often looks good, it tends to become inaccurate after several thousand steps (depending upon the specific differential equation.) In this and subsequent weeks, we will use MATLAB to numerically integrate ordinary differential equations.

Assignment

  1. The solution to the differential equation
  2. over the interval 0 < x < 7.5 subject to the initial condition f(0) = 2 can be evaluated with MATLAB using for loops. We are interested in using Euler’s method for various intervals Dx. If we number our set of x and f values with a subscript n, then Euler’s formula for this differential equation becomes

    We now wish to divide our x interval into N steps and to run this formula forward while saving all the information along the way into arrays. To drive this formula forward in Dx = 0.25 increments, we first calculate the number of increments N = 7.5/Dx and then enter the following code which calculates the x and f values and stores them in two arrays of size N+1 (31 for the increment size Dx = 0.25):

    >> x(1) = 0;

    >> f(1) = 2;

    >> N = 30;

    >> for n = 2:N+1, x(n) = x(n–1) + 7.5/N; end

    >> for m=2:N+1, f(m) = f(m–1) + (7.5/N)*(x(m-1)^3-f(m-1)^2); end

    >> plot(x,f)

    The first two lines enter the initial value of x and f into the first elements of the arrays, while the for loop then increments m and stores the latest value of f into the array as element number m. The final command draws a graph of the f versus x. Repeat this summation for Dx = 0.1 and 0.05. Change the names of your horizontal and vertical variables to x2, f2 and x3, f3 when you run the summations for the second and third times, then plot these approximations on one graph with

    >> plot(x2,f2,x3,f3)

  3. Use Euler’s method to solve the differential equation

with f(0) = 1 over the interval 0 < x < 10 for Dx = 0.2, 0.04, and 0.01 and plot these approximations on a single graph.