You are on page 1of 5

MEC3456EngineeringComputationalAnalysis

MEC3456 Engineering Computational Analysis Laboratory #6


Due date: Friday of week 8 5:00pm [Note: You are asked to submitted Lab #6 and Lab #7 reports together.] Submitted to assignment box MEC3456 next to Department of Mechanical and Aerospace Engineering main office in building 31. General Instructions: The aim of this computer lab is to practice the Runge-Kutta method to solve an ordinary differential equation. It will not be possible to finish all the questions in the two-hour lab sessions on Wednesday. Seven hours of private study is expected each week for this unit. The solutions will be uploaded on MUSO site on Friday at 6pm of week 8. After this time, no further submission will be accepted. If you have a valid cause for not handing in the solution on time, please see lecturer of this unit who will provide alternative assessment. In your report, you should include: (a) The steps taken to obtain the solutions. (b) All the coding should be cut and paste in your report, including the comment to your codes. (c) Output of your code. (d) Hand calculation (if asked), discussions, and conclusions.

You may seek help from lecturer (during lab class and consultation hours) and tutors (during lab class), in textbooks, lecture slides, and using MATLAB help files.

Question: Runge-Kutta method.

MEC3456EngineeringComputationalAnalysis

Use the fourth order Runge-Kutta method to solve ODE

with initial condition y(0)=2.0, from x=0 to x=4.0. (a) (b) Write a MATLAB code to solve this question with step size h=1.0. You may start from the code euler provided in lecture 12 page 21. Convert your code into a function, the head of your function should read as: function [x, y]=RK4(func, y_init, x_init, x_end, nstep) where func is the function file defined the RHS of the ODE, y_init is the initial value of y, x_init is the initial value of x, the x_end is the final x value, and nstep is the number of steps between x_init and x_end. (c) Use your function RK4 to solve this ODE with nstep = 2, 4, 8, 16, 32, 64. Tabulate the computed y value at x=4.0 with the corresponding step size h. Use Euler method (i.e., the code euler in lecture 12) to redo the same calculations. Compare the results obtained by these two methods at x=4.0. Comment on their accuracy. The exact solution y(4.0) = 75.3390 Now the computed y value at x = 4.0 is a function of h4. The graphical Romberg method could be used to determine the step size h that is required for an error (measured at x=4.0) less than 10-4. Update your RK4 code to include this graphical Romberg method so that the updated code can automatically determine the number of steps (your code should quit once the required accuracy is reached). The head of your new function should read as:

(d)

function [x, y, y_end]=RK4_Romberg(func, y_init, x_init, x_end, toler) where func is the function file defined the RHS of the ODE, y_init is the initial value of y, x_init is the initial value of x, the x_end is the final x value, toler is the required accuracy measured at x_end, x and y are the calculated results by RK4 method with the determined step size h, y_end is the estimated value at x_end by the graphical Romberg method. (Note: task (d) is prepared for the shooting method that we will do in next computer lab.)

Solution:

MEC3456EngineeringComputationalAnalysis

(a) The MATLAB code solve the ODE with h=1.0 by RK4 method is
%%% %%% %%% %%% %%% %%% %%% %%% RK4 method to solve ODE dy/dx=f(x,y) with intitial condition y(x_init)=y_init x_init: the initial x value y_init: the initial y value x_end: the end of x nstep: number of steps. h: step size

clear all; clc; % defintion of the ODE. func = @(x, y) 4*exp(0.8*x)-0.5*y; x_init = 0.0; x_end = 4.0; y_init = 2.0; % nstep=4 suggests step size h=1.0; nstep = 4; h = (x_end-x_init)/nstep; % initilization x(1) = x_init; y(1) = y_init; % The iterations in RK4 method for i = 1 : nstep x(i+1) = x(i) + h; % compute x value at next step. k1=feval(func, x(i), y(i)); % compute the first slope function k2=feval(func, x(i)+h/2, y(i)+h/2*k1); % compute the second slope fucntion k3=feval(func, x(i)+h/2, y(i)+h/2*k2); % compute the thired slope function k4=feval(func, x(i+1), y(i)+h*k3); % computer the fourth slope function y(i+1) = y(i) + h/6 * ( k1 + 2*k2 + 2*k3 + k4 ); % compute y value with the averaged slope end plot(x, y, '-o');

The result is plot as:

(b) the MATLAB function is:

MEC3456EngineeringComputationalAnalysis

function [x, y]=RK4(func, y_init, x_init, x_end, nstep) % Euler method to solve ODE 08/2009 % dy/dx=f(x,y) with initial condition y(x_init)=y_init % % % % % func: the RHS of the ODE function f(x,y) x_init: the initial value of x. x_end: the end of x y_init: the value of initial y. nstep: number of steps

% step size h h = ( x_end - x_init ) / nstep; % initialization x(1) = x_init; y(1) = y_init; % The iterations in RK4 method for i = 1 : nstep x(i+1) = x(i) + h; % compute x value at next step. k1=feval(func, x(i), y(i)); % compute the first slope function k2=feval(func, x(i)+h/2, y(i)+h/2*k1); % compute the second slope fucntion k3=feval(func, x(i)+h/2, y(i)+h/2*k2); % compute the thired slope function k4=feval(func, x(i+1), y(i)+h*k3); % computer the fourth slope function y(i+1) = y(i) + h/6 * ( k1 + 2*k2 + 2*k3 + k4 ); % compute y value with the averaged slope end

(c) The results are summarized in next table. nstep 2 4 8 16 32 64 h 2.0 1.0 0.5 0.25 0.125 0.0625 y(4.0) calculated by Euler method 39.6243 56.8493 66.0714 70.7161 73.0323 74.1870 y(4.0) calcualted by RK4 method 76.7697 75.4392 75.3453 75.3394 75.3390 75.3390

From the question, we know the exact solution y(4.0)=75.3390. For RK4 method, with nstep=32 and 64, we obtain the calculated y(4.0) with up to the fourth decimal digits the same as the exact solution. However, for Euler method, at the same number of steps, it is still far from the convergence. (d) MATLAB code with the Romberg method implemented.
function [x, y, y_end]=RK4_Romberg(func, y_init, x_init, x_end, toler) % Euler method to solve ODE 08/2009 % dy/dx=f(x,y) with initial condition y(x_init)=y_init % func: the RHS of the ODE function f(x,y)


% % % % % x_init: x_end: y_init: toler:

MEC3456EngineeringComputationalAnalysis
the initial value of x. the end of x the value of initial y. tolerance between the computed values of y(x_end) with two successive number of steps.

% initialization x(1) = x_init; y(1) = y_init; % initialize ii, the number of steps is 2^ii ii=1; % initialize error value error = 10; % while loop to determine whether the required accuracy is reached. while abs(error) > toler % number of steps and the step size h nstep=2^ii; h = ( x_end - x_init ) / nstep; % The iterations in RK4 method for i = 1 : nstep x(i+1) = x(i) + h; % compute x value at next step. k1=feval(func, x(i), y(i)); % compute the first slope function k2=feval(func, x(i)+h/2, y(i)+h/2*k1); % compute the second slope fucntion k3=feval(func, x(i)+h/2, y(i)+h/2*k2); % compute the thired slope function k4=feval(func, x(i+1), y(i)+h*k3); % computer the fourth slope function % compute y value with the averaged slope function y(i+1) = y(i) + h/6 * ( k1 + 2*k2 + 2*k3 + k4 ); end % computed y(x_end) is a function of h^4. % preparison for the graphical Romberg method. h_vec(ii) = h^4; z_vec(ii) = y(length(y)); % The graphical Romberg method. % polyfit is a inbuilt function of MATLAB to do the polynomial % interpolation. polyval is to extrapolate the y_end at h=0. p=polyfit(h_vec,z_vec,ii-1); y_end_vec(ii)=polyval(p, 0.0) % compare the two estimates of y(x_end) with two successive number of % steps if ii>1 error = y_end_vec(ii) - y_end_vec(ii-1); end ii = ii + 1; end % return the results by the graphical Romberg method. y_end = y_end_vec(ii-1);

Using the combination of RK4 and the graphical Romberg method, we can obtain the results y(4.0) with up to the fourth decimal digits the same as the exact solution, using nstep only up to 8 and 16. In question (b), we need 32 and 64 steps to get the same results if only RK4 method is employed.

You might also like