Thanks guys for help, I have modified the function now to this:
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;
char name[20], filename[24];
FILE *EulerData;
int i, n;
x[0] = x0;
y[0] = y0;
z[0] = z0;
Sleep(1000);
printf("\nPlease enter the name of the file to store the data in, (example: lorenz)\n");
printf("If it doesnt exist, it will be created.\n\n");
scanf("%s", name);
sprintf(filename, "%s.txt",name);
printf("\nStoring all Data in file '%s'\n", filename);
Sleep(1000);
printf("Beginning execution of improved Euler...\n\n");
Sleep(1000);
system("cls");
for (i = 0; i<101; 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("New x = %5.3f New y = %5.3f New z = %5.3f\n", x[i+1], y[i+1], z[i+1]);
printf("%d\n", i);
}
EulerData = fopen(filename, "w");
for (n = 0; n<101; n++)
{
printf("%d\n", n);
fprintf(EulerData, "%f %f %f\n", x[n], y[n], z[n]);
}
fclose(filename);
}
I added the lines
printf("%d\n", i);
and
printf("%d\n", n);
to see if it was actually running the for loops, which it is because i get the values printed to the console, but it crashes AFTER the n's have been printed. This tells me that the loop is running, but there is nothing in the textfile, which i suspect means it cant close the file, and thats why it crashes - why would that be?
EDIT - I had fclose(filename) not fclose(EulerData)
in reply to vart, should you never have a * before a character array, for instance in another function i have
char *option[100], *as[100], *bs[100], *cs[100], *Xos[100], *Yos[100], *Zos[100];//the s at the end of the name tell me it is stored as a string.
option is waiting for the user to type an option, "exit" etc, and the others are taking data from the user, should they not have the *?