Math1360: Computer Lab I Introduction Today we will learn how to use Matlab to solve some of the problems we have covered so far. As the first step you have to download to your account several subroutines I have listed on my website. Use a web browser, and go to the link http://www.math.pitt.edu/~swigon/math1360.html click on the files for the Lab and download them to your home directory. You will need the following files: flow.m – subroutine for plotting the 1-D flow diagram euler.m – subroutine for numerical solution of ODE by Euler method impreuler.m - subroutine for numerical solution of ODE by improved Euler method rungekutta45.m – subroutine for numerical solution of ODE by Runge-Kutta method Fixed points Fixed points of the first-order differential equation dx/dt = f(x) are the solutions of the equation f(x) = 0. Matlab contains several subroutines that can help us locate fixed points by 1) plotting the function f(x) ezplot('x^2') ezplot('-x^3+x^2+4*x-4',[-10 10]) 2) finding the solutions of f(x) = 0 fsolve(inline('x^2-1'),2) 3) plotting the flow of dx/dt = f(x) flow(inline('x^2-1'),[-2 2]) – plots the function x2 – plots the function –x3 + x2 + 4x – 4 over the interval –10 < x < 10 – finds a solution of the equation x2 – 1 = 0 that is closest to the initial guess x = 2 – plots the flow diagram for dx/dt = x2 – 1 in the interval –2 < x < 2 Problems For the following ODEs determine all fixed points (if possible) and their stability. a) dx/dt = 3*sin(x) – x b) dx/dt = x2 + e-x use exp(-x) for e-x x c) dx/dt = e – cos(x) d) dx/dt = x sin(1/x) Note Plotting two functions on the same graph: ezplot('x^2',[-2 2]) hold on ezplot('cos(x)',[-2 2]) Numerical solution of ODEs Methods for numerical solution of dx/dt = f(x) with initial condition x(t0) = x0 provide estimates of the values of x = x(t) at discrete points t1, t2, t3, etc. The simplest estimate is provided by the first term in Taylor expansion (linearization) about x(t): x(t + t) ≈ x(t) + f(x(t)) t This defines the Euler's algorithm: xn+1 = xn + f(xn) (tn+1 – tn) The points tn are usually assumed equidistant with tn+1 – tn = t. Solution found using Euler method can be visualized by plotting xn versus tn. Example: Find numerically approximate solutions of the initial value problem dx/dt = x, x(0) = 1, for various values of t and compare them with the exact solution. 1) Use the euler subroutine to compute approximate solution with x(0) = 1 and t = 0.1 on the interval [0, 5]. The values of tn are stored in the matrix T and xn in the matrix X. [T,X] = euler(inline('x'),[0 5],1,0.01); 2) Plot the graph of xn versus tn in red plot(T,X,'r') 3) On the same figure plot in blue the graph of the exact solution x(t) = et hold on ezplot('exp(t)',[0 5]) 4) Observe how the error is changing with increasing t by plotting on the same graph numerical solutions for t = 0.05, 0.1, 0.5. [T,X] = euler(inline('x'),[0 5],1,0.05); plot(T,X,'r') [T,X] = euler(inline('x'),[0 5],1,0.1); plot(T,X,'r') [T,X] = euler(inline('x'),[0 5],1,0.5); Problems Follow the same procedure for the following initial value problems: a) dx/dt = x (3 – x), x(0) = 0.1, – 1, 5 b) dx/dt = x2, x(0) = – 2, 2 c) dx/dt = –9x, x(0) = 5. (What is the smallest value of t at which numerical instability occurs?) More accurate algorithm is the improved Euler method in which first a trial step k is taken and then a slope f(k) is found at that point. The real step is taken with a slope equal to the average of f(xn) and f(k): k = xn + f(xn) t xn+1 = xn + t [f(xn) + f(k)]/2 Improved Euler method is included in the file impreuler.m Example Use the improved Euler method to calculate an approximate solution of the initial value problem dx/dt = x, x(0) = 1, for various values of t and compare them with solutions obtained using the Euler method. 1) Use the impreuler subroutine to compute approximate solution with x(0) = 1 and t = 0.1 on the interval [0, 5]. [T,X] = impreuler (inline('x'),[0 5],1,0.1); 2) Plot the graph of xn versus tn in red plot(T,X,'r') 3) On the same figure plot in green the solution obtained by using Euler's method hold on [T,X] = euler(inline('x'),[0 5],1,0.1); plot(T,X,'g') 5) On the same figure plot in blue the graph of the exact solution x(t) = et hold on ezplot('exp(t)',[0 5]) 6) Which method is more accurate? Repeat for different values of t. The improved Euler method is of order t2 while the original Euler method is of order t. This means that improved Euler method tends to make smaller error for a given stepsize. Problem The Euler method showed oscillatory numerical instability in solving the equation dx/dt = –9x, x(0) = 5 for certain values of t. Does the improved Euler method show a similar instability ? Or a different type of instability ? (Hint: obtain numerical solution for various values of t and observe the results.) Fourth-order Runge-Kutta method is frequently used as the method of choice in first attempts for a numerical solution of a first-order differential equation. As the name suggest the method is of order t4 and hence even more accurate than improved Euler method. (For description see the book.) It is included in the file rungekutta45.m. Problem Examine whether the numerical instabilities of Euler and improved Euler methods have been resolved in the design of the Runge-Kutta method. (Hint: obtain numerical solution of the problem dx/dt = –9x, x(0) = 5 for various values of t and observe the results.)