forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample_1_18.m
176 lines (147 loc) · 4.5 KB
/
Example_1_18.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function Example_1_18
% ~~~~~~~~~~~~~~~~~~~
%{
This function uses the RK1 through RK4 methods with two
different time steps each to solve for and plot the response
of a damped single degree of freedom spring-mass system to
a sinusoidal forcing function, represented by
x'' + 2*z*wn*x' + wn^2*x = (Fo/m)*sin(w*t)
The numerical integration is done by the external
function 'rk1_4', which uses the subfunction 'rates'
herein to compute the derivatives.
This function also plots the exact solution for comparison.
x - displacement (m)
' - shorthand for d/dt
t - time (s)
wn - natural circular frequency (radians/s)
z - damping factor
wd - damped natural frequency
Fo - amplitude of the sinusoidal forcing function (N)
m - mass (kg)
w - forcing frequency (radians/s)
t0 - initial time (s)
tf - final time (s)
h - uniform time step (s)
tspan - a row vector containing t0 and tf
x0 - value of x at t0 (m)
x_dot0 - value of dx/dt at t0 (m/s)
f0 - column vector containing x0 and x_dot0
rk - = 1 for RK1; = 2 for RK2; = 3 for RK3; = 4 for RK4
t - solution times for the exact solution
t1, ...,t4 - solution times for RK1,...,RK4 for smaller
t11,...,t41 - solution times for RK1,...,RK4 for larger h
f1, ...,f4 - solution vectors for RK1,...,RK4 for smaller h
f11,...,f41 - solution vectors for RK1,...,RK4 for larger h
User M-functions required: rk1_4
User subfunctions required: rates
%}
% ------------------------------------------------------------------------
clear all; close all; clc
%...Input data:
m = 1;
z = 0.03;
wn = 1;
Fo = 1;
w = 0.4*wn;
x0 = 0;
x_dot0 = 0;
f0 = [x0; x_dot0];
t0 = 0;
tf = 110;
tspan = [t0 tf];
%...End input data
%...Solve using RK1 through RK4, using the same and a larger
% time step for each method:
rk = 1;
h = .01; [t1, f1] = rk1_4(@rates, tspan, f0, h, rk);
h = 0.1; [t11, f11] = rk1_4(@rates, tspan, f0, h, rk);
rk = 2;
h = 0.1; [t2, f2] = rk1_4(@rates, tspan, f0, h, rk);
h = 0.5; [t21, f21] = rk1_4(@rates, tspan, f0, h, rk);
rk = 3;
h = 0.5; [t3, f3] = rk1_4(@rates, tspan, f0, h, rk);
h = 1.0; [t31, f31] = rk1_4(@rates, tspan, f0, h, rk);
rk = 4;
h = 1.0; [t4, f4] = rk1_4(@rates, tspan, f0, h, rk);
h = 2.0; [t41, f41] = rk1_4(@rates, tspan, f0, h, rk);
output
return
% ~~~~~~~~~~~~~~~~~~~~~~~~
function dfdt = rates(t,f)
% ------------------------
%{
This function calculates first and second time derivatives
of x as governed by the equation
x'' + 2*z*wn*x' + wn^2*x = (Fo/m)*sin(w*t)
Dx - velocity (x')
D2x - acceleration (x'')
f - column vector containing x and Dx at time t
dfdt - column vector containing Dx and D2x at time t
User M-functions required: none
%}
% ~~~~~~~~~~~~~~~~~~~~~~~~
x = f(1);
Dx = f(2);
D2x = Fo/m*sin(w*t) - 2*z*wn*Dx - wn^2*x;
dfdt = [Dx; D2x];
end %rates
% ~~~~~~~~~~~~~
function output
% -------------
%...Exact solution:
wd = wn*sqrt(1 - z^2);
den = (wn^2 - w^2)^2 + (2*w*wn*z)^2;
C1 = (wn^2 - w^2)/den*Fo/m;
C2 = -2*w*wn*z/den*Fo/m;
A = x0*wn/wd + x_dot0/wd +(w^2 + (2*z^2 - 1)*wn^2)/den*w/wd*Fo/m;
B = x0 + 2*w*wn*z/den*Fo/m;
t = linspace(t0, tf, 5000);
x = (A*sin(wd*t) + B*cos(wd*t)).*exp(-wn*z*t) ...
+ C1*sin(w*t) + C2*cos(w*t);
%...Plot solutions
% Exact:
subplot(5,1,1)
plot(t/max(t), x/max(x), 'k', 'LineWidth',1)
grid off
axis tight
title('Exact')
% RK1:
subplot(5,1,2)
plot(t1/max(t1), f1(:,1)/max(f1(:,1)), '-r', 'LineWidth',1)
hold on
plot(t11/max(t11), f11(:,1)/max(f11(:,1)), '-k')
grid off
axis tight
title('RK1')
legend('h = 0.01', 'h = 0.1')
% RK2:
subplot(5,1,3)
plot(t2/max(t2), f2(:,1)/max(f2(:,1)), '-r', 'LineWidth',1)
hold on
plot(t21/max(t21), f21(:,1)/max(f21(:,1)), '-k')
grid off
axis tight
title('RK2')
legend('h = 0.1', 'h = 0.5')
% RK3:
subplot(5,1,4)
plot(t3/max(t3), f3(:,1)/max(f3(:,1)), '-r', 'LineWidth',1)
hold on
plot(t31/max(t31), f31(:,1)/max(f31(:,1)), '-k')
grid off
axis tight
title('RK3')
legend('h = 0.5', 'h = 1.0')
% RK4:
subplot(5,1,5)
plot(t4/max(t4), f4(:,1)/max(f4(:,1)), '-r', 'LineWidth',1)
hold on
grid off
plot(t41/max(t41), f41(:,1)/max(f41(:,1)), '-k')
axis tight
title('RK4')
legend('h = 1.0', 'h = 2.0')
end %output
end %Example_1_18
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~