-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRK4.m
54 lines (49 loc) · 1.41 KB
/
RK4.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function [y] = RK4(Fun,t,dt,inputFun)
% [y] = RK4(Fun,t,dt,inputFun) solves numerically the equations of
% motion of a damped system
%
% INPUT
% Fun : anonymous function -- already defined in the main script
% t: scalar -- size: [1 x 1] -- time
% dt : scalar -- size: [1 x 1] -- time step
% inputFun : structure -- size: [1 x 1] -- defned in main script. it containe
% state space representation of load, system properties and its
% displacement and veloctiy
%
% OUTPUT
% y: matrix -- size [2x N] --time history of the system response to the load
% First row is displacement, and second row is velocity
%
% Example
% Fun = @(t,Y,A,F) A*Y+ F;
% t = linspace(0,100,1000);
% dt = median(diff(t));
% y5 = zeros(size(t));
% clear inputFun
% Y = [0,10]';
% M = 1;
% K = 1;
% C = 0.005;
% F = cos(t); % expression of the harmonic force
% for ii=1:numel(t),
% inputFun.A = [0, 1;-M\K,-M\C];
% inputFun.F =[0;M\F(ii)];
% inputFun.Y= Y;
% [Y] = RK4(Fun,t,dt,inputFun);
% y5(ii) = Y(1);
% end
%
% author: E. Cheynet. University of Stavanger. last updated: 31/12/2016
%%
% reduction of the variable number
Y = inputFun.Y;
F = inputFun.F;
A = inputFun.A;
% Runge-Kutta of order 4
k_1 = Fun(t,Y,A,F);
k_2 = Fun(t+0.5.*dt,Y+0.5*dt*k_1,A,F);
k_3 = Fun(t+0.5.*dt,Y+0.5*dt*k_2,A,F);
k_4 = Fun(t+dt,Y+k_3*dt,A,F);
% output
y = Y + (1/6)*(k_1+2*k_2+2*k_3+k_4)*dt;
end