0% found this document useful (0 votes)
34 views7 pages

NUmerical Method

This document contains code for three different numerical methods: 1) Euler's method for numerically solving differential equations using forward Euler steps 2) Runge-Kutta method (RK4), a higher-order method that uses multiple intermediate slope estimates to provide a more accurate solution than Euler's method 3) Taylor series method for approximating functions as an infinite sum involving derivatives, implemented here to sum the first n terms of the series.

Uploaded by

Rashmi Dahal
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)
34 views7 pages

NUmerical Method

This document contains code for three different numerical methods: 1) Euler's method for numerically solving differential equations using forward Euler steps 2) Runge-Kutta method (RK4), a higher-order method that uses multiple intermediate slope estimates to provide a more accurate solution than Euler's method 3) Taylor series method for approximating functions as an infinite sum involving derivatives, implemented here to sum the first n terms of the series.

Uploaded by

Rashmi Dahal
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/ 7

//Euler's method

#include<stdio.h>

//#include<conio.h>

#define f(x,y) x+y

int main()

float x0, y0, xn, h, yn, slope;

int i, n;

//clrscr();

printf("Enter Initial Condition\n");

printf("x0 = ");

scanf("%f", &x0);

printf("y0 = ");

scanf("%f", &y0);

printf("Enter calculation point xn = ");

scanf("%f", &xn);

printf("Enter number of steps: ");

scanf("%d", &n);

/* Calculating step size (h) */

h = (xn-x0)/n;

/* Euler's Method */

printf("\nx0\ty0\tslope\tyn\n");

printf("------------------------------\n");

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

{
slope = f(x0, y0);

yn = y0 + h * slope;

printf("%.4f\t%.4f\t%0.4f\t%.4f\n",x0,y0,slope,yn);

y0 = yn;

x0 = x0+h;

/* Displaying result */

printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);

//getch();

return 0;

Output of Euler’s method


2 RK-4 method

#include<stdio.h>
//#include<conio.h>

#define f(x,y) (y*y-x*x)/(y*y+x*x)

int main()
{
float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
int i, n;
//clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);

/* Calculating step size (h) */


h = (xn-x0)/n;

/* Runge Kutta Method */


printf("\nx0\ty0\tyn\n");
for(i=0; i < n; i++)
{
k1 = h * (f(x0, y0));
k2 = h * (f((x0+h/2), (y0+k1/2)));
k3 = h * (f((x0+h/2), (y0+k2/2)));
k4 = h * (f((x0+h), (y0+k3)));
k = (k1+2*k2+2*k3+k4)/6;
yn = y0 + k;
printf("%0.4f\t%0.4f\t%0.4f\n",x0,y0,yn);
x0 = x0+h;
y0 = yn;
}

/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);

// getch();
return 0;
}
//Taylor's series

#include<stdio.h>

#include<math.h>

int main()

int x,i;

int fact = 1,n;

float sum=0;

printf("\n\nEnter the value of x in the series : ");

scanf("%d",&x);

printf("\nEnter the number of terms in the series : ");

scanf("%d",&n);

for(i=1;i<n;i++)

fact = fact*i;

sum = sum + (pow(x,i)/fact) ;

sum= sum +1; //Since series starts with 1

printf("\n\nThe sum of the taylor series is : %.2f\n\n",sum);


return 0;

Output for taylor’s series

You might also like