answersLogoWhite
Ask
C Programming
Numerical Series Expansion

C programming of Runge-Kutta method?

252627
Answer

Top Answer
User Avatar
Wiki User
2011-01-25 14:29:14
2011-01-25 14:29:14

PROGRAM :-

/* Runge Kutta for a set of first order differential equations */

#include

#include

#define N 2 /* number of first order equations */

#define dist 0.1 /* stepsize in t*/

#define MAX 30.0 /* max for t */

FILE *output; /* internal filename */

void runge4(double x, double y[], double step); /* Runge-Kutta function */

double f(double x, double y[], int i); /* function for derivatives */

void main()

{

double t, y[N];

int j;

output=fopen("osc.dat", "w"); /* external filename */

y[0]=1.0; /* initial position */

y[1]=0.0; /* initial velocity */

fprintf(output, "0\t%f\n", y[0]);

for (j=1; j*dist<=MAX ;j++) /* time loop */

{

t=j*dist;

runge4(t, y, dist);

fprintf(output, "%f\t%f\n", t, y[0]);

}

fclose(output);

}

void runge4(double x, double y[], double step)

{

double h=step/2.0, /* the midpoint */

t1[N], t2[N], t3[N], /* temporary storage arrays */

k1[N], k2[N], k3[N],k4[N]; /* for Runge-Kutta */

int i;

for (i=0;i

{

t1[i]=y[i]+0.5*(k1[i]=step*f(x,y,i));

}

for (i=0;i

{

t2[i]=y[i]+0.5*(k2[i]=step*f(x+h, t1, i));

}

for (i=0;i

{

t3[i]=y[i]+ (k3[i]=step*f(x+h, t2, i));

}

for (i=0;i

{

k4[i]= step*f(x+step, t3, i);

}

for (i=0;i

{

y[i]+=(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;

}

}

double f(double x, double y[], int i)

{

if (i==0)

x=y[1]; /* derivative of first equation */

if (i==1)

x= -0.2*y[1]-y[0]; /* derivative of second equation */

return x;

}

123
0

Related Questions

User Avatar

C++ allows any combination of structured programming, procedural programming, object-oriented programming and template metaprogramming.

User Avatar

C and it's variants (C++, C#, etc...) are all programming languages. C is simply a method of using words and alphanumeric characters to create a set of instructions that form a program.

User Avatar

Hafner/Maass have done it in Fortran. Maybe of help by translating?

User Avatar

c programming is structured programming

User Avatar

Programming with C++ is writing computer programs using the C++ programming language.


Copyright © 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.