Project 1 Consider the following differential equations: dy = xy dx dy II. = y2 dx dy III. x + sin( x) y = x 2 dx I. 1. 2. 3. 4. Graph the direction field in the neighborhood of the origin Graph approximate integral curves for 10 different initial conditions (you can use dfield8.m) Solve the differential equation, using Euler's method (e.g. with euler.m), for three initial conditions and two values of the stepsize, h If possible compute the error, either using the exact solution (if available) or a very accurate numerical solution (from matlab) Please submit a write-up of your project, in electronic form. It should include a statement of the problem, tabular results, graphical results, summary and conclusion. You should also include printouts of any m-files you have written, or modified. You should also give proper citations to any links you may have used. Do your own work. I. Using matlab, we investigate the following differential equation dy = xy dx a) Direction Field. The direction field near the origin, − 5 ≤ x ≤ 5. − 5 ≤ y ≤ 5 , is shown below b) Integral curves. 10 different initial conditions are shown in the figure below Note that y = 0 is a critical point for the differential equation, but is unstable. c) Numerical solution. The exact solution for y (0) = 1 (found by integration factors or using seperability) is given by y=e x2 2 This can also be found using the Matlab command >> dsolve (‘Dy=x*y’, ‘y(0)=1’, ‘x’) The graph of the results of Euler’s method, with h=0.1 and h=0.01, is compared to the exact solution below Because of the convexity of the exact solution, the numerical solution given by Euler’s method always lies below the graph of the solution. The exact solution for y(0)=2 (found by integration factors or using seperability) is given by y = 2e x2 2 This can also be found using the Matlab command >> dsolve(‘Dy=x*y’, ‘y(0)=2’, ‘x’) The graph of the results of Euler’s method, with h=0.1 and h=0.01, is compared to the exact solution below The exact solution for y(0)=-1 (found by integration factors or using seperability) is given by y = −e x2 2 This can also be found using the Matlab command >> dsolve(‘Dy=x*y’, ‘y(0)=-1’, ‘x’) The graph of the results of Euler’s method, with h=0.1 and h=0.01, is compared to the exact solution below Because of the convexity of the exact solution, the numerical solution given by Euler’s method always lies above the graph of the solution. d) Error. Error, as a function of h. We fix the initial condition, and the interval [0,1], and then compute the error (defined as the maximum of the absolute value of the pointwise error over the interval) versus the step size h h=Step size 0.1 0.05 0.01 0.005 0.001 Error = max(abs(y-exact)) 0.10161 0.05278 0.01090 0.00547 0.00110 This results in an essentially linear graph of error vs. h. Summary. There is only one critical point for the differential equation dy = xy , given by y = 0 . The dx critical point is not stable. The exact solution can be found by the method of integrating factors; the method of seperability; or by using Matlab’s built-in dsolve command. The exact solution is given x2 2 by y = Ce , where C is determined by the initial conditions. Euler’s method converges linearly to the exact solution, in terms of step size, for a fixed interval. II. Using matlab, we now investigate the following differential equation dy = y2 dx a) Direction Field. The direction field near the origin, − 5 ≤ x ≤ 5. − 5 ≤ y ≤ 5 , is shown below b) Integral curves. 10 different initial conditions are shown in the figure below Note that y = 0 is a critical point for the differential equation, but is unstable. c) Numerical solution. The exact solution for y(0)=1 (found by integration factors or using seperability) is given by y= 1 1− x This can also be found using the Matlab command >> dsolve(‘Dy=y^2’, ‘y(0)=1’, ‘x’) The graph of the results of Euler’s method, with h=0.1 and h=0.01, is compared to the exact solution below Note, the exact solution has a singularity at x=1, so no matter how small the step size, Euler will never be close to the exact solution as x approaches 1! Also, because of convexity, the numerical solution given by Euler’s method lies below the exact curve. The exact solution for y(0)=2 (found by integration factors or using seperability) is given by y= 1 1/ 2 − x This can also be found using the Matlab command >> dsolve(‘Dy=y^2’, ‘y(0)=2’, ‘x’) The graph of the results of Euler’s method, with h=0.1 and h=0.01, is compared to the exact solution below The exact solution for y(0)=1/2 (found by integration factors or using seperability) is given by y= 1 2− x This can also be found using the Matlab command >> dsolve(‘Dy=y^2’, ‘y(0)=1/2’, ‘x’) The graph of the results of Euler’s method, with h=0.1 and h=0.01, is compared to the exact solution below Notice that since we are well away from the singularity (x=2), Euler’s method will converge to the exact solution. d) Error. Error, as a function of h. We fix the initial condition (y(0)=1/2), and the interval [0,1], and then compute the error (defined as the maximum of the absolute value of the pointwise error over the interval) versus the step size h h=Step size 0.1 0.05 0.01 0.005 0.001 Error = max(abs(y-exact)) 0.0578 0.0315 0.0068 0.0034 0.00069 This is essentially a linear graph Summary. There is only one critical point for the differential equation dy = y 2 , given by y = 0 . The dx critical point is not stable. The exact solution can be found by the method of integrating factors; the method of seperability; or by using Matlab’s built-in dsolve command. The exact solution is given by y = 1 , where C is determined by the initial conditions. Euler’s method converges linearly to the C−x exact solution, in terms of step size, for a fixed interval (as long as you are sufficiently far from the singularity). Without knowing the exact solution, it would be difficult to predict where the singularity would occur, even by an extremely accurate numerical solver. This is a fundamental problem of nonlinear differential equations. III. The final differential equation we consider is x dy + sin( x) y = x 2 dx or, in standard form, dy sin( x) = x− y dx x a) Direction Field. The direction field near the origin, − 5 ≤ x ≤ 5. − 5 ≤ y ≤ 5 , is shown below b) Integral curves. 10 different initial conditions are shown in the figure below c) Numerical solution. This equation has no closed form solution, or analytical solution (at least in terms of elementary functions). In order to get an accurate solution to compare to, we use the Matlab command ode45. This can be found using the Matlab command >> [t,y] = ode45(@rhs45,[1,10],[1]); where the listing of “rhs.m” is given in the Appendix. The graph of the results of Euler’s method, with h=0.1, is compared to the numerical solution (ode45) is shown below below Although it may look like the differential equation has a problem at x=0, the term sin(x)/x is bounded (and infinitely smooth) at x=0. This is why Euler’s method gives such good results. Summary. There are no critical points for the differential equation. There is no exact solution, in terms of elementary functions. Euler’s method converges to the exact solution, in terms of step size, for a fixed interval. Because of the smooth behavior of sin(x)/x near the origin, there is no singularity near x=0. Conclusions. In this project we examine three different ordinary differential equations – One linear (with an integrating factor), one linear (with no integrating factor), and one nonlinear. As long as the exact solution remains smooth and bounded, Euler’s method has no trouble converging to the exact solution. Convergence is linear in terms of step size. When no exact solution is known, as in the third example, we can still compare Euler’s method to a more sophisticated (and accurate) numerical method, such as ode45 which is built-in to Matlab. Matlab provides a power numerical and graphical tool for investigating the direction fields, integral (solution) curves, as well as approximate numerical solutions. In addition to allowing one to program custom numerical routines, Matlab comes with built-in numerical methods which work well. Appendix I. Program listings ---------------------------------------------------- Euler.m ------------------------------------------------------------function [tout, yout] = euler(rhs, t0, y0, h, N) % % % % % % % % % % % euler('rhs',t0,y0,h,N) integrates a single ordinary differential equation of the form y'=f(t,y) where the initial value of t = t0 and y(t0) = y0 the stepsize = h and the number of steps = N Note: the right hand side must be defined in the file rhs.m % allocate space and initialize to vectors to zero tout = zeros(N+1,1); yout = zeros(N+1,1); % set initial values tout(1) = t0; yout(1) = y0; % apply Euler's method for i=1:N tout(i+1) = tout(i) + h; slope = feval(rhs,tout(i),yout(i)); yout(i+1) = yout(i) + slope*h; end ---------------------------------------------------- rhs1.m ---------------------------------------------------function yprime = rhs1(x, y) % rhs returns a function which is the right hand side % of a first order differential equation % dy/dx = y' = f(x,y) yprime = x*y; ---------------------------------------------------- rhs2.m ---------------------------------------------------function yprime = rhs1(x, y) % rhs returns a function which is the right hand side % of a first order differential equation % dy/dx = y' = f(x,y) yprime = y^2; ---------------------------------------------------- rhs3.m ---------------------------------------------------function yprime = rhs3(x, y) % rhs returns a function which is the right hand side % of a first order differential equation % dy/dx = y' = f(x,y) yprime = x-sin(x)*y/x; ---------------------------------------------------- rhs45.m ---------------------------------------------------function [ydot] = rhs45(t, y) % rhs returns a function which is the right hand side % of a first order differential equation % dy/dx = y' = f(x,y) ydot = zeros(1,1); ydot(1) = t-sin(t)*y(1)/t;