Program 16
Program 16
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>
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>
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);
printf("\nx0\ty0\tk1\tk2\tk3\tk4\tyn\n");
yn = y0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
y0 = yn;
x0 = x0 + h;
}
return 0;
}
OUTPUT(1)