-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob1.c
272 lines (207 loc) · 7.98 KB
/
prob1.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Antoniou Christodoulos 2641
Ilias Diamantis 2685
Tzounas Antonios 2368
compiled and tested on gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
and on gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
oi grafikes parastaseis exoun ginei xrisimopoiontas ta output arxeia .dat tou programmatos sto gnuplot
arxika dialegoume ena katalilo terminal me tin entoli:
set terminal png size 800,600
dimiourgoume ena arxeio proorismou gia to paragomeno plot:
set output "plot_image.png"
kai telos, gia paradeigma oi grafikes parastaseis gia ta a b c d sto provlima 1 erotima delta me tin methodo tou euler
exoun ginei me tis entoles:
plot "erotima_delta_euler.dat" using 1:2 linetype 7 linecolor 7 lw 2 with lines
plot "erotima_delta_euler.dat" using 1:3 linetype 7 linecolor 7 lw 2 with lines
plot "erotima_delta_euler.dat" using 1:4 linetype 7 linecolor 7 lw 2 with lines
plot "erotima_delta_euler.dat" using 1:5 linetype 7 linecolor 7 lw 2 with lines
*/
#include <stdio.h>
#include <math.h>
#define M 1
#define g 9.81
#define Iz 0.08
#define AM 2685
double fz, Tz, Cz, Cy;
double h = 0.001;
double Kpz, Kdz, Kpy, Kdy, zdes, ydes;
// sistima gia erotima vita
#define q1(t,a,b) b
#define q2(t,a,b) ((fz-g*M-Cz*fabs(b)*b)/M)
#define k1(t,c,d) d
#define k2(t,c,d) ((Tz-0.5*Cy*fabs(d)*d)/Iz)
// Euler gia erotima vita
#define An1_Euler(t0,a0,b0) a0+h*q1(t0,a0,b0)
#define Bn1_Euler(t0,a0,b0) b0+h*q2(t0,a0,b0)
#define Cn1_Euler(t0,c0,d0) c0+h*k1(t0,c0,d0)
#define Dn1_Euler(t0,c0,d0) d0+h*k2(t0,c0,d0)
// Veltiomeni Euler gia erotima vita
#define An1_Velt_Euler(t0,a0,b0) a0+(h/2)*(q1(t0,a0,b0)+q1(t0+h,a0+h*q1(t0,a0,b0),b0+h*q2(t0,a0,b0)))
#define Bn1_Velt_Euler(t0,a0,b0) b0+(h/2)*(q2(t0,a0,b0)+q2(t0+h,a0+h*q1(t0,a0,b0),b0+h*q2(t0,a0,b0)))
#define Cn1_Velt_Euler(t0,c0,d0) c0+(h/2)*(k1(t0,c0,d0)+k1(t0+h,c0+h*k1(t0,c0,d0),d0+h*k2(t0,c0,d0)))
#define Dn1_Velt_Euler(t0,c0,d0) d0+(h/2)*(k2(t0,c0,d0)+k2(t0+h,c0+h*k1(t0,c0,d0),d0+h*k2(t0,c0,d0)))
// sistima gia erotima delta
#define q1_d(t,a,b) b
#define q2_d(t,a,b) (((M*g+Kpz*(zdes-a)-Kdz*b)-g*M-Cz*fabs(b)*b)/M)
#define k1_d(t,c,d) d
#define k2_d(t,c,d) (((Kpy*(ydes-c)-Kdy*d)-0.5*Cy*fabs(d)*d)/Iz)
// Euler gia erotima delta
#define An1_Euler_d(t0,a0,b0) a0+h*q1_d(t0,a0,b0)
#define Bn1_Euler_d(t0,a0,b0) b0+h*q2_d(t0,a0,b0)
#define Cn1_Euler_d(t0,c0,d0) c0+h*k1_d(t0,c0,d0)
#define Dn1_Euler_d(t0,c0,d0) d0+h*k2_d(t0,c0,d0)
// Veltiomeni Euler gia erotima delta
#define An1_Velt_Euler_d(t0,a0,b0) a0+(h/2)*(q1_d(t0,a0,b0)+q1_d(t0+h,a0+h*q1_d(t0,a0,b0),b0+h*q2_d(t0,a0,b0)))
#define Bn1_Velt_Euler_d(t0,a0,b0) b0+(h/2)*(q2_d(t0,a0,b0)+q2_d(t0+h,a0+h*q1_d(t0,a0,b0),b0+h*q2_d(t0,a0,b0)))
#define Cn1_Velt_Euler_d(t0,c0,d0) c0+(h/2)*(k1_d(t0,c0,d0)+k1_d(t0+h,c0+h*k1_d(t0,c0,d0),d0+h*k2_d(t0,c0,d0)))
#define Dn1_Velt_Euler_d(t0,c0,d0) d0+(h/2)*(k2_d(t0,c0,d0)+k2_d(t0+h,c0+h*k1_d(t0,c0,d0),d0+h*k2_d(t0,c0,d0)))
int main() {
// gia na grapsoume ta apotelesmata se arxeia dat
FILE *output_file;
double t;
double an1, a0, bn1, b0, cn1, c0, dn1, d0;
double z0 = ((double)AM/1000.0);
double y0 = 0.0;
Cz = 3.0-((double)AM/5000.0);
Cy = 5.0-((double)AM/5000.0);
// erotima vita - 1o zeugari eisodon
fz = M*g+((double)AM/1000.0);
Tz = 0.0;
// Euler
a0 = z0;
b0 = c0 = d0 = 0.0;
// neo arxeio
output_file = fopen("./erotima_vita_zeugari_1_euler.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t A B C D");
for (t=0.0; t<30.001; t+=h) {
an1 = An1_Euler(t,a0,b0);
bn1 = Bn1_Euler(t,a0,b0);
cn1 = Cn1_Euler(t,c0,d0);
dn1 = Dn1_Euler(t,c0,d0);
//printf("%f - %f - %f - %f\n",an1,bn1,cn1,dn1);
// grafoume ta dedomena
if (output_file != NULL) fprintf(output_file,"%f %.10f %.10f %.10f %.10f\n",t,an1,bn1,cn1,dn1);
// epomenes times a b c d
a0 = an1;
b0 = bn1;
c0 = cn1;
d0 = dn1;
}
// Veltiomeni Euler
a0 = z0;
b0 = c0 = d0 = 0.0;
// neo arxeio
output_file = fopen("./erotima_vita_zeugari_1_velt_euler.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t A B C D");
for (t=0.0; t<30.001; t+=h) {
an1 = An1_Velt_Euler(t,a0,b0);
bn1 = Bn1_Velt_Euler(t,a0,b0);
cn1 = Cn1_Velt_Euler(t,c0,d0);
dn1 = Dn1_Velt_Euler(t,c0,d0);
//printf("%f - %f - %f - %f\n",an1,bn1,cn1,dn1);
// grafoume ta dedomena
if (output_file != NULL) fprintf(output_file,"%f %.10f %.10f %.10f %.10f\n",t,an1,bn1,cn1,dn1);
// epomenes times a b c d
a0 = an1;
b0 = bn1;
c0 = cn1;
d0 = dn1;
}
// erotima vita - 2o zeugari eisodon
fz = M*g;
Tz = ((double)AM/10000.0);
// Euler
a0 = z0;
b0 = c0 = d0 = 0.0;
// neo arxeio
output_file = fopen("./erotima_vita_zeugari_2_euler.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t A B C D");
for (t=0.0; t<30.001; t+=h) {
an1 = An1_Euler(t,a0,b0);
bn1 = Bn1_Euler(t,a0,b0);
cn1 = Cn1_Euler(t,c0,d0);
dn1 = Dn1_Euler(t,c0,d0);
//printf("%f - %f - %f - %f\n",an1,bn1,cn1,dn1);
// grafoume ta dedomena
if (output_file != NULL) fprintf(output_file,"%f %.10f %.10f %.10f %.10f\n",t,an1,bn1,cn1,dn1);
// epomenes times a b c d
a0 = an1;
b0 = bn1;
c0 = cn1;
d0 = dn1;
}
// Veltiomeni Euler
a0 = z0;
b0 = c0 = d0 = 0.0;
// neo arxeio
output_file = fopen("./erotima_vita_zeugari_2_velt_euler.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t A B C D");
for (t=0.0; t<30.001; t+=h) {
an1 = An1_Velt_Euler(t,a0,b0);
bn1 = Bn1_Velt_Euler(t,a0,b0);
cn1 = Cn1_Velt_Euler(t,c0,d0);
dn1 = Dn1_Velt_Euler(t,c0,d0);
//printf("%f - %f - %f - %f\n",an1,bn1,cn1,dn1);
// grafoume ta dedomena
if (output_file != NULL) fprintf(output_file,"%f %.10f %.10f %.10f %.10f\n",t,an1,bn1,cn1,dn1);
// epomenes times a b c d
a0 = an1;
b0 = bn1;
c0 = cn1;
d0 = dn1;
}
// erotima delta
Kpz = Kpy = 5.0;
Kdz = 15.0+((double)AM/1000.0);
Kdy = 20.0;
z0 = 0.0;
y0 = ((double)AM/10000.0);
zdes = ((double)AM/200.0);
ydes = -((double)AM/3000.0);
Cz = 3.0+((double)AM/5000.0);
Cy = 5.0;
// Euler
a0 = z0;
c0 = y0;
b0 = d0 = 0.0;
// neo arxeio
output_file = fopen("./erotima_delta_euler.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t A B C D");
for (t=0.0; t<30.001; t+=h) {
an1 = An1_Euler_d(t,a0,b0);
bn1 = Bn1_Euler_d(t,a0,b0);
cn1 = Cn1_Euler_d(t,c0,d0);
dn1 = Dn1_Euler_d(t,c0,d0);
//printf("%f - %f - %f - %f\n",an1,bn1,cn1,dn1);
// grafoume ta dedomena
if (output_file != NULL) fprintf(output_file,"%f %.10f %.10f %.10f %.10f\n",t,an1,bn1,cn1,dn1);
// epomenes times a b c d
a0 = an1;
b0 = bn1;
c0 = cn1;
d0 = dn1;
}
// Veltiomeni Euler
a0 = z0;
c0 = y0;
b0 = d0 = 0.0;
// neo arxeio
output_file = fopen("./erotima_delta_velt_euler.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t A B C D");
for (t=0.0; t<30.001; t+=h) {
an1 = An1_Velt_Euler_d(t,a0,b0);
bn1 = Bn1_Velt_Euler_d(t,a0,b0);
cn1 = Cn1_Velt_Euler_d(t,c0,d0);
dn1 = Dn1_Velt_Euler_d(t,c0,d0);
//printf("%f - %f - %f - %f\n",an1,bn1,cn1,dn1);
// grafoume ta dedomena
if (output_file != NULL) fprintf(output_file,"%f %.10f %.10f %.10f %.10f\n",t,an1,bn1,cn1,dn1);
// epomenes times a b c d
a0 = an1;
b0 = bn1;
c0 = cn1;
d0 = dn1;
}
printf("zdes: %f ydes: %f\n", zdes, ydes);
return 0;
}