0% found this document useful (0 votes)
30 views10 pages

Numerical Method

The document provides code examples for several numerical methods: Bisection Method, Euler's Method, False Position Method, Gauss Method, Kutta's Method, Newton-Raphson Method, Secant Method, and Stirling's Approximation. It includes the code to implement each method and brief descriptions of how each algorithm works to find the root of an equation or approximate a function. The document contains 7 pages of code examples for common numerical analysis techniques.

Uploaded by

Md Rony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
30 views10 pages

Numerical Method

The document provides code examples for several numerical methods: Bisection Method, Euler's Method, False Position Method, Gauss Method, Kutta's Method, Newton-Raphson Method, Secant Method, and Stirling's Approximation. It includes the code to implement each method and brief descriptions of how each algorithm works to find the root of an equation or approximate a function. The document contains 7 pages of code examples for common numerical analysis techniques.

Uploaded by

Md Rony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Numerical Method

Bisection Method:
#include<stdio.h>
#include<math.h>
void main()
{
int i;
float a,b,tol;
float f(float);
float f1(float);
printf("Enter the end points of the interval[a,b]\n");
scanf("%f%f",&a,&b);
if((a)*(b)>0)
printf("The root does not lie between
interval[%.2f,%.2f]\n",a,b);
else
{
printf("Enter the value of tol\n");
scanf("%f",&tol);
a=(a+b)/2;
for(i=1;;++i)
{
a=a-f(a)/f1(a);
if(fabs(f(a))<tol)
break;
}
printf("The approximate real root is=%.13f\n",a);
printf("The function value at %.13f is=%.13f\n",a,f(a));
}
}
float f(float x)
{
return(3*x-cos(x)-1.0);
}
float f1(float x)
{
return(3+sin(x));
}

Page-1
Euler’s Method:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x,y) ((y-x)/(y+x))
void main()
{
float a,b,x,y,t,k,h;
printf("Enter the value of x0,y0,h and xn");
scanf("%f%f%f%f",&a,&b,&h,&t);
x=a;
y=b;
while(x<t)
{
y=y+h*f(x,y);
x=x+h;
printf("x=%f\ty=%f\n",x,y);
}
getch();
}

Page:2
False Mathod:
#include<stdio.h>
#include<math.h>
int main()
{
float x,x0,x1,t,tol;
float f(float);
printf("Enter the value of x0\n");
scanf("%f",&x0);
printf("Enter the value of x1\n");
scanf("%f",&x1);
printf("Enter the value of tol");
scanf("%f",&tol);
if((x0)*f(x1)>0)
{
printf("The root dose not lie between %.2f and %.2f\n",x0,x1);
printf("TRY again\n\n");
}
do
{
x=x0-(f(x0)/(f(x0)-f(x1)))*(x0-x1);
t = fabs(x0 - x);
if(f(x0)*f(x)<0)
x0 = x;
else
x1 = x;
}
while(t>tol);
printf("The approximate real root is: = %f\n",x);

}
float f(float x)
{
float y;
y = pow(x,3)-x-4.0;
return(y);
}
Gauss Method:
#include<stdio.h>
#include<stdio.h>
int main()
{
int i,j,k,n;
float s,x[10],a[20][20],b[20],c[20][20];
printf("Enter the number of equations\n");
scanf("%d",&n);
printf("Enter the coefficient matrix A of AX=b\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&a[i][j]);
printf("Enter the constant vector b of AX=b\n");
for(i=1;i<=n;i++)
scanf("%f",&b[i]);
for(k=1;k<=n;k++)
{
for(i=k+1;i<=n;i++)
{
c[i][k]=a[i][k]/a[k][k];
a[i][k]=0.0;
b[i]=b[i]-(c[i][k]*b[k]);
for(j=k+1;j<=n;j++)
{
a[i][j]=a[i][j]-(c[i][k]*a[k][j]);

}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" a%d%d=%.2f",i,j,a[i][j]);
}
printf("b%d=%.3f\n",i,b[i]);
if(j==n)printf("\n");
}
printf("\n");
x[n]=b[n]/a[n][n];
}

Page:4
Kuttas Mathod:

#include<stdio.h>
#include<math.h>
float f(float x,float y);
int main()
{
float x0,y0,m1,m2,m3,m4,m,y,x,h,xn;
printf("Enter x0,y0,xn,h:");
scanf("%f %f %f %f",&x0,&y0,&xn,&h);
x=x0;
y=y0;
printf("\n\nX\t\tY\n");
while(x<xn)
{
m1=f(x0,y0);
m2=f((x0+h/2.0),(y0+m1*h/2.0));
m3=f((x0+h/2.0),(y0+m2*h/2.0));
m4=f((x0+h),(y0+m3*h));
m=((m1+2*m2+2*m3+m4)/6);
y=y+m*h;
x=x+h;
printf("%f\t%f\n",x,y);
}
}
float f(float x,float y)
{
float m;
m=(x*y);
return m;
}

Page:5
Newton Raphson:
#include<stdio.h>
#include<math.h>
void main()
{
int i;
float a,b,tol;
float f(float);
float f1(float);
printf("Enter the end points of the interval[a,b]\n");
scanf("%f%f",&a,&b);
if((a)*(b)>0)
printf("The root does not lie between
interval[%.2f,%.2f]\n",a,b);
else
{
printf("Enter the value of tol\n");
scanf("%f",&tol);
a=(a+b)/2;
for(i=1;;++i)
{
a=a-f(a)/f1(a);
if(fabs(f(a))<tol)
break;
}
printf("The approximate real root is=%.13f\n",a);
printf("The function value at %.13f is=%.13f\n",a,f(a));
}
}
float f(float x)
{
return(3*x-cos(x)-1.0);
}
float f1(float x)
{
return(3+sin(x));
}

Page: 6
Scant Method:
#include<stdio.h>
#include<math.h>
/*Function whose root is to be determined*/
double f(double x){
return x*x*x-2*x-5;
}
main(){
int iter=1,maxSteps;
double x1,x2,x3,eps;
printf("Enter the accuracy desired: \n");
scanf("%lf",&eps);
printf("Enter the intial guesses: \nx1 = ");
scanf("%lf",&x1);
printf("x2 = ");
scanf("%lf",&x2);
printf("Enter the max number of iterations to be
performed: ");
scanf("%d",&maxSteps);
printf("iter\tx1\t\tx2\t\tx3\t\tf(x3)\n");

printf("____________________________________________________
_______________\n");
do{
x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));

printf("%d\t%lf\t%lf\t%lf\t%lf\n",iter,x1,x2,x3,f(x3));
x1=x2;
x2=x3;
iter++;
}while(fabs(f(x3))>eps&&iter<=maxSteps);
printf("\nOne of the roots is: %lf",x3);
}

Page:7
Stirling Method:
#define PI 3.1416
#define EULERNUM 2.71828

float stirling_approximation(int n) {
int fact;
float stirling, ans;

fact = factorial(n);
stirling = sqrt(2.0*PI*n) * pow(n / EULERNUM, n);
ans = fact / stirling;

return ans;
}

int factorial(int input) {


int i;
int ans = 0;

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


ans += i;
return ans;
}

int main(void) {
int n;
printf(" n\t Ratio\n");
for (n = 1; n <= 12; n++) {
printf("n: %2d\t %f\n", n, stirling_approximation(n));
}

return 0;
}

End

You might also like