Skip to main content

Posts

Showing posts with the label Numerical Methods

Gauss-Jacobi Method using C

/* Gauss-Jacobi Method Equation:8x+2y-2z=0,x-8y+3y=-4,2x+y+9z=12 */ #include<conio.h> #include<stdio.h> void main () { float ax , ay , az , x , y , z ; int i , n ; float f1 ( float , float ); float f2 ( float , float ); float f3 ( float , float ); clrscr (); printf ( " \n Enter the number of equations:: " ); scanf ( "%d" , & n ); x = y = z = 0 ; for ( i = 0 ; i <= n ; i ++ ) { ax = f1 ( y , z ); ay = f2 ( z , x ); az = f3 ( x , y ); x = ax ; y = ay ; z = az ; } printf ( " \n x= %f, y= %f, z= %f" , x , y , z ); getch (); } float f1 ( float y , float z ) { return ( 8 - 2 * y + 2 * z ) / 8 ; } float f2 ( float z , float x ) { return ( 4 + x + 3 * z ) / 8 ; } float f3 ( float x , float y ) { return ( 12 - 2 * x - y ) / 9 ; }

Gauss-Seidal Method using C

/* Gauss-Seidal Method Equation:8x+2y-2z=0,x-8y+3y=-4,2x+y+9z=12 */ #include<conio.h> #include<stdio.h> void main () { float ax , ay , az , x , y , z ; int i , n ; float f1 ( float , float ); float f2 ( float , float ); float f3 ( float , float ); clrscr (); printf ( " \n Enter the number of equations:: " ); scanf ( "%d" , & n ); x = y = z = 0 ; for ( i = 0 ; i <= n ; i ++ ) { ax = f1 ( y , z ); ay = f2 ( z , ax ); az = f3 ( ax , ay ); x = ax ; y = ay ; z = az ; } printf ( " \n x= %f, y= %f, z= %f" , x , y , z ); getch (); } float f1 ( float y , float z ) { return ( 8 - 2 * y + 2 * z ) / 8 ; } float f2 ( float z , float x ) { return ( 4 + x + 3 * z ) / 8 ; } float f3 ( float x , float y ) { return ( 12 - 2 * x - y ) / 9 ; }

Newtons forward formula Using C

/* Newtons forward formula x: 1 1.10 1.20 1.30 y: .4158 .8912 .9320 .9636 Compute::f(1.02)=? */ #include<conio.h> #include<stdio.h> #define n 4 void main () { float x [ n ], y [ n ][ n ], xf , fx , u , h ; int j , i , index , f = 1 ; for ( i = 0 ; i < n ; i ++ ) { printf ( " \n Enter x[%d] & y[%d][0]" , i + 1 , i + 1 ); scanf ( "%f%f" , & x [ i ], & y [ i ][ 0 ]); } for ( j = 1 ; j < n ; j ++ ) { for ( i = 0 ; i < n ; i ++ ) y [ i ][ j ] = y [ i + 1 ][ j - 1 ] - y [ i ][ j - 1 ]; } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n - 1 ; j ++ ) printf ( "%f \t " , y [ i ][ j ]); printf ( " \n " ); } printf ( " \n Enter x" ); scanf ( "%f" , & xf ); for ( i = 0 ; i < n - 1 ; i ++ ) { if ( xf >= x [ i ] && xf <= x [ i + 1 ]) { index = i ; break ; } } h = x [ 1 ] - x [ 0 ]; u = ( xf - x [ index ]) / h ; fx = y [ index ][ 0 ]; for ( i

Newtons backward formula using C

