0% found this document useful (0 votes)
947 views

Runge-Kutta 4th-Order Method and Hints

The document describes the 4th order Runge-Kutta numerical method for solving differential equations. It presents the general form of the method which uses the average of four slope approximations to estimate the next value. It then provides Matlab code to implement the method to solve the differential equation mv'=-bv+f with initial condition v(0)=0.5. The code calculates the numerical solution, exact solution, and plots them for comparison. It also uses two methods, numerical integration and symbolic integration, to calculate work done over time by an engine.

Uploaded by

OO
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
947 views

Runge-Kutta 4th-Order Method and Hints

The document describes the 4th order Runge-Kutta numerical method for solving differential equations. It presents the general form of the method which uses the average of four slope approximations to estimate the next value. It then provides Matlab code to implement the method to solve the differential equation mv'=-bv+f with initial condition v(0)=0.5. The code calculates the numerical solution, exact solution, and plots them for comparison. It also uses two methods, numerical integration and symbolic integration, to calculate work done over time by an engine.

Uploaded by

OO
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Runge-Kutta 4th-Order

Method
4th Order Runge-Kutta Method (Average slopes of 4 points to approximate the result)
𝒇˙  𝟒 : 𝒔𝒍𝒐𝒑𝒆 𝒊𝒏 𝒆𝒏𝒅𝒑𝒐𝒊𝒏𝒕  
current value
  next value
=

=
=
=
  𝒇˙  𝟑 : 𝒔𝒍𝒐𝒑𝒆 𝒊𝒏 𝒎𝒊𝒅 − 𝒑𝒐𝒊𝒏𝒕 =
=

𝒇˙  𝟐 : 𝒔𝒍𝒐𝒑𝒆 𝒊𝒏 𝒎𝒊𝒅 − 𝒑𝒐𝒊𝒏𝒕


 
𝑦  0 =
𝒇˙  𝟏 : 𝒔𝒍𝒐𝒑𝒆 𝒊𝒏 𝒊𝒏𝒊𝒕𝒊𝒂𝒍 𝒑𝒐𝒊𝒏𝒕

h 𝑡  0+ h
𝑡 0 𝑡  0+
2
clear
close all
%% the function is mv'=-bv+f intitial condition v(0)=0.5  
%% the exact solution is v=f/b+(-f/b+0.5)*exp((-b/m)*t))
%%
m=1;
b=2;
f=3;
tstart=0;
tend=10;
h = 0.001; %%change to small step size (0.1 or 0.01) then the result of numerical
method will be better Time step
t = tstart:h:tend;
v_num(1) = 0.5;
t(1) = 0;
v_exact(1) =0.5;
%fprintf('Step 0: t = %12.8f, w = %12.8f\n', t, w);
for i=2:size(t,2)
Slop of Point1
k1 = h*fnum(t(i-1),v_num(i-1));
Slop of Point2
k2 = h*fnum(t(i-1)+h/2, v_num(i-1)+k1/2); 4th-Order Runge-Kutta Method
Slop of Point3
k3 = h*fnum(t(i-1)+h/2, v_num(i-1)+k2/2); V_num(1)=v(0)=0.5
k4 = h*fnum(t(i-1)+h, v_num(i-1)+k3); Slop of Point4
Iteration from 2 to final step
v_num(i) = v_num(i-1) + (k1+2*k2+2*k3+k4)/6;
end
yt=tstart:0.01:tend;
y(1) =0.5;
for i=2:size(yt,2) Get Exact Result
v_exact(i) = fexact(yt(i)); Time interval:0.01
end
figure
plot(t,v_num)
hold on
plot(yt,v_exact) Plot Result
legend('RK Method','exact result')
xlabel('time(s)')
ylabel('velocity(m/s)')
%%%%%%%%%%%%%%%%%%
function dv = fnum(t,v)
m=1;
b=2; Use to calculate the slope in Runge-Kutta Method
f=3;
dv = -b*v+f;
end
function v_exact = fexact(t)
m=1;
b=2; Use to calculate the exact solution
f=3;
v_exact=(f/b)+(-f/b+0.5)*exp((-b/m)*t);
end
0<𝑡
  ≤ 𝑡𝑐

𝑡  > 𝑡 𝑐

𝑃
𝑓  𝑝= ; 𝑓 𝑑 =𝑏𝑣
𝑣

0<𝑡
  ≤ 𝑡𝑐 𝑚
  𝑣˙ =𝑓 𝑝 − 𝑓 𝑑

𝑡  > 𝑡 𝑐 𝑚
  𝑣˙ =− 𝑓 𝑑
𝑐 ¿ 𝑆𝑜𝑙𝑣𝑒 𝑡h𝑒 𝐹𝑖𝑟𝑠𝑡 𝑂𝐷𝐸
 
  d)
Initial Condition:0

Solve by Laplace Transform


and Inverse Laplace Transform

  Initial Condition:

Solve by Laplace Transform


and Inverse Laplace Transform
  e) as

𝑣 2 𝑐 = 𝑣 (2 𝑡 𝑐 )
 
  f)

 

What is ?
i) Compare Exact and Numerical Solution

Runge-Kutta 4th Order Method


j) Work and Energy
Work: W(t) from Engine

Kinetic: K(t)

Total Energy: E(t)= W(t) – D(t) – K(t)

Dissipated Energy: –D(t)


If you draw D(t) the value is positive
clear Matlab Integral
close all
%%%Two Ways to do the Integral Function
Chose one method to calculation the energy
%%%%%%%%%%%%%%%First method, use numerical integral%%%%%%%%%%%%
fun=@(t)(1-exp(-t));
t=0:0.01:1;
%use for loop to calculate the numerical solution from 0 to 1s First Method:
%time interval is 0.1s Integral by numerical integral
for i=1:size(t,2)
W(i)=integral(fun,0,t(i));
end
plot(t,W);

%%%%%%%%%Second Method Use Symbolic Integral%%%%%%%%%%%


%%%Create the symbolic variable
syms t C
p_sym=(1-exp(-t)); %Origianl Function before Integral
Wexact_sym=int(p_sym)+C %Symbolic integral function int(symbolic equation)
%%%Calculate the Constant C Second Method:
%Waxact_sym(0)=0=C+t+exp(-t)--> C=-t-exp(-t) and t=0; Integral by Symbolic integral
t=0;
C=-t-exp(-t);
%Draw the Exact Solution
t=0:0.01:1; %Creat Time Variable
Wexact=(t+exp(-t))+C; %generate the exact solution from 0 to 1 sec
hold on
plot(t,Wexact);

You might also like