Note that there are some explanatory texts on larger screens.

plurals
  1. PORunge Kutta in C for Lorenz equation
    primarykey
    data
    text
    <p>i'm trying to compute the Lorenz system using Runge Kutta method, but i can't find where my code have an error. When I run it, the system goes to a static point and i should obtain a butterfly (the Lorenz attractor). I think it's something on the 'for'loops in the Runge Kutta method section. Here's my code</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;math.h&gt; double f(int,double,double []); double s,r,b; FILE *output; int main() { output=fopen("lorenzdata.dat","w"); int j,N=3; double x[2],dt,y[2],K1[2],K2[2],K3[2],K4[2],ti,t,i; printf("\n Introduce parameters s, r and b sepparated by a space:"); scanf("%lf %lf %lf",&amp;s,&amp;r,&amp;b); printf("\n Introduce initial conditions t0,x0,y0 and z0:"); scanf("%lf %lf %lf %lf",&amp;ti,&amp;x[0],&amp;x[1],&amp;x[2]); printf("\n Introduce time step:"); scanf("%lf",&amp;dt); printf("\n Specify time to compute in the equations:"); scanf("%lf",&amp;t); /* Loop para Runge Kutta 4 */ do { /* printf("%9.4f %9.4f %9.4f %9.4f \n",ti,x[0],x[1],x[2]); */ i++; for(j=0;j&lt;N;j++) { K1[j] = f(j,ti,x); } for(j=0;j&lt;N;j++) { y[j] = x[j]+(K1[j]/2)*dt; } for(j=0;j&lt;N;j++) { K2[j] = f(j,ti+dt/2,y); } for(j=0;j&lt;N;j++) { y[j] = x[j]+(K2[j]/2)*dt; } for(j=0;j&lt;N;j++) { K3[j] = f(j,ti+dt/2,y); } for(j=0;j&lt;N;j++) { y[j] = x[j]+(K3[j])*dt; } for(j=0;j&lt;N;j++) { K4[j] = f(j,ti+dt,y); } for(j=0;j&lt;N;j++) { x[j] += dt*((K1[j]/6)+(K2[j]/3)+(K3[j]/3)+(K4[j]/6)); } ti +=dt; fprintf(output,"%9.4f %9.4f %9.4f \n",x[0],x[1],x[2]); }while(i*dt &lt;= t); fclose(output); return 0; } /* Definimos la funcion */ double f(int m,double h,double x[]) { if(m==0) { return s*(x[1]-x[0]); } else if(m==1) { return x[0]*(r-x[2])-x[1]; } else if(m==2) { return x[0]*x[1]-b*x[2]; } } </code></pre> <p>Thanks in advance</p> <p><strong>EDIT</strong></p> <p>I wrote the code again (in a more simplified way) on linux and it runs ok, it seems my editor on Windows had something (maybe the encoding) that when i compiled the code, it throwed a lot of infinites values. Don't know why and it took me a lot of time to notice that. Thanks for your help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload