Math 308, Matlab assignment 2

advertisement
Math 308, Matlab assignment 2
due February 12 in class
1. In the initial value problem
dy
= −x/y,
y(0) = 5,
dx
use Euler’s method to compute approximations to y(5) with 1, 5, 50 steps. Note that you will
need to determine the step sizes yourself. Plot the results on one plot. Label the plot and the axes
appropriately.
The syntax is
[x, y] = eul(f, [x0 , x1 ], y0 , h)
or
[x, y] = eul(@function, [x0 , x1 ], y0 , h)
where f is an inline function or function an M-file function, [x0 , y0 ] is the initial condition, x1
is the value of x where you want to compute the solution, and h is the (optional) step size. Before
running eul, you will need to create a function f (x, y) = −x/y. There are two ways to do this.
You can create an inline function, by typing
>> f = inline(’-x./y’,’x’,’y’);
(note the use of vectorized division ./). This is quick, but the function will be erased at the end of
the Matlab session. If you want to use it next time, create an M-file: edit opens the Matlab editor,
in which you type
function yprime = minusxovery(x,y)
yprime = -x./y;
and save the result as minusxovery.m (of course you can call the function, and the file, anything
you want as long as you remember what it was).
plot(x,y) plots entries of the vector y versus entries of the vector x. plot(x1,y1,x2,y2)
plots two graphs on the same plot. They are automatically done in different colors. You can also
modify the style of each graph: try plot(x1,y1,’x:’,x2,y2,’--’) and use help plot
for more details. You can also add a new graph to the previous plot by using hold on / hold
off commands. Use legend(’first graph’,’second graph’) to label the graphs,
xlabel(’x’), ylabel(’y’), title(’Title’) to label the axes and the figure, and
grid on to add a grid if you want.
The relevant Maple command is dsolve with options numeric and the Euler method designation
method=classical[foreuler]. Method rk4 is the 4th order Runge-Kutta; omitting the
method uses the default procedure, the analog of Matlab ode45.
2. Redo Problem 1 with
y 0 (x) = y 2 − x,
y(0) = 0.5
to estimate y(10) using 10, 20, 40 steps of Euler’s method (don’t forget to use .ˆ2 not ˆ2). Plot
the result. Which number of steps seems sufficient to provide the correct result?
1
√
3. The differential equation in Problem 1 is separable; verify that its solution is 25 − x2 . Use
the fourth order Runge-Kutta method rk4 on the interval [0, 4] with 10, 40,
√ 100, 400, 1000 steps to
approximate it. In each case, compute the error by using max(abs(y − 25 − x.ˆ2)). Plot the
errors versus the step size on a log-log plot (using loglog instead of plot). Note that you can
make a bunch of numbers e1 , e2 , e3 into a vector by writing e = [e1 , e2 , e3 ]. Do the points lie close
to a line? If so, that means that the error is proportional to a power of the step size. By dividing the
error by the fourth power of the step size, approximate the constant C in the expression
Error = C · step size4
which is valid for small step sizes.
4. In the previous problem, use the Runge-Kutta approximation with 1000 steps on the interval
[0, 5]. What is the error? How does it compare with the errors in the preceding problem? What
happens to the solution at x = 5 to explain this?
5. The initial value problem
y 0 = 2xy 2 ,
y(0) = 1
is solvable. Find y(0.99). Run the default eul, rk4, ode45 (without step size parameters) and
compare their predictions to the correct value. Why is there a problem? Now run the solvers for,
say, y(1.01). You should see that ode45 detects a problem and tells you not to trust its results.
Download