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((yh2­yh),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((yh2­yh),Inf)/norm(yh2,Inf)
end
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
    fprintf (fid,' %4.2f  %6.4f   %6.4fn',t,y(1),yexact(1));
    % Plot data
    plot(t,y(1),'ro', t,yexact(1),'b*') ;
end
toc
fclose(fid) ;
Here h started from 0.2 and dropped to 0.05. The drop of the local error is given in Table 3.
Table 3. Local error
local_err = 0.074895
local_err = 0.025799
local_err = 0.0063613
h = 0.050000
The Backward Euler solution with the adjusted step-size is given in Figure 1.
Figure 1. Comparison of numerical solution ( red circles) with analytical solution.
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.
yk1/2= yk
h
2
f tk , yk
yk1=ykh f tk1/2, yk1/2
(1)
yk1
0
= ykh f tk , yk
yk1=yk
h
2
f tk , yk f tk1, yk1
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:
yk1=yk
h
2
f tk , yk f tk1 , yk1 (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:
Et =∣exact t−approximate t∣ (4)
Also compare them in terms of the computational time.

More Related Content

PPTX
curve fitting or regression analysis-1.pptx
PPTX
Solving Simultaneous Linear Equations-1.pptx
PPTX
Solving of Non-Linear Equations-1.pptx
PDF
Hand gesture recognition using discrete wavelet transform and hidden Markov m...
DOCX
Forecasting models for Customer Lifetime Value
PDF
Support Vector Machine Derivative
PDF
assignment_2
PDF
curve fitting or regression analysis-1.pptx
Solving Simultaneous Linear Equations-1.pptx
Solving of Non-Linear Equations-1.pptx
Hand gesture recognition using discrete wavelet transform and hidden Markov m...
Forecasting models for Customer Lifetime Value
Support Vector Machine Derivative
assignment_2

Similar to Assignment 3 (20)

PDF
CollidingParticles.pdf
PPTX
Modelling using differnt metods in matlab2 (2) (2) (2) (4) (1) (1).pptx
PPT
This is related to numberical method, in engineering college
PPT
introduction to numerical analysis .ppt
PPT
numerical.ppt
PDF
Animation as-the-final-step-in-the-dynamics-experience
PPTX
Mechanical Engineering Assignment Help
PDF
Fluids_Final
PDF
Matlab lab.pdf
DOCX
LAB05ex1.mfunction LAB05ex1m = 1; .docx
PDF
Robert_Tanner_Temperature_Distribution
DOCX
Lab 5 template Lab 5 - Your Name - MAT 275 Lab The M.docx
DOCX
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
PPTX
Daa unit 2
PPTX
Daa unit 2
PPTX
Algorithm Using Divide And Conquer
PDF
ilovepdf_merged
PPTX
Daa unit 1
PDF
Daa chapter 2
CollidingParticles.pdf
Modelling using differnt metods in matlab2 (2) (2) (2) (4) (1) (1).pptx
This is related to numberical method, in engineering college
introduction to numerical analysis .ppt
numerical.ppt
Animation as-the-final-step-in-the-dynamics-experience
Mechanical Engineering Assignment Help
Fluids_Final
Matlab lab.pdf
LAB05ex1.mfunction LAB05ex1m = 1; .docx
Robert_Tanner_Temperature_Distribution
Lab 5 template Lab 5 - Your Name - MAT 275 Lab The M.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
Daa unit 2
Daa unit 2
Algorithm Using Divide And Conquer
ilovepdf_merged
Daa unit 1
Daa chapter 2
Ad

More from Ersin Tukenmez (20)

PDF
İDARE 2 VİZE-FİNAL-kilidi açıldı.pdf
PDF
ÇOCUKLUK ÇAĞININ DÖKÜNTÜLÜ HASTALIKLARI.pdf
DOCX
İCRA HUKUKU 15 KASIM 2021.docx
DOCX
İCRA HUKUKU 15 KASIM 2021 (1).docx
DOCX
İDARE HUKUKU PRATİK CEVAPLARI.docx
DOCX
İCRA HUKUKU 16 KASIM (1).docx
DOCX
İCRA HUKUKU 16 KASIM.docx
DOCX
Tüm hasta Eğitim Formları Cerrahi.docx
DOCX
Eğitim Planı Formu.docx
DOCX
Eğitim Planı Formu (1).docx
DOCX
Egitim plani formu(dilara2).docx
DOCX
Egitim plani formu(dilara2) (1).docx
DOCX
Eğitim Planı Formu Dahiliye.docx
DOCX
Eğitim Planı Formu Dahiliye (1).docx
DOCX
Bizim_Toptan_Satis_Magazalari_AS.docx
PDF
Quiz 1 solutions
PDF
Ise 455 lecture 10
PDF
Ise 455 2021 midterm question 3
PDF
Ise 455 2021 final question 1 and 2
PDF
6 propagation ap
İDARE 2 VİZE-FİNAL-kilidi açıldı.pdf
ÇOCUKLUK ÇAĞININ DÖKÜNTÜLÜ HASTALIKLARI.pdf
İCRA HUKUKU 15 KASIM 2021.docx
İCRA HUKUKU 15 KASIM 2021 (1).docx
İDARE HUKUKU PRATİK CEVAPLARI.docx
İCRA HUKUKU 16 KASIM (1).docx
İCRA HUKUKU 16 KASIM.docx
Tüm hasta Eğitim Formları Cerrahi.docx
Eğitim Planı Formu.docx
Eğitim Planı Formu (1).docx
Egitim plani formu(dilara2).docx
Egitim plani formu(dilara2) (1).docx
Eğitim Planı Formu Dahiliye.docx
Eğitim Planı Formu Dahiliye (1).docx
Bizim_Toptan_Satis_Magazalari_AS.docx
Quiz 1 solutions
Ise 455 lecture 10
Ise 455 2021 midterm question 3
Ise 455 2021 final question 1 and 2
6 propagation ap
Ad

Assignment 3

  • 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((yh2­yh),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((yh2­yh),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
  • 3.     fprintf (fid,' %4.2f  %6.4f   %6.4fn',t,y(1),yexact(1));     % Plot data     plot(t,y(1),'ro', t,yexact(1),'b*') ; end toc fclose(fid) ; Here h started from 0.2 and dropped to 0.05. The drop of the local error is given in Table 3. Table 3. Local error local_err = 0.074895 local_err = 0.025799 local_err = 0.0063613 h = 0.050000 The Backward Euler solution with the adjusted step-size is given in Figure 1. Figure 1. Comparison of numerical solution ( red circles) with analytical solution.
  • 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. yk1/2= yk h 2 f tk , yk yk1=ykh f tk1/2, yk1/2 (1) yk1 0 = ykh f tk , yk yk1=yk h 2 f tk , yk f tk1, yk1 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: yk1=yk h 2 f tk , yk f tk1 , yk1 (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: Et =∣exact t−approximate t∣ (4) Also compare them in terms of the computational time.