Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:25:37+00:00 2026-05-31T00:25:37+00:00

I have a problem with a series of functions. I have an array of

  • 0

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 sys which depends on a integer variable, lets say, j, and I want to return them according to this j , i mean, if i want the equation number j, for example, i just write sys(j)
For this, i used a for 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.

I let this part of the code here: (c is just the j integer i used to explain before)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
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<N;l++)
    {
        for(m=0;m<N;m++)
        {
            for(n=0;n<N;n++)
            {
                t[l][m] += j[l][n] * id[n][m];
            }
        }
    }

    // Transpose the matrix (J * Y) -> () t
    for(l=0;l<N;l++)
    {
        for(m=l+1;m<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<N;l++)
    {
        for(m=0;m<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<(N*(N+1));l++)
    {
        if(c==l)
        {
            return flat[c];
        }
    }
}

EDIT —————————————————————-

Ok, this is the part of the code where i use the function

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<rmax;r++){   
    y[0]=19;
    y[1]=20;
    y[2]=50;
    for(j=N;j<NN;j++) y[j]=0;
    for(j=N;j<NN;j=j+3) y[j]=1; // Identity matrix for y from 3 to 11 
    while(t<tf){
        /* RK4 step 1 */
        for(j=0;j<NN;j++){
            k1[j] = sys(j,r,y)*dt;
            z[j] = y[j] + k1[j]*0.5;
        }
        /* RK4 step 2 */
        for(j=0;j<NN;j++){
            k2[j] = sys(j,r,z)*dt;
            z[j] = y[j] + k2[j]*0.5;
        }
        /* RK4 step 3 */
        for(j=0;j<NN;j++){
            k3[j] = sys(j,r,z)*dt;
            z[j] = y[j] + k3[j];
        }
        /* RK4 step 4 */
        for(j=0;j<NN;j++){
            k4[j] = sys(j,r,z)*dt;
        }
        /* Updating y matrix with new values */
        for(j=0;j<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; 
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:25:39+00:00Added an answer on May 31, 2026 at 12:25 am

    Since you’re actually computing all these values at the same time, what you really want is for the function to return them all together. The easiest way to do this is to pass in a pointer to an array, into which the function will write the values. Or perhaps two arrays; it looks to me as if the output of your function is (conceptually) a 3×3 matrix together with a length-3 vector.

    So the declaration of sys would look something like this:

    void sys(double v[3], double JYt[3][3], double r, const double y[12]);
    

    where v would end up containing the first three elements of your flat and JYt would contain the rest. (More informative names are probably possible.)

    Incidentally, the for loop at the end of your code is exactly equivalent to just saying return flat[c]; except that if c happens not to be >=0 and <N*(N+1) then control will just fall off the end of your function, which in practice means that it will return some random number that almost certainly isn’t what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of commands (calls to bash shell functions really) that compute
I've been struggling with the following problem. I have a series of function objects,
I am trying to solve this problem. I have a series of SELECT statements
I have problem with return statment >.< I want to store all magazine names
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run
I have a series of functions which populate a series of drop down boxes.
I have a series of session variables in an array. When I use quotes
I have a working flot graph, showing dynamically from sql using ajax. My problem
I have a problem understanding some code fragment from Matlab Signal processing Toolbox fir2()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.