-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob2.c
134 lines (93 loc) · 3.32 KB
/
prob2.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
/*
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
compilation command:
gcc provlima2.c -lm
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, Cz;
double h = 0.001;
double Kpz, Kdz, zdes;
// lisi
#define z(t) (-15.169)*exp((-0.234)*t)+0.169*exp((-20.988)*t)+13.425
// sistima
#define q1_d(t,a,b) b
#define q2_d(t,a,b) (((M*g+Kpz*(zdes-a)-Kdz*b)-g*M-Cz*b)/M)
// Euler
#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)
// Veltiomeni Euler
#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)))
int main() {
// gia na grapsoume ta apotelesmata se arxeia dat
FILE *output_file;
double t;
double an1, a0, bn1, b0;
double z0;
Kpz = 5.0;
Kdz = 15.0+((double)AM/1000.0);
z0 = 0.0;
zdes = ((double)AM/200.0);
Cz = 3.0+((double)AM/5000.0);
// Euler
a0 = z0;
b0 = 0.0;
// neo arxeio
output_file = fopen("./provlima_2_euler.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t A B");
for (t=0.0; t<30.001; t+=h) {
an1 = An1_Euler_d(t,a0,b0);
bn1 = Bn1_Euler_d(t,a0,b0);
//printf("%f - %f\n",an1,bn1);
// grafoume ta dedomena
if (output_file != NULL) fprintf(output_file,"%f %.10f %.10f\n",t,an1,bn1);
// epomenes times a b
a0 = an1;
b0 = bn1;
}
// Veltiomeni Euler
a0 = z0;
b0 = 0.0;
// neo arxeio
output_file = fopen("./provlima_2_velt_euler.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t A B");
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);
//printf("%f - %f\n",an1,bn1);
// grafoume ta dedomena
if (output_file != NULL) fprintf(output_file,"%f %.10f %.10f\n",t,an1,bn1);
// epomenes times a b
a0 = an1;
b0 = bn1;
}
// lisi
output_file = fopen("./provlima_2_lisi.dat","w+");
if (output_file != NULL) fprintf(output_file,"%s\n","# t Z");
for (t=0.0; t<30.001; t+=h) {
z0 = z(t);
if (output_file != NULL) fprintf(output_file,"%f %.10f\n",t,z0);
}
printf("%f\n", zdes);
return 0;
}