Introduction to Mathematical Software

21-126, Spring 2000
Dr. David Handron

Homework Assignment 3

In class we defined a rule that would approximate definite integrals using the Left Endpoint Rule:

Clear[Lft]
Lft[fctn_, a_, b_, 1] := (b - a)*fctn[a]
Lft[fctn_, a_, b_, n_Integer] := ((b - a)/n)*fctn[a] + 
        N[Lft[fctn, a + (b - a)/n, b, n - 1]]

We also defined a rule that would compute an approximate solution to a differential equation using Eulers Method:

Clear[Eul]
Eul[fctn_, y_, {tb_, tf_}, d_] := {} /; 
        tb ≥ tf
Eul[fctn_, y_, {tb_, tf_}, d_] := 
      Prepend[Eul[fctn, y + d*fctn[tb, y], {tb + d, tf}, d], 
          y + d*fctn[tb, y]] /; tb < tf

In this homework, we will examine a variation on each of these themes.

1) The Trapezoid Rule is a better way to approximate a definite integral.  Given a function
f(x), the approximate integral from a to b is given by dividing the interval [a,b] into n sub-intervals, and then adding up the areas of the trapezpods defined by

(x_i,0), (x_i,f(x_i)), (x_(i+1),f(x_(i+1))),(x_(i+1),0).

The usual formula for this sum is given by

(h/2)[f(x_0)+2f(x_1)+...+2f(x_(n-1))+f(x_n)],

Where h=(b-a)/n.

Use Mathematica to define a rule,Trap[fctn,n,a,b],that will compute the trapezoid rule approximation to a definite integral when given the function to integrate, the number of sub-intervals, and the endpoints a and b.  (Hint: Think about adding areas of trapezoids, rather than the usual formula.)

2) Use the rule Trap[] to approximate the integral of

x Exp[-x]

from 0 to 3.

3)We have seen how to use Euler's Method to compute a solution to the differential equation dy/dt=f(t,y).  A more efficient method is the Second Order Runge-Kutta method.  This method works much like Euler's Method, but the rule for calculating  y_(k+1) is a bit different:

y_(k+1) = y_k + (h/2)(f(t_k, y_k) + f(t_k, y_k+h f(t_k, y_k)))
t_(k+1) = t_k + h

Define a rule RK[f, yb, {tb, tf}, h] that will compute the Second Order Runge-Kutta approximate solution to a differential equation dy/dt=f(t,y),  between t=tb and t=tf, with y(tb)=yb and timestep h.

4) Use the rule RK to find an approximate solution to

dy/dt=y(3-y)

with

y(0)=.2, h=.1 and tf=10.

Use the command ListPlot[] to graph the solution.  (Hint: you may want to use the ListPlot Helper from the notebook extra.nb.)