/* Newtons backward formula x: 1 1.10 1.20 1.30 y: .4158 .8912 .9320 .9636 Compute::f(1.40)=? */ #include<conio.h> #include<stdio.h> #define n 4 void main () { float x [ n ], y [ n ][ n ], xf , fx , u , h ; int j , i , index , f = 1 ; for ( i = 0 ; i < n ; i ++ ) { printf ( " \n Enter x[%d] & y[%d][0]" , i + 1 , i + 1 ); scanf ( "%f%f" , & x [ i ], & y [ i ][ 0 ]); } for ( j = 1 ; j < n ; j ++ ) { for ( i = 0 ; i < n ; i ++ ) y [ i ][ j ] = y [ i + 1 ][ j - 1 ] - y [ i ][ j - 1 ]; } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n - 1 ; j ++ ) printf ( "%f \t " , y [ i ][ j ]); printf ( " \n " ); } printf ( " \n Enter x" ); scanf ( "%f" , & xf ); index = n - 1 ; h = x [ 1 ] - x [ 0 ]; u = ( xf - x [ index ]) / h ; fx = y [ index ][ 0 ]; for ( i = 1 ; i < n ; i ++ ) { if ( i != 1 ) u = u * ( u + i - 1 ); f = f * i ; fx = fx + ( u * y [ index ]

Lagranges formula using C

/* Lagranges formula x: 30 35 45 55 y: 148 196 68 34 Compute::f(40)=? */ #include<conio.h> #include<stdio.h> #define n 4 void main () { float f , x [ n ], y [ n ], xf , fx ; int j , i ; for ( i = 0 ; i < n ; i ++ ) { printf ( " \n Enter x[%d] & y[%d]" , i + 1 , i + 1 ); scanf ( "%f%f" , & x [ i ], & y [ i ]); } printf ( " \n Enter x" ); scanf ( "%f" , & xf ); fx = 0 ; for ( i = 0 ; i < n ; i ++ ) { f = 1 ; for ( j = 0 ; j < n ; j ++ ) { if ( i != j ) f = f * (( xf - x [ j ]) / ( x [ i ] - x [ j ])); } fx = fx + ( f * y [ i ]); } printf ( " \n Result %f" , fx ); getch (); }

Runga-Kutta Method using C

/* Runga-Kutta Method COmpute:: f(0.2)=? When f(x,y)=x+y^2; y(0)=1 h=0.1 */ #include<conio.h> #include<stdio.h> void main () { float x , y , h , xf , k1 , k2 , k3 , k4 , n ; int i ; float f ( float , float ); printf ( " \n Enter x0 & y0" ); scanf ( "%f%f" , & x , & y ); printf ( " \n Enter x & h" ); scanf ( "%f%f" , & xf , & h ); n = ( xf - x ) / h ; for ( i = 0 ; i <= ( int ) n ; i ++ ) { 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 = y + ( k1 + 2 * ( k2 + k3 ) + k4 ) / 6 ; x = x + h ; } printf ( " \n Result %f" , y ); getch (); } float f ( float x , float y ) { return x + y * y ; }

C Program to solve using Euler Rule

/* Euler Rule Compute:: f(0.1)=? When f(x,y)=1+xy, y(0)=2,h=0.01 */ #include<conio.h> #include<stdio.h> void main () { float x0 , y0 , h , xf , fx , x ; float f ( float , float ); printf ( " \n Enter x0 & y0" ); scanf ( "%f%f" , & x0 , & y0 ); printf ( " \n Enter x & h" ); scanf ( "%f%f" , & x , & h ); xf = x0 ; fx = y0 ; while ( xf <= x ) { fx = fx + h * f ( xf , fx ); xf = xf + h ; } printf ( " \n Result %f" , fx ); getch (); } float f ( float x , float y ) { return 1 + x * y ; }

C Program to implement Simpson's 1/3 rd Rule

