Homework 7, due November 7, 2007.
Posted: Fri Nov 02, 2007 4:37 pm
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.
- Write a computer program to determine an approximate numerical solution to
- Write a computer program to determine an approximate numerical solution for 0<t<1.5 to
- 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
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: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
function xdot = f(t,x) xdot = zeros(2,1); xdot(1) = x(2); xdot(2) = sin(t) - x(1) - 0.5*x(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 typeCode: Select all
>> [t,x] = ode45(@f,[0 10],[1 2])
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().Code: Select all
>> plot(t,x(:,1))
- 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.
- Consider the rather innocent-looking differential equation
- Show that
- Write a computer program to determine an approximate solution using the 4th order R-K method to this first order differential equation.
- 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.
- 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 points extra credit) Figure out how to tweak the tolerances in ode45(). Is it possible to get an accurate solution?
- 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
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.
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)
- Write a computer program using Euler's method to determine an approximate numerical solution to
- 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.
- Explain any dramatic changes in the results that you observe.