-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project3_Spring.m
163 lines (102 loc) · 5.19 KB
/
Project3_Spring.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
clc
clear
close all
%% Data
g = 9.81; %Gravitational Acceleration
M1 = input('Enter M1: '); %Mass Of Ball 1
M2 = input('Enter M2: '); %Mass Of Ball 2
k = input('Enter k: '); %Spring Constant
L0 = 0.5; %Initial Length Of Spring
h = 0.001; %Time Interval
%% Initial Condition For Frame(X,Y)
% For Ball (1): % (X1: Location) , (X1_1=(X1)': Velocity ) , (X1_2=(X1)'': Acceleration )
% (Y1: Location) , (Y1_1=(Y1)': Velocity ) , (Y1_2=(Y1)'': Acceleration )
X1 = zeros(); X1(1) = 0.4; % Initial Location For Ball 1
Y1 = zeros(); Y1(1) = 2.1; %
X1_1 = zeros(); X1_1(1) = 5; % Initial Velocity For Ball 1
Y1_1 = zeros(); Y1_1(1) = 1; %
% For Ball (2): % (X2: Location) , (X2_1=(X2)': Velocity ) , (X2_2=(X2)'': Acceleration )
% (Y2: Location) , (Y2_1=(Y2)': Velocity ) , (Y2_2=(Y2)'': Acceleration )
X2 = zeros(); X2(1) = 0.1; % Initial Location For Ball 2
Y2 = zeros(); Y2(1) = 2.5; %
X2_1 = zeros(); X2_1(1) = 1; % Initial Velocity For Ball 2
Y2_1 = zeros(); Y2_1(1) = 5; %
% For Center Of Mass: % (Xc: Location) , (Xc_1=(Xc)': Velocity ) , (Xc_2=(Xc)''= 0: Acceleration )
% (Yc: Location) , (Yc_1=(Yc)': Velocity ) , (Yc_2=(Yc)''=-g: Acceleration )
Xc = zeros(); Xc(1) = (M1*X1(1)+M2*X2(1))/(M1+M2);
Yc = zeros(); Yc(1) = (M1*Y1(1)+M2*Y2(1))/(M1+M2);
Xc0_1 = (M1*X1_1(1)+M2*X2_1(1))/(M1+M2);
Yc0_1 = (M1*Y1_1(1)+M2*Y2_1(1))/(M1+M2);
%Yc_1 = -g*t+ Yc0_1;
% (t: Time Matrix)
t = zeros(); t(1)=0;
X1_2 = @(t,X1,Y1,X2,Y2) k*(((X2-X1)^2+(Y2-Y1)^2).^(1/2)-L0)*(X2-X1)/(M1*((X2-X1)^2+(Y2-Y1)^2).^(1/2));
Y1_2 = @(t,X1,Y1,X2,Y2) -g + k*(((X2-X1)^2+(Y2-Y1)^2).^(1/2)-L0)*(Y2-Y1)/(M1*((X2-X1)^2+(Y2-Y1)^2).^(1/2));
X2_2 = @(t,X1,Y1,X2,Y2) -k*(((X2-X1)^2+(Y2-Y1)^2).^(1/2)-L0)*(X2-X1)/(M2*((X2-X1)^2+(Y2-Y1)^2).^(1/2));
Y2_2 = @(t,X1,Y1,X2,Y2) -g - k*(((X2-X1)^2+(Y2-Y1)^2).^(1/2)-L0)*(Y2-Y1)/(M2*((X2-X1)^2+(Y2-Y1)^2).^(1/2));
Xc_function = @(t) Xc0_1*t + Xc(1);
Yc_function = @(t) -1/2*(g*t.^2) + Yc0_1*t + Yc(1);
%% Runge Kutta Algorithm For Moving Frame(X,Y)
A = zeros(1,4);
B = zeros(1,4);
C = zeros(1,4);
D = zeros(1,4);
a = zeros(1,4);
b = zeros(1,4);
c = zeros(1,4);
d = zeros(1,4);
e = [1,2,2,1];
n = 1;
while Yc>0
% FINDIG VELOCITY FROM ACCELERATION
A(1)=X1_2( t(n) , X1(n) , Y1(n) , X2(n) , Y2(n) );
B(1)=Y1_2( t(n) , X1(n) , Y1(n) , X2(n) , Y2(n) );
C(1)=X2_2( t(n) , X1(n) , Y1(n) , X2(n) , Y2(n) );
D(1)=Y2_2( t(n) , X1(n) , Y1(n) , X2(n) , Y2(n) );
A(2)=X1_2( t(n)+h/2 , X1(n)+h/2*A(1) , Y1(n)+h/2*B(1) , X2(n)+h/2*C(1) , Y2(n)+h/2*D(1) );
B(2)=Y1_2( t(n)+h/2 , X1(n)+h/2*A(1) , Y1(n)+h/2*B(1) , X2(n)+h/2*C(1) , Y2(n)+h/2*D(1) );
C(2)=X2_2( t(n)+h/2 , X1(n)+h/2*A(1) , Y1(n)+h/2*B(1) , X2(n)+h/2*C(1) , Y2(n)+h/2*D(1) );
D(2)=Y2_2( t(n)+h/2 , X1(n)+h/2*A(1) , Y1(n)+h/2*B(1) , X2(n)+h/2*C(1) , Y2(n)+h/2*D(1) );
A(3)=X1_2( t(n)+h/2 , X1(n)+h/2*A(2) , Y1(n)+h/2*B(2) , X2(n)+h/2*C(2) , Y2(n)+h/2*D(2) );
B(3)=Y1_2( t(n)+h/2 , X1(n)+h/2*A(2) , Y1(n)+h/2*B(2) , X2(n)+h/2*C(2) , Y2(n)+h/2*D(2) );
C(3)=X2_2( t(n)+h/2 , X1(n)+h/2*A(2) , Y1(n)+h/2*B(2) , X2(n)+h/2*C(2) , Y2(n)+h/2*D(2) );
D(3)=Y2_2( t(n)+h/2 , X1(n)+h/2*A(2) , Y1(n)+h/2*B(2) , X2(n)+h/2*C(2) , Y2(n)+h/2*D(2) );
A(4)=X1_2( t(n)+h , X1(n)+h*A(3) , Y1(n)+h*B(3) , X2(n)+h*C(3) , Y2(n)+h*D(3) );
B(4)=Y1_2( t(n)+h , X1(n)+h*A(3) , Y1(n)+h*B(3) , X2(n)+h*C(3) , Y2(n)+h*D(3) );
C(4)=X2_2( t(n)+h , X1(n)+h*A(3) , Y1(n)+h*B(3) , X2(n)+h*C(3) , Y2(n)+h*D(3) );
D(4)=Y2_2( t(n)+h , X1(n)+h*A(3) , Y1(n)+h*B(3) , X2(n)+h*C(3) , Y2(n)+h*D(3) );
X1_1(n+1)=X1_1(n) + h/6*sum(d.*A);
Y1_1(n+1)=Y1_1(n) + h/6*sum(d.*B);
X2_1(n+1)=X2_1(n) + h/6*sum(d.*C);
Y2_1(n+1)=Y2_1(n) + h/6*sum(d.*D);
% FINDIG LOCATION FROM VELOCITY
a(1)=X1_1(n);
b(1)=Y1_1(n);
c(1)=X2_1(n);
d(1)=Y2_1(n);
a(2)=X1_1(n) + h/2*A(1);
b(2)=Y1_1(n) + h/2*B(1);
c(2)=X2_1(n) + h/2*C(1);
d(2)=Y2_1(n) + h/2*D(1);
a(3)=X1_1(n) + h/2*A(2);
b(3)=Y1_1(n) + h/2*B(2);
c(3)=X2_1(n) + h/2*C(2);
d(3)=Y2_1(n) + h/2*D(2);
a(4)=X1_1(n) + h*A(3);
b(4)=Y1_1(n) + h*B(3);
c(4)=X2_1(n) + h*C(3);
d(4)=Y2_1(n) + h*D(3);
X1(n+1)=X1(n) + h/6*sum(d.*a);
Y1(n+1)=Y1(n) + h/6*sum(d.*b);
X2(n+1)=X2(n) + h/6*sum(d.*c);
Y2(n+1)=Y2(n) + h/6*sum(d.*d);
t(n+1)=t(n)+h;
n=n+1;
%Findig Center Of Mass
Xc(n) = Xc_function( t(n) );
Yc(n) = Yc_function( t(n) );
end
%% Result & Plot
plot(X1,Y1,'.g' , X2,Y2,'.r' , Xc,Yc,'.b'); grid
xlabel('X (meter)'); ylabel('Y (meter)');
legend('Ball (1)','Ball (2)','Center Of Mass'); title('Y - X Plot');