/* Simpson's 1/3 Rule Equation: x/(1+x) dx lower limit=0 upper limit=1 number of interval=6 */ #include<conio.h> #include<stdio.h> void main () { float a , b , h , x , y , so = 0 ; float x0 , y0 , yn , xn , r , se = 0 ;; int i , n ; float f ( float ); clrscr (); printf ( " \n Enter lower limit value:: " ); scanf ( "%f" , & a ); printf ( " \n Enter upper limit value:: " ); scanf ( "%f" , & b ); printf ( " \n Enter the number of intervals:: " ); scanf ( "%d" , & n ); h = ( b - a ) / n ; x0 = a ; y0 = f ( x0 ); yn = f ( b ); x = x0 + h ; for ( i = 1 ; i <= n - 1 ; i = i + 2 ) { y = f ( x ); so = so + y ; x = x + 2 * h ; } x = x0 + 2 * h ; for ( i = 2 ; i <= n - 2 ; i = i + 2 ) { y = f ( x ); se = se + y ; x = x + 2 * h ; } r = ( h / 3 ) * ( y0 + yn + 4 * so + 2 * se ); printf ( " \n Result is:: %f" , r ); getch (); } float f ( float x ) {

C Program to implement Trapezoidal Rule

/* Trapezoidal Rule Equation: x/(1+x) dx lower limit=0 upper limit=1 number of interval=6 */ #include<conio.h> #include<stdio.h> void main () { float a , b , h , x , y , s = 0 ; float x0 , y0 , xn , yn , r ; int i , n ; float f ( float ); clrscr (); printf ( " \n Enter lower limit value:: " ); scanf ( "%f" , & a ); printf ( " \n Enter upper limit value:: " ); scanf ( "%f" , & b ); printf ( " \n Enter the number of intervals:: " ); scanf ( "%d" , & n ); h = ( b - a ) / n ; x0 = a ; y0 = f ( a ); yn = f ( b ); x = x0 + h ; for ( i = 1 ; i <= n - 1 ; i ++ ) { y = f ( x ); s = s + y ; x = x + h ; } r = ( h / 2 ) * ( y0 + yn + 2 * s ); printf ( " \n Result is:: %f" , r ); getch (); } float f ( float x ) { return x / ( 1 + x ); }

Implement Secant Method using C

/* Secant Method Equation: x^3-4x-9=0 */ #include<conio.h> #include<stdio.h> void main () { float a , b , k , c ; int i ; float f ( float ); clrscr (); printf ( " \n Enter first initial value:: " ); scanf ( "%f" , & a ); printf ( " \n Enter second initial value:: " ); scanf ( "%f" , & b ); for ( i = 1 ; i <= 5 ; i ++ ) { c = b - (( b - a ) * ( f ( b ) / ( f ( b ) - f ( a )))); a = b ; b = c ; } printf ( " \n Root is:: %f" , c ); getch (); } float f ( float x ) { return ( x * x * x ) - ( 4 * x ) - 9 ; }

Method of Regula-Falsi using C

/* Method of Regula-Falsi Equation: x^3-4x-9=0 */ #include<conio.h> #include<stdio.h> void main () { float a , b , k , c ; int i ; float f ( float ); clrscr (); printf ( " \n Enter first initial value:: " ); scanf ( "%f" , & a ); printf ( " \n Enter second initial value:: " ); scanf ( "%f" , & b ); for ( i = 1 ; i <= 5 ; i ++ ) { c = b - (( b - a ) * ( f ( b ) / ( f ( b ) - f ( a )))); k = f ( c ); if ( k > 0 ) b = c ; if ( k <= 0 ) a = c ; } printf ( " \n Root is:: %f" , c ); getch (); } float f ( float x ) { return ( x * x * x ) - ( 4 * x ) - 9 ; }

Implement Newton-Raphson Method using C

/* Method of Newton-Raphson Equation: x^3-4x-9=0 */ #include<stdio.h> void main () { float a , h ; int i ; float f ( float ); float f1 ( float ); printf ( " \n Enter initial value:: " ); scanf ( "%f" , & a ); for ( i = 1 ; i <= 5 ; i ++ ) { h =- f ( a ) / f1 ( a ); a = a + h ; } printf ( " \n Root is:: %f" , a ); } float f ( float x ) { return ( x * x * x ) - ( 4 * x ) - 9 ; } float f1 ( float x ) { return ( 3 * x * x ) - 4 ; }