Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define x0 2
- #define v0 1
- #define t0 0
- #define tf 1000
- #define w02 3
- #define dt 1e-2
- #define N ((tf-t0)/dt)
- double phi(double x)
- {
- return -w02*x;
- }
- double E(double x, double v)
- {
- return v*v/w02 + x*x;
- }
- int main(void)
- {
- double x = x0, xn,
- t = t0, tn,
- v = v0, vn;
- FILE *h = fopen("./diferencial.dat","w+");
- double E0 = E(x,v);
- for (int i = 0; i < N; ++i)
- {
- xn = x + vn*dt + 0.5*phi(x)*dt*dt;
- vn = v + 0.5*(phi(xn)+phi(x))*dt;
- tn = t + dt;
- fprintf(h, "%d %f %f %f\n", i, t, x, E(x,v)/E0-1);
- x = xn;
- v = vn;
- t = tn;
- }
- fclose(h);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement