Docsity

Runge-Kutta Methods: Analytical Solution & Numerical Integration with Euler & Heun - Prof., Study notes of Mechanical Engineering

Toggle sidebar
bg1
Problem 9.1: Runge-Kutta Basics
Description and Objectives
Solve the following initial value problem over the interval from t = 0 to 2. Use a = 1.1 and y0 =
1. Display all your results on a single graph.
0
2
)0( yy
yaty
dt
dy
a) Analytically.
b) Using Euler’s method with h =0.5 and 0.25.
c) Using Heun’s method with h =0.5.
Analysis
(a) Analytical Solution
For this particular case, it is possible to separate the variables.
dtat
y
dy
2
This equation can now be integrated directly.
cta
t
y
cta
t
y
dtat
y
dy
3
exp
3
)ln(
3
3
2
The unknown constant is determined by forcing the solution to satisfy the initial condition.
cy exp
0
ta
t
yy 3
exp
3
0
pf3
pf4

Partial preview of the text

Download Runge-Kutta Methods: Analytical Solution & Numerical Integration with Euler & Heun - Prof. and more Study notes Mechanical Engineering in PDF only on Docsity!

Problem 9.1: Runge-Kutta Basics

Description and Objectives

Solve the following initial value problem over the interval from t = 0 to 2. Use a = 1.1 and y 0 =

1. Display all your results on a single graph.

0 2

y ( 0 ) y

y t a y

dt

dy

a) Analytically.

b) Using Euler’s method with h =0.5 and 0.25.

c) Using Heun’s method with h =0.5.

Analysis

(a) Analytical Solution

For this particular case, it is possible to separate the variables.

 t a  dt

y dy  2 

This equation can now be integrated directly.

 ^ 

a t c t y a t c t y t a dt y dy 3 exp

ln( ) 3 3 2

The unknown constant is determined by forcing the solution to satisfy the initial condition.

y 0 exp  c 

  at t y y 3 exp 3 0

MATLAB Codes

Function odeEuler

function [t,y] = odeEuler(diffeq,tn,h,y0) % odeEuler Euler's method for integration of a single, first order ODE % % Syntax: [t,y] = odeEuler(diffeq,tn,h,y0) % % Input: diffeq = anonymous function that evaluates the right % hand side of the ODE as a function of (t,y) % tn = stopping value of the independent variable % h = stepsize for advancing the independent variable % y0 = initial condition for the dependent variable % % Output: t = vector of independent variable values: t(i) = (i-1)h % y = vector of numerical solution values at the t(i) % initialize t = (0:h:tn)'; % Column vector of elements with spacing h n = length(t); % Number of elements in the t vector y = zeros(n,1); % Preallocate y for speed y(1) = y0; % compute solution using Euler scheme for i = 1:n- y(i+1) = y(i) + hdiffeq(t(i),y(i)); end

Function odeHeun

function [t,y] = odeHeun(diffeq,tn,h,y0) % odeHeun Heun method for integration of a single, first order ODE % % Syntax: [t,y] = odeHeun(diffeq,tn,h,y0) % % Input: diffeq = anonymous function that evaluates the right % hand side of the ODE as a function of (t,y) % tn = stopping value of the independent variable % h = stepsize for advancing the independent variable % y0 = initial condition for the dependent variable % % Output: t = vector of independent variable values: t(i) = (i-1)h % y = vector of numerical solution values at the t(i) t = (0 : h : tn)'; % Column vector of elements with spacing h n = length(t); % Number of elements in the t vector y = y0zeros(n,1); % Preallocate y for speed y(1) = y0; % Begin Midpoint scheme; j=1 for initial condition for i = 1:n- k1 = feval( diffeq, t(i), y(i) ); k2 = feval( diffeq, t(i)+h, y(i)+hk1 ); y(i+1) = y(i) + h(k1+k2)/2; end

Results

t y(t) exact Euler,h=0. Euler,h=0. Heun,h=0.

Comments

Using the Euler scheme, reducing the step size by a factor of 2 reduces the error by

approximately a factor of 2. The Heun method is second order accurate and gives better results

at the expense of more computations.