₦airaland Forum

Welcome, Guest: Register • Login • With Google • Trending • Recent • New

Stats: 3,344,217 members, 8,510,702 topics. Date: Friday, 30 October 2026 at 09:40 PM

Toggle theme

Help With A C Program Code To Solve A Differential Eqn Using Rungekutta 4th Ord - Programming - Nairaland

Nairaland Forum › Science/Technology › Programming › Help With A C Program Code To Solve A Differential Eqn Using Rungekutta 4th Ord (722 Views)

1 Reply

Help With A C Program Code To Solve A Differential Eqn Using Rungekutta 4th Ord by afotrick(op): 9:26pm On Feb 23, 2015
hello.any one advanced in runge kutta 4th order and c programing? Am just an undergraduate working on it. I need help with this code. It sussessfully solves a system of differential equations using runge kutta 4th order, but my lecturer wants something else. something i dont know how to do. the code originally has the question already in the program, but what am required to do is make the program ask the question like an interface asking the question and parameters to solve. i believe u will understand when i post the code.


‪#‎define‬ X0 1.0
#define Xn 2.0
#define STEPLEN 10
#define Y1 1 //initial condition for y1 at x=0
#define Y2 0 //initial condition for y2 at x=0
‪#‎include‬ <stdio.h>
#include <math.h>
#include <stdlib.h>
double f1(double x,double y1,double y2)
{
//a function to solve the functions 2 using x and y
return(y2);
}
double f2(double x,double y1,double y2)
{
//a function to solve the functions 1 using x and y
return(x*log(x) - (2*y1)/(x*x) + (2*y2)/x);
}
main()
{
double x,y1[STEPLEN],y2[STEPLEN];
double xmid,k1[2],k2[2],k3[2],k4[2],h = (double)(Xn - X0)/STEPLEN;
x = X0;
y1[0]=Y1;
y2[0]=Y2;
int i;
printf("This are the initial values. y1=%.10f y2=%.10f and h=%f\n\n", y1[0], y2[0],h);
//printf("x\t|k1\t|k2\t|k3\t|k4\t|y\n\n"wink;
for(i=0; i<STEPLEN; i++){
k1[0]=h * f1(x,y1[i],y2[i]);
k1[1]=h * f2(x,y1[i],y2[i]);
xmid = x + h/2.0;
k2[0] = h * f1(xmid, y1[i] + k1[0] * 1/2.0, y2[i] + k1[1] * 1/2.0);
k2[1] = h * f2(xmid, y1[i] + k1[0] * 1/2.0, y2[i] + k1[1] * 1/2.0);
k3[0] = h * f1(xmid, y1[i] + k2[0] * 1/2.0, y2[i] + k2[1] * 1/2.0);
k3[1] = h * f2(xmid, y1[i] + k2[0] * 1/2.0, y2[i] + k2[1] * 1/2.0);

k4[0] = h * f1(x + h, y1[i] + k3[0] , y2[i] + k3[1]);
k4[1] = h * f2(x + h, y1[i] + k3[0] , y2[i] + k3[1]);

y1[i+1] = y1[i] + (k1[0] + 2*k2[0] + 2*k3[0] + k4[0])/6;

y2[i+1] = y2[i] + (k1[1] + 2*k2[1] + 2*k3[1] + k4[1])/6;

printf("When x=%f\n",x+h);
printf("k1 for y1=%.10f\n",k1[0]);
printf("k1 for y2=%.10f\n",k1[1]);
printf("k2 for y1=%.10f\n",k2[0]);
printf("k2 for y2=%.10f\n",k2[1]);
printf("k3 for y1=%.10f\n",k3[0]);
printf("k3 for y2=%.10f\n",k3[1]);
printf("k4 for y1=%.10f\n",k4[0]);
printf("k4 for y2=%.10f\n",k4[1]);
printf("y1 when x is %f=\t%.10f\n",x+h,y1[i+1]);
printf("y2 when x is %f=\t%.10f\n\n",x+h,y2[i+1]);
// printf("%f %f %f %f %f %f %f %f %f %f \n",k1[0],k1[1],k2[0],k2[1],k3[0],k3[1],k4[0],k4[1],y1[i+1],y2[i+1]);
x = x+h;

}
system("pause"wink;
}
please if u dont mind contact me by email @ afotrick2011@gmail.com Please. your help will be very much apprciated.
Re: Help With A C Program Code To Solve A Differential Eqn Using Rungekutta 4th Ord by GoodBoi1(m):
Do you mean GUI or just want the program to ask for input( you can use scanf function)?
1 Reply

Help! Im Stuck With This C# Program • Matlab/simulink Specialist(in Benin City) Who Can Model A Differential Relay • Please Programmars Help Me Out With This C# Program! I Have Test Tommorrow!!!!! • 2 • 3 • 4

An App Developer Needed For A Project • Surfi - The All In One Browser For Android • Check Out Thiscovenant University Alumni Innovation