-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07-runge_kutta_4.c
104 lines (72 loc) · 1.28 KB
/
07-runge_kutta_4.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
////////////////////
////RUNGE-KUTTA4////
////PEDRO MENDES////
////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double F(double x);
int main()
{
int c;
double t, deltat, tf;
double x;
double a1, a2, a3, a4;
double k1, k2, k3, k4;
double b, b2, b3, b4, p, q;
printf("\n#Escolha o passo: ");
scanf("%lf", &deltat);
printf("\n#Escolha o metodo:\n#Runge-Kutta-4(0)\n#Ponto Medio(1)\n\n");
scanf("%d", &c);
x = 10;
tf = 5;
t = 0;
printf("%lf %lf\n", t, x);
switch(c)
{
case 0: //rk 4
a1 = 1./6.;
a2 = 1./3.;
a3 = 1./3.;
a4 = 1./6.;
while(t < tf)
{
k1 = F(x);
b2 = x + .5*k1*deltat;
k2 = F(b2);
b3 = x + .5*k2*deltat;
k3 = F(b3);
b4 = x + k3*deltat;
k4 = F(b4);
x = x + (1./8)*(k1 + 3*k2 + 3*k3 + k4)*deltat;
t = t + deltat;
printf("%lf %lf\n", t, x);
}
case 1: //ponto medio
a1 = 0;
a2 = 1;
q = .5;
while(t < tf)
{
k1 = F(x);
b = x + q*k1*deltat;
k2 = F(b);
x = x + (a1*k1 + a2*k2)*deltat;
t = t + deltat;
printf("%lf %lf\n", t, x);
}
break;
default:
printf("\nDon't Panic! The answer is 42...\n\n");
break;
}
return 0;
}
double F(double x)
{
double tau;
double f;
tau = 2;
f = - (x/tau);
return f;
}