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 100
- #define w02 3
- #define dt 1e-2
- #define N ((tf-t0)/dt)
- double dvdt(double x, double v, double t)
- {
- 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("./eulernovo.dat","w+");
- double E0 = E(x,v);
- for (int i = 0; i < N; ++i)
- {
- vn = v + dvdt(x,v,t)*dt;
- xn = x + vn*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