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

Program 16

Uploaded by

Abhishek Negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Program 16

Uploaded by

Abhishek Negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM 16

Name: Abhishek Negi


Roll No: 05
Section: A2

Aim: Write a program in “C” Language to find the numerical solution of ordinary
differential equations by Euler’s Method.
PROGRAM
Algorithm
1. Input x0, y0, h, target_x
2. Define f(x, y) // Function that calculates dy/dx
3. Initialize x = x0, y = y0
4. While x < target_x do:
y_new = y + h * f(x, y)
x=x+h
y = y_new
End While
5. Output y // Approximate solution at x = target_x
Program

#include<stdio.h>
#include<stdlib.h>

float f(float x , float y){


return (x+y);
}
float f(float x , float y){
return (x-y)/(x+y);
}

int main(){
float x0,y0,xn,yn;
printf("Enter the value of x0 : ");
scanf("%f",&x0);
printf("Enter the value of y0 : ");
scanf("%f",&y0);
printf("Enter the calculationpoint xn : ");
scanf("%f",&xn);
int n;
printf("Enter the total number of steps : ");
scanf("%d",&n);
float h=(xn-x0)/n;
printf("\nx0\ty0\tslope\tyn\n");
for(int i=0;i<n;++i){
float slope= f(x0,y0);
yn=y0+h* slope;
printf("%.3f\t%.3f\t%.3f\t%.3f\n",x0,y0,slope,yn);
y0=yn;
x0=x0+h;
}
printf("\nValue of y at x=%2f is %.2f\n",xn,yn);

return 0;
}

OUTPUT(1)

OUTPUT(2)
PROGRAM 17
Name: Abhishek Negi
Roll No: 05
Section: A2

Aim: Write a program in “C” Language to find the numerical solution of ordinary
differential equations by Runge Kutta (Order 4) Method.
PROGRAM
Algorithm
1. Input x0, y0, h, target_x
2. Define f(x, y) // Function that calculates dy/dx
3. Initialize x = x0, y = y0
4. While x < target_x do:
k1 = h * f(x, y)
k2 = h * f(x + h/2, y + k1/2)
k3 = h * f(x + h/2, y + k2/2)
k4 = h * f(x + h, y + k3)
y_new = y + (k1 + 2*k2 + 2*k3 + k4) / 6
x=x+h
y = y_new
End While
5. Output y // Approximate solution at x = target_x
Program

#include<stdio.h>
#include<stdlib.h>

// Function to define dy/dx = f(x, y)


float f(float x, float y) {
return (x + y);
}

int main() {
float x0, y0, xn, yn;
int n;
// Input initial conditions
printf("Enter the value of x0: ");
scanf("%f", &x0);
printf("Enter the value of y0: ");
scanf("%f", &y0);
printf("Enter the calculation point xn: ");
scanf("%f", &xn);
printf("Enter the total number of steps: ");
scanf("%d", &n);

// Calculate step size


float h = (xn - x0) / n;

printf("\nx0\ty0\tk1\tk2\tk3\tk4\tyn\n");

for (int i = 0; i < n; ++i) {


float k1 = h * f(x0, y0);
float k2 = h * f(x0 + h / 2, y0 + k1 / 2);
float k3 = h * f(x0 + h / 2, y0 + k2 / 2);
float k4 = h * f(x0 + h, y0 + k3);

yn = y0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6;

printf("%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", x0, y0, k1, k2, k3, k4, yn);

y0 = yn;
x0 = x0 + h;
}

printf("\nValue of y at x = %.2f is %.2f\n", xn, yn);

return 0;
}

OUTPUT(1)

You might also like