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 7512427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:52:01+00:00 2026-05-29T23:52:01+00:00

i’m trying to compute the Lorenz system using Runge Kutta method, but i can’t

  • 0

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

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
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",&s,&r,&b);
    printf("\n Introduce initial conditions t0,x0,y0 and z0:");
    scanf("%lf %lf %lf %lf",&ti,&x[0],&x[1],&x[2]);
    printf("\n Introduce time step:");
    scanf("%lf",&dt);
    printf("\n Specify time to compute in the equations:");
    scanf("%lf",&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<N;j++)
        {
            K1[j] = f(j,ti,x);
        }
        for(j=0;j<N;j++)
        {
            y[j] = x[j]+(K1[j]/2)*dt;
        }
        for(j=0;j<N;j++)
        {
            K2[j] = f(j,ti+dt/2,y);
        }
        for(j=0;j<N;j++)
        {
            y[j] = x[j]+(K2[j]/2)*dt;
        }
        for(j=0;j<N;j++)
        {
            K3[j] = f(j,ti+dt/2,y);
        }
        for(j=0;j<N;j++)
        {
            y[j] = x[j]+(K3[j])*dt;
        }
        for(j=0;j<N;j++)
        {
            K4[j] = f(j,ti+dt,y);
        }
        for(j=0;j<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 <= 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];
    }
}

Thanks in advance

EDIT

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

  • 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-29T23:52:04+00:00Added an answer on May 29, 2026 at 11:52 pm

    The lines that look like

    for(j=0;j<N;j++)
    {
        y[j] = x[j]+(K1[j]/2)*dt;
    }
    

    are wrong.

    It should look like,

    for(j=0;j<N;j++)
    {
        K1[j] = f(j,ti,x[j],y[j]);
        L1[j] = g(j,ti,x[j],y[j]);
    }
    for(j=0;j<N;j++)
    {
        K2[j] = f(j,ti+dt/2,x+k1[j]/2,y+L1[j]/2);
        L2[j] = g(j,ti+dt/2,x+k1[j]/2,y+L1[j]/2);
    }
    for(j=0;j<N;j++)
    {
        K3[j] = f(j,ti+dt/2,x+K2[j]/2,y+L2[j]/2);
        L3[j] = g(j,ti+dt/2,x+K2[j]/2,y+L2[j]/2);
    }
    for(j=0;j<N;j++)
    {
        K4[j] = f(j,ti+dt,x+K3[j],y+L3[j]);
        L4[j] = g(j,ti+dt,x+K3[j],y+L3[j]);
    }
    for(j=0;j<N;j++)
    {
        x[j] += dt*((K1[j]/6)+(K2[j]/3)+(K3[j]/3)+(K4[j]/6));
        y[j] += dt*((L1[j]/6)+(L2[j]/3)+(L3[j]/3)+(L4[j]/6));
    }
    

    Runge-Kutta works like this.

    You have a system of equations.

    dx/dt = f(t,x,y)
    dy/dt = g(t,x,y)

    For each argument of the functions, you need a Runge-Kutta sub step (the k1, k2, etc…).
    So for each sub-step “k”, you need the “sub-step” updated values of x and y. As an example for the 2nd sub-step x+k1/2 and y+l1/2.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.