Note that there are some explanatory texts on larger screens.

plurals
  1. POC array of functions
    primarykey
    data
    text
    <p>I have a problem with a series of functions. I have an array of 'return values' (i compute them through matrices) from a single function <strong>sys</strong> which depends on a integer variable, lets say, <strong>j</strong>, and I want to return them according to this <strong>j</strong> , i mean, if i want the equation number <strong>j</strong>, for example, i just write <strong>sys(j)</strong> For this, i used a <strong>for</strong> loop but i don't know if it's well defined, because when i run my code, i don't get the right values. Is there a better way to have an array of functions and call them in a easy way? That would make easier to work with a function in a Runge Kutta method to solve a diff equation.</p> <p>I let this part of the code here: (<strong>c</strong> is just the <strong>j</strong> integer i used to explain before)</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; int N=3; double s=10.; //float r=28.; double b=8.0/3.0; / * Define functions * / double sys(int c,double r,double y[]) { int l,m,n,p=0; double tmp; double t[3][3]={0}; double j[3][3]={{-s,s,0},{r-y[2],-1,-y[0]},{y[1],y[0],-b}}; //Jacobiano double id[3][3] = { {y[3],y[6],y[9]} , {y[4],y[7],y[10]} , {y[5],y[8],y[11]} }; double flat[N*(N+1)]; // Multiplication of matrices J * Y for(l=0;l&lt;N;l++) { for(m=0;m&lt;N;m++) { for(n=0;n&lt;N;n++) { t[l][m] += j[l][n] * id[n][m]; } } } // Transpose the matrix (J * Y) -&gt; () t for(l=0;l&lt;N;l++) { for(m=l+1;m&lt;N;m++) { tmp = t[l][m]; t[l][m] = t[m][l]; t[m][l] = tmp; } } // We flatten the array to be left in one array for(l=0;l&lt;N;l++) { for(m=0;m&lt;N;m++) { flat[p+N] = t[l][m]; } } flat[0] = s*(y[1]-y[0]); flat[1] = y[0]*(r-y[2])-y[1]; flat[2] = y[0]*y[1]-b*y[2]; for(l=0;l&lt;(N*(N+1));l++) { if(c==l) { return flat[c]; } } } </code></pre> <p>EDIT ----------------------------------------------------------------</p> <p>Ok, this is the part of the code where i use the function</p> <pre><code>int main(){ output = fopen("lyapcoef.dat","w"); int j,k; int N2 = N*N; int NN = N*(N+1); double r; double rmax = 29; double t = 0; double dt = 0.05; double tf = 50; double z[NN]; // Temporary matrix for RK4 double k1[N2],k2[N2],k3[N2],k4[N2]; double y[NN]; // Matrix for all variables /* Initial conditions */ double u[N]; double phi[N][N]; double phiu[N]; double norm; double lyap; //Here we integrate the system using Runge-Kutta of fourth order for(r=28;r&lt;rmax;r++){ y[0]=19; y[1]=20; y[2]=50; for(j=N;j&lt;NN;j++) y[j]=0; for(j=N;j&lt;NN;j=j+3) y[j]=1; // Identity matrix for y from 3 to 11 while(t&lt;tf){ /* RK4 step 1 */ for(j=0;j&lt;NN;j++){ k1[j] = sys(j,r,y)*dt; z[j] = y[j] + k1[j]*0.5; } /* RK4 step 2 */ for(j=0;j&lt;NN;j++){ k2[j] = sys(j,r,z)*dt; z[j] = y[j] + k2[j]*0.5; } /* RK4 step 3 */ for(j=0;j&lt;NN;j++){ k3[j] = sys(j,r,z)*dt; z[j] = y[j] + k3[j]; } /* RK4 step 4 */ for(j=0;j&lt;NN;j++){ k4[j] = sys(j,r,z)*dt; } /* Updating y matrix with new values */ for(j=0;j&lt;NN;j++){ y[j] += (k1[j]/6.0 + k2[j]/3.0 + k3[j]/3.0 + k4[j]/6.0); } printf("%lf %lf %lf \n",y[0],y[1],y[2]); t += dt; } </code></pre>
    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.
    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