## Homework 7, due November 7, 2007.

Due Wednesday, November 7, 2007.
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Homework 7, due November 7, 2007.

Unless otherwise indicated, each problem that requires you to write a computer program must be done in C, C++, FORTRAN or another language explicitly approved by the instructor.
1. Write a computer program to determine an approximate numerical solution to
• using Euler's method, 2nd order R-K and 4th order R-K. Then cut the time step in half. Does the error behave as expected?
2. Write a computer program to determine an approximate numerical solution for 0<t<1.5 to
• Determine an appropriate time step by either determining the analytical solution or by continuing to decrease the time step until it appears that the solution no longer changes.
3. How to use Matlab.
Matlab has a built-in function called ode45() that is an implementation of the 4th order R-K method. As an example of how to use it, let's use it to numerically solve
• which is equivalent to the two first order equations
• To use ode45() you must create a new Matlab function. The easiest way to make a new function is to make a *.m file where the * is the the new function name.

Since we like to solve differential equations with f(x,t) in the equation, let's created a new function called f that needs an x and t as arguments. In this example, the following would go inside the file called "f.m" and the file f.m should be in the same directory in which you started Matlab:

Code: Select all

``````function xdot = f(t,x)
xdot = zeros(2,1);
xdot(1) = x(2);
xdot(2) = sin(t) - x(1) - 0.5*x(2);
``````
Note that the order in the first line, f(t,x) is important. You'll get the wrong answer with f(x,t) there. Now, to compute the solution, simply type the follwing at the Matlab prompt:

Code: Select all

``>> [t,x] = ode45(@f,[0 10],[1 2])``
This will give back a vector of times and a matrix for x. The first column of x will be x(1) and the second column will be x(2). The [0,10] is the time interval (t=0 to t=10) and the [1 2] are the initial conditions. To plot the answer, just type

Code: Select all

``>> plot(t,x(:,1))``
Note: if you want to call your function something other than f you have to change f in three places: the first line of the file f.m, the name of the file and the first argument of ode45().
• Use ode45() to find approximate numerical solutions for the differential equations in the first two problems and compare the result with your answer. You don't have to plot them on the same plot, but at least visually compare them to make sure you get the same answer.
4. Consider the rather innocent-looking differential equation
• 1. Show that
• is the solution to the differential equation.
2. Write a computer program to determine an approximate solution using the 4th order R-K method to this first order differential equation.
3. Submit a plot of the approximate solution and the exact solution for this equation for the time interval from t=-1 to t=1. Use trial and error to determine an appropriate step size by comparing your approximate numerical solution to the exact solution for different step sizes and ensure that the magnitude of the maximum error is less than 0.001. Hint the step size has to be pretty small! Be sure to submit your code as well.
4. Use the Matlab ode45() function to solve the equation. Submit your code and a plot of the solution and the exact solution. Does Matlab give the correct answer?
5. (5 points extra credit) Figure out how to tweak the tolerances in ode45(). Is it possible to get an accurate solution?
6. Write a matlab script to implement Euler's method to determine an approximate numerical solution to the above "innocent" equation. An example script using Euler's method to solve the equation
• would look like the following:

Code: Select all

``````>> x(1) = -6;
>> dt = 0.01;
>> t = 0:dt:10;
>> for i=2:length(t)
x(i)=x(i-1)+sin(t(i-1))*dt;
end
>> plot(t,x)
``````
Using this script and Euler's method. Approximately determine how long it would take for the script to run to return an approximate solution that is as accurate as your code from above.
5. Write a computer program using Euler's method to determine an approximate numerical solution to
• 1. Submit a plot illustrating the exact solution and the three numerically computed solutions for the case where the time step is equal to
• 0.5;
• 0.25;
• 0.125; and
• 0.01.
2. Explain any dramatic changes in the results that you observe.
Last edited by goodwine on Tue Nov 06, 2007 8:53 pm, edited 1 time in total.
Bill Goodwine, 376 Fitzpatrick
cknapick
Do we have to print out and hand in anything for the third problem?
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:
cknapick wrote:Do we have to print out and hand in anything for the third problem?
Yes, what it says at the very end. Unless it says otherwise, always submit plots and code.
Bill Goodwine, 376 Fitzpatrick
jsv2nd

### Problem 5.a

Does Problem 5.a require solving the ODE with Euler's method, 2nd Order R-K, and 4th Order R-K? If so, should there be four seperate plots for 5.a alone?

I was a little confused by the wording in the problem
goodwine