Computational Lab in Physics: Solving Differential Equations II. The Runge-Kutta method is an improvement on the Euler method, which gives a better approximation to the solution of the Diff. Eqn. Taylor expansion: rederive Simple Euler method Simple Euler, Modified Euler, valid to O(h), can improve the accuracy. h2 yn 1 y ( xn h) yn hy 'n y ''n O(h3 ) 2 Simple Euler Method Method can be expressed as: y 'n f ( xn , yn ) yn1 yn hf ( xn , yn ) Where xn and yn are known, and the differential equation under consideration specifies f(xn,yn) 2 2nd Order Runge-Kutta: Rederive Midpoint Euler Method, Part I Assume yn+1/2 is known. Next: Expand in a Taylor series: First Use Dx=-h/2 Expand around yn+1/2 to estimate yn. Keeping terms up to 2nd order. h (h / 2) 2 yn yn 1/ 2 y 'n 1/ 2 y ''n 1/ 2 O(h3 ) 2 2 Now use Dx=+h/2, expand around yn+1/2 to yn+1. h (h / 2) 2 yn 1 yn 1/ 2 y 'n 1/ 2 y ''n 1/ 2 O(h3 ) 2 2 3 Rederive Midpoint Euler Method, Part II - Subtract the two: h (h / 2) 2 yn 1 yn 1/ 2 y 'n 1/ 2 y ''n 1/ 2 O(h3 ) 2 2 2 h (h / 2) yn = yn+1/2 - y 'n+1/2 + y ''n+1/2 + O(h3 ) 2 2 yn+1 - yn = 0 + hy 'n+1/2 + 0 + O(h3 ) We get rid of the 2nd Order terms! But, hey! We don’t know y’n+1/2. Approximate it using simple Euler method. h yn 1/ 2 yn y 'n 2 y 'n 1/ 2 f ( xn 1/ 2 , yn 1/ 2 ) or k1 yn f ( xn , yn ) k2 yn 1/ 2 f ( xn 1/ 2 , yn 1/ 2 ) 4 Graphically, 2nd Order Runge-Kutta: Calculate k1=y’n=f(xn,yn) Esimtate: Midpoint: yn+1/2 k2=y’n+½=f(xn+h/2,yn+hk1/2) y’n+1/2 y’n Propagate through. yn+1=yn+hk2 5 2nd Order Runge-Kutta (midpoint method) What we have done: 1) Use the known derivative at xn. Obtained y’n from xn and yn, define it as: y’n = f(xn, yn) = k1 2) Estimate the midpoint x and y. Obtained from xn+ ½ = xn+h/2, yn+ ½ = yn+(h/2)y’n; and y’n=k1 from step 1) Estimate midpoint derivative y’n+½ = f(xn+h/2, yn+(h/2)* k1)=k2 3) Propagate through the whole interval. yn+1 = yn + hk2 , with k2 from step 2). 6 4th Order Runge-Kutta We can iterate again, and cancel higher and higher orders. Most commonly used algorithm: k1 f ( xn , yn ) hk1 h k2 f xn , yn 2 2 hk2 h k3 f xn , yn 2 2 k4 f ( xn h, yn hk3 ) k k k k yn 1 yn h 1 2 3 4 O (h5 ) 6 3 3 6 7 Example • Setup: y’ = y/x – x2/y2 Initial condition Step size: y(1)=1 h=0.01 Range 1<x< 3 e // Range and step size double xmin=1.0; double xmax=pow(exp(1.),1./3.); double step=0.01; int Nsteps= static_cast<int>((xmax-xmin)/step); const int maxPoints = 10000; if (Nsteps>maxPoints) { cout << "Need to declare array of larger size: " << Nsteps << endl; return; } // Arrays to hold all x and y values. double yVal[maxPoints]; double xVal[maxPoints]; // initial condition xVal[0] = xmin; yVal[0] = 1; 8 Applying R-K Method Example: Solving y’=(y/x)-(x2/y2). Algorithm. for (int i=0; i<Nsteps; ++i) { xVal[i+1]=xVal[i]+step; double xValhalf=xVal[i]+step/2.; //x_midpoint //calculate k1 = y’ = y/x – x*x/y*y double ky1=yVal[i]/xVal[i]-(xVal[i]*xVal[i])/(yVal[i]*yVal[i]); double ytemp=yVal[i]+step/2.0*ky1; // y+h*k1/2, or y_midpoint //calculate k2 using x_midpoint and y_midpoint double ky2=ytemp/xValhalf-(xValhalf*xValhalf)/(ytemp*ytemp); ytemp = yVal[i]+step/2.0*ky2; // y+h*k2/2 //calculate k3 double ky3 = ytemp/xValhalf-(xValhalf*xValhalf)/(ytemp*ytemp); ytemp = yVal[i]+step*ky3; //calculate k4 double ky4 = ytemp/xVal[i+1]-(xVal[i+1]*xVal[i+1])/(ytemp*ytemp); //propagate through the interval yVal[i+1]=yVal[i]+step*(ky1/6.0+ky2/3.+ky3/3.+ky4/6.); 9 } Applying R-K Method Example: Solving y’=(y/x)-(x2/y2), With a helper function: double helperFunction(double x, double y) { //definition of y’ return y/x – x*x/(y*y); } for (int i=0; i<Nsteps; ++i) { xVal[i+1]=xVal[i]+step; double xValhalf=xVal[i]+step/2.; //calculate k1 double ky1= helperFunction(xVal[i],yVal[i]); //calculate k2 double ky2=helperFunction(xValhalf, yVal[i]+step/2.0*ky1); //calculate k3 double ky3 = helperFunction(xValhalf, yVal[i]+step/2.0*ky2); //calculate k4 double ky4 = helperFunction(xVal[i+1], yVal[i]+step*ky3); //propagate through the interval yVal[i+1]=yVal[i]+step*(ky1/6.0+ky2/3.+ky3/3.+ky4/6.); } 10 Result for y’=y/x – x2/y2 With y(1)=1, h=0.05, This solution is for 1<x<e1/3 Can you plot the range for 0<x<1? Need to go backwards. 11 Homework 7 Ch 8, Problem 4: Use a Runge-Kutta algorithm to solve the problem of projectile motion with air resistance, i.e. To the 2nd Law Eq, you’ll have the usual force of gravity (mg) directed down, then add a Force term F=–bv (where v is the velocity). Part a) You need to calculate the motion in both the x (horizontal) and y(vertical) direction. You can take the initial conditions to be always x(0)=y(0)=0 for simplicity. Assume ground is flat. The two other initial conditions you will need to specify for the problem are the magnitude v0 and angle q0 of the initial velocity vector Produce a graph of the trajectory (y vs x) for m=1,g=9.81, b=1, v0=30 (all SI units), q0 = p/4. Part b) For a fixed v0, which angle gives the largest horizontal distance? Make a plot of Max Distance vs. angle to answer this. Mark on the plot the required angle, specifying its numerical value in radians. 12 Can you guess if it should be larger or smaller than 45 degrees? Solution for v0=50, b=0.75. Your solution should look similar (not identical, because the input parameters are different) maximum distance (m) Solution for 1-b): angle (radians) 13