0% found this document useful (0 votes)
15 views2 pages

RK4 2ND Order (C) JRF

The document is a C program that solves a second-order ordinary differential equation (ODE) using the 4th order Runge-Kutta method. It takes initial conditions and parameters from an input file and outputs the results, including the value of y(1.0) calculated to six decimal places. The program also handles the calculation of a variable R based on the user's roll number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

RK4 2ND Order (C) JRF

The document is a C program that solves a second-order ordinary differential equation (ODE) using the 4th order Runge-Kutta method. It takes initial conditions and parameters from an input file and outputs the results, including the value of y(1.0) calculated to six decimal places. The program also handles the calculation of a variable R based on the user's roll number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

NAME: BISWADEEP CHATTERJEE DATE:


ROLL NO: 11
PROBLEM NO:
PROBLEM: WRITE A PROGRAM IN C TO FIND THE VALUE OF y(1.0) OF THE SECOND ORDER ODE
y" + xyy' + sin(Rx)y = cos(Rx) WITH y(0)=1, y'(0)=1
CONSIDERING THE STEP LENGTH h=0.1, BY USING 4TH ORDER RUNGE-KUTTA METHOD.
(CORRECT UPTO 6D)
WHERE R = r1/10 IF 0 < r1 < 10
= r1/100 IF r1 >= 10
WHERE r1 BEING YOUR CLASS ROLL NO.
*/

#include <stdio.h>
#include <math.h>

float f(float x, float y, float z, float R)


{
return(z);
}

float g(float x, float y, float z, float R)


{
return(cos(R*x)-(x*y*z)-(sin(R*x)*y));
}

int main()
{
float x, y, z, xn, R, h, k, l, k1, k2, k3, k4, l1, l2, l3, l4;
int r1;

FILE *fp1;
FILE *fp2;

fp1 = fopen("2RK4INPUT.dat", "r");


fp2 = fopen("2RK4OUTPUT.out", "w");

if (fp1 == NULL || fp2 == NULL)


{
printf("Error opening file!\n");
return 1;
}

fprintf(fp2, "Here we have considered that y'(x) = z(x). Then the given 2nd
order ODE converted to a system of two first order ODEs\n");
fprintf(fp2,"\n y' = z, \n z' = cos(Rx) - xyz - (sin(Rx)y)\n");
fprintf(fp2,"\nWith initial conditions y(0) = 1, z(0) = y'(0) = 1.\n");
fscanf(fp1, "%d", &r1);
fprintf(fp2, "Enter your roll No: %d\n", r1);
if (r1 < 10)
R = (r1*1.0) / 10.0;
else
R = (r1*1.0) / 100.0;

fscanf(fp1, "%f %f %f", &x, &y, &z);


fprintf(fp2, "Enter the Initial values of x(0), y(0) and z(0): %f\t, %f\t, %f\
n", x, y,z);

fscanf(fp1, "%f", &h);


fprintf(fp2, "Enter the step length %f\n", h);
fscanf(fp1, "%f", &xn);
fprintf(fp2, "Enter the last value of x: %f\n", xn);

fprintf(fp2, "\n x\t k1\t\t l1\t k2\t\t l2\t k3\t\t l3\t


k4\t\t l4\t y\t\t z\n");

while (x < xn)


{
k1 = h * f(x, y, z, R);
l1 = h * g(x, y, z, R);

k2 = h * f(x + (h / 2.0), y + (k1 / 2.0), z + (l1 / 2.0), R);


l2 = h * g(x + (h / 2.0), y + (k1 / 2.0), z + (l1 / 2.0), R);

k3 = h * f(x + (h / 2.0), y + (k2 / 2.0), z + (l2 / 2.0), R);


l3 = h * g(x + (h / 2.0), y + (k2 / 2.0), z + (l2 / 2.0), R);

k4 = h * f(x + h, y + k3, z + l3, R);


l4 = h * g(x + h, y + k3, z + l3, R);

k = (1.0 / 6.0) * (k1 + (2.0 * k2) + (2.0 * k3) + k4);


l = (1.0 / 6.0) * (l1 + (2.0 * l2) + (2.0 * l3) + l4);

y = y + k;
z = z + l;
x = x + h;

fprintf(fp2, "%0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f


%0.6f %0.6f %0.6f %0.6f\t\n", x-h, k1, l1, k2, l2, k3, l3, k4, l4, y, z);
}

fprintf(fp2, "\nThe value of y(1.0) is: %0.6f (Correct up to 6 decimal


places)\n", y);

fclose(fp1);
fclose(fp2);

return 0;
}

You might also like