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 2.5
- #define dt0 0.01
- #define dtf 1e-6
- #define ddt -1e-4
- #define w02 3
- #define Ndt ((dtf-dt0)/ddt)
- 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, xn,
- t, tn,
- v, vn,
- dt;
- FILE *h = fopen("./eulerdt.dat","w+");
- for(int k = 0; k < Ndt; k++)
- {
- dt = dt0 + k * ddt;
- x = x0; t = t0; v = v0;
- double E0 = E(x,v);
- for (int i = 0; i < ((tf-t0)/dt); ++i)
- {
- vn = v + dvdt(x,v,t)*dt;
- xn = x + vn*dt;
- tn = t + dt;
- x = xn;
- v = vn;
- t = tn;
- }
- fprintf(h, "%f %f\n", dt, E(x,v)/E0-1);
- }
- fclose(h);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement