1. ME 372 Computer-Aided Mechanical Engineering
Instructor: Emre Alpman
Assignment #3
Due Date: Mar 16th, 2011
SECOND ORDER ACCURATE SCHEMES
Summary of the Previous Assignment
In Assignment #2, the main goal was to analyze the effect of local error on the solution accuracy with
the aim of controlling it. A desired tolerance on the local error was set by dtol which was then used to
adjust the stepsize that produces it.
For this purpose the module accuracy.m was developed according to the algorithm given in
Assignment #2. This module is given in Table 1.
Table 1. Module accuracy.m
% This module adjusts the step size according to the given
% error tolerance dtol
% Inputs: Step sizei error tolerance, initial time and condition
% Outputs: Adjusted time step
function [hnew] = accuracy(h,dtol,t0,y0)
hnew = h ;
yh = y0 + hnew*deriv(t0,y0) ; % one step solution
ymid = y0 + hnew/2*deriv(t0,y0); % take half an euler step
tmid = t0+hnew/2;
yh2 = ymid + hnew/2*deriv(tmid,ymid) ; % two step solution
local_err = norm((yh2yh),Inf)/norm(yh2,Inf) % Local error
% Repeat the above steps by dividing the step size by 2 until
% relative error becomes small enough
while local_err > dtol
hnew = hnew/2 ;
yh = ymid ;
ymid = y0 + hnew/2*deriv(t0,y0);
tmid = t0+hnew/2;
yh2 = ymid + hnew/2*deriv(tmid,ymid) ;
local_err = norm((yh2yh),Inf)/norm(yh2,Inf)
end
2. The above routine is called by hw2_main.m before the solution loop starts. The modified
hw2_main.m is given in Table 2.
Table 2. Module hw2_main.m
% main program for hw2
clear all;
% assignments
t0 = 0.0 ; % starting time
tf = 20. ; % final time
h = 0.2 ; % time step size
ctol = 0.001; % Error tolerance for BE
% Open a file to write the data in
filename = 'data.txt' ;
fid = fopen (filename, 'w+');
fprintf (fid,'time approximate exactn');
% initial conditions
y0 = [10.*pi/180; 0.0];
% initialization
i = 0 ; % loop step counter
y = y0 ; % current state = initial state
t = t0 ; % current time = initial time
fprintf (fid,'%3i %6.4f %6.4fn',t,y(1),y0(1));
% Exact Solution
yexact = exact(t0, y0);
figure(1) ;
plot(t,y(1), 'ro',t,yexact,'b*');
grid;
axis([0 20 0.2 0.2])
hold;
% Adjust time step by controlling the local error
h = accuracy(h,0.01,t0,y0)
tic
% time loop
while ( t < tf )
% Obtain the solution
[t,y] = beuler(h,t,y, ctol) ;
% Exact Solution
yexact = exact(t,y0);
% Write data to the file
4. Lab Assignment
In this assignment, you are expected to implement the explicit midpoint and explicit trapezoidal
methods. The formulations for the above methods are given in equations 1 and 2, respectively.
yk1/2= yk
h
2
f tk , yk
yk1=ykh f tk1/2, yk1/2
(1)
yk1
0
= ykh f tk , yk
yk1=yk
h
2
f tk , yk f tk1, yk1
0
(2)
You will write two separate modules named midp.m and trap.m that use Eq. 2 and set of Eqs. 3,
respectively. These modules should be able to replace previous beuler.m module, just by changing its
name in the main program.
In this lab we will compare the methods we have studied for their accuracy. Start with h = 0.02 and
adjust the stepsize with accuracy.m using dtol = 0.01. Then plot and compare angular displacement
with the exact solution for Forward Euler, Backward Euler, Midpoint and Trapezoidal methods.
Homework Assignment
Generate a module for the implicit Trapezoidal method:
yk1=yk
h
2
f tk , yk f tk1 , yk1 (3)
Since the above method is implicit you would need to generate an iterative solution procedure similar
to the one we have done for the Backward Euler method. Compare the performance of explicit and
implicit trapezoidal methods by plotting and tabulating the exact absolute error with respect to time.
We can define the exact absolute error as:
Et =∣exact t−approximate t∣ (4)
Also compare them in terms of the computational time.