I am trying to write a program that executes the improved Euler method of curve fitting. I am pretty sure i have written the code correctly to run it, but when it runs, the numbers get massive very quickly, and i worry i;m making a fundamental (C programming) error.
thanks
Code:
int Euler(t, u, v, x0, y0, z0)
float t, u, v, x0, y0, z0;
{
float x[100], y[100], z[100], dxdt_i, dydt_i, dzdt_i,dx, dy, dz, dxdt_new, dydt_new, dzdt_new, dt = 1;
int i;
x[0] = x0;
y[0] = y0;
z[0] = z0;
system("cls");
printf ("\nLorenz formulas:\n\n");
printf("dx dy dz \n");
printf("-- = a(y - x) : -- = x(b - z) - y : -- = xy - cz\n");
printf("dt dt dt\n\n\n");
for (i = 0; i<5; i++)
{
dxdt_i = t*(y[i] - x[i]);
dydt_i = x[i]*(u - z[i]) - y[i];
dzdt_i = (x[i]*y[i]) - (v*z[i]);
dx = (t*(y[i] - x[i]))*dt;
dy = (x[i]*(u - z[i]) - y[i])*dt;
dz = ((x[i]*y[i]) - (v*z[i]))*dt;
dxdt_new = t*(y[i] - (x[i] + dx));
dydt_new = x[i]*(u - z[i]) - (y[i] + dy);
dzdt_new = (x[i]*y[i]) - (v*(z[i] + dz));
x[i+1] = x[i] +(dt/2)*(dxdt_i + dxdt_new);
y[i+1] = y[i] +(dt/2)*(dydt_i + dydt_new);
z[i+1] = z[i] +(dt/2)*(dzdt_i + dzdt_new);
printf("dx dy dz\n");
printf("-- = %.2f : -- = %.2f : -- = %.2f\n", dxdt_i, dydt_i, dzdt_i);
printf("dt dt dt\n");
printf("\nnew xgrad = %.3f new ygrad = %.3f new zgrad = %.3f\n\n", dxdt_new, dydt_new, dzdt_new);
printf("\nnew x = %.3f new y = %.3f new z = %.3f\n\n", x[i+1], y[i+1], z[i+1]);
i = i++;
}
}
P.S at the top where i sent the function the variables for x0, y0 and z0 and then on the next line i do this:
x[0] = x0;
y[0] = y0;
z[0] = z0;
its because i keep getting an error if i try and send the function a varaible to go straight into x[0]. I.e
int Euler(t, u, v, x[0], y[0], z[0])
can you not do this, it would neated things up abit...
also how do you control significant figures, on the off chance that it is working correctly, the numbers are too big and ruin my display.
If you cant control sigfigs, when i write this bit:
Code:
printf("dx dy dz\n");
printf("-- = %.2f : -- = %.2f : -- = %.2f\n", dxdt_i, dydt_i, dzdt_i);
printf("dt dt dt\n");
if the floating variables are big, then it messes up my nice rows. Can i control it so it is nicely tabulated?