0% found this document useful (0 votes)
28 views9 pages

C Prog

The document discusses several numerical methods including bisection, secant, Newton-Raphson, fixed point, Lagrange interpolation, curve fitting, trapezoidal rule, Simpson's 1/3 rule, and Simpson's 3/8 rule. Code examples are provided for each method in C programming language. The document is intended to teach numerical analysis techniques.

Uploaded by

Biplove Pokhrel
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)
28 views9 pages

C Prog

The document discusses several numerical methods including bisection, secant, Newton-Raphson, fixed point, Lagrange interpolation, curve fitting, trapezoidal rule, Simpson's 1/3 rule, and Simpson's 3/8 rule. Code examples are provided for each method in C programming language. The document is intended to teach numerical analysis techniques.

Uploaded by

Biplove Pokhrel
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/ 9

1 Numerical Methods BSc.

CSIT

C-Programming

Bisection method Secant Method

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
#include<stdlib.h> float f ( float x)
float f ( float x) {
{ float y;
float y; y= pow(x, 2)+x -2;
y= pow(x, 2)+x -2; return y;
return y; }
} void main()
void main() {
{ float x1, x2, x0, error=0.0001;
float x1, x2, x0, error=0.0001; int i=0;
int i=0; printf("\nEnter two initial guess:");
printf("\nEnter two initial guess:"); scanf("%f%f", &x1, &x2);
scanf("%f%f", &x1, &x2); do
if (f(x1 )*f(x2 )>0) {
{ x0=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1));
printf("\nWrong Input!!"); x2=x1;
exit(0); x1=x0;
} i++;
else }while(fabs (f(x0))>error);
{ printf("\nRoot=%f", x0);
do printf("\nNumber of iteration=%d",i);
{ getch();
x0=(x1+x2)/2;
if(f(x0 )*f(x1 )>0) }
x1=x0;
else
x2=x0;
i++;
}while(fabs (f(x0))>error);
}
printf("\nRoot=%f", x0);
printf("\nNumber of iteration=%d",i);
getch();

Prepared By: Jayanta Poudel


2 Numerical Methods BSc.CSIT

Newton Raphson Method Fixed Point Method

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
float f( float x) float g ( float x)
{ {
float y; float y;
y= pow(x, 2)+x -2; y= 2.0-x*x;
return y; return y;
} }
float fd( float x) int main()
{ {
float y; float x0, x, error, E=0.00001;
y= 2*x+1; printf("Input initial estimate of a root:\n");
return y; scanf("%f", &x0);
} while(1)
void main() {
{ x=g(x0);
float x0,x1,error=0.0001; error=(x-x0)/x;
int i=0; if(fabs(error)<E)
printf("\nGuess initial root:"); {
scanf("%f", &x1); printf("\nRoot=%f", x0);
do break;
{ }
x0=x1-(f(x1)/fd(x1)); x0=x;
x1=x0; }
i++; getch();
}while(fabs (f(x0))>error); return 0;
printf("\nRoot=%f", x0);
printf("\nNumber of iteration=%d",i); }
getch();
}

Prepared By: Jayanta Poudel


3 Numerical Methods BSc.CSIT

Lagranges Interpolation Curve Fitting(Fitting Linear Equation)

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
int main() #include<math.h>
{ #define error 0.001
float x[10], f[10], y, sum=0.0, l; int main()
int n, i, j; {
printf("\nInput number of data:"); int i, n;
scanf("%d", &n); float x[10], y[10], sumx=0.0,sumy=0.0;
printf("\nInput data points x(i) & f(i):\n"); float sumxx=0.0, sumxy=0.0;
for(i=0;i<n;i++) float meanx, meany, denom, a, b;
{ printf("how many element?:");
printf("x[%d]=",i); scanf("%d", &n);
scanf("%f", &x[i]); for(i=0;i<n;i++)
printf("f[%d]=",i); {
scanf("%f", &f[i]); printf("x[%d]=",i);
} scanf("%f",&x[i]);
printf("\nFunctional value:"); printf("y[%d]=",i);
scanf("%f", &y); scanf("%f",&y[i]);
for(i=0;i<n;i++) }
{ for(i=0;i<n;i++)
l=1; {
for(j=0;j<n;j++) sumx+=x[i];
{ sumy+=y[i];
if(j!=i) sumxx+=x[i]*x[i];
{ sumxy+=x[i]*y[i];
l=l*(y-x[j])/(x[i]-x[j]); }
} meanx=sumx/n;
} meany=sumy/n;
sum=sum+l*f[i]; denom=n*sumxx-sumx*sumx;
} if(fabs(denom)>error)
printf("\nValue at %f=%f", y, sum); {
getch(); b=(n*sumxy-sumx*sumy)/denom;
return 0; a=meany-b*meanx;
} printf("y=%fx+%f",b,a);
}
else
{
printf("\nNo Solution");
}
getch();
return 0;
}

Prepared By: Jayanta Poudel


4 Numerical Methods BSc.CSIT

Trapezoidal Rule Simpson’s 1/3 Rule

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
float f(float x) float f(float x)
{ {
return (1-exp(-x/2.0)); return (1-exp(-x/2.0));
} }
void main() void main()
{ {
float a, b, h, x, sum=0; float a, b, h, x,ans,sum=0;
int n; int n,i;
printf("Enter initial and final value of printf("Enter initial and final value of
x:\n"); x:\n");
scanf("%f%f", &a, &b); scanf("%f%f", &a, &b);
printf("\nNumber of segments:"); printf("\nNumber of segments:");
scanf("%d", &n); scanf("%d", &n);
h=(b-a)/n; h=(b-a)/n;
for(x=a;x<=b;x=x+h) for(i=1;i<n;i++)
{ {
if(x==a) x=a+i*h;
sum=sum+f(x); if(i%2==0)
else if(x==b) {
sum=sum+f(x); sum=sum+2*f(x);
else }
sum=sum+2*f(x); else{
} sum=sum+4*f(x);
sum=sum*h/2; }
printf("\nIntegral value of f(x)=%f ", sum); }
getch(); ans=(h/3)*(f(a)+f(b)+sum);
} printf("\nIntegral value of f(x)=%f ", ans);
getch();
}

Prepared By: Jayanta Poudel


5 Numerical Methods BSc.CSIT

Simpson’s 3/8 Rule Euler Method

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
float f(float x) float fun( float x, float y)
{ {
return (1-exp(-x/2.0)); float f;
} f = x*y;
void main() return f;
{ }
float a, b, h, x,ans,sum=0; int main()
int n,i; {
printf("Enter initial and final value of int i, n;
x:\n"); float x0, y0, xp, h, y;
scanf("%f%f", &a, &b); printf("Enter initial value of x and y:");
printf("\nNumber of segments:"); scanf("%f%f",&x0,&y0);
scanf("%d", &n); printf("Enter x at which y is required:");
h=(b-a)/n; scanf("%f",&xp);
for(i=1;i<n;i++) printf("Enter step-size,h:");
{ scanf("%f",&h);
x=a+i*h; n=(xp - x0)/h;
if(i%3==0) for(i=0; i < n; i++)
{ {
sum=sum+2*f(x); y=y0+h*fun(x0,y0);
} x0=x0+h;
else{ y0=y;
sum=sum+3*f(x); printf("%f\t%f\n",x0,y);
} }
} printf("\nValue of y at x=%f id %f",x0,y0);
ans=(3*h/8)*(f(a)+f(b)+sum); getch();
printf("\nIntegral value of f(x)=%f ", ans); }
getch();
}

Prepared By: Jayanta Poudel


6 Numerical Methods BSc.CSIT

Heun’s Method 4th Order Runge-Kutta Method

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
float func(float x, float y) float func(float x, float y)
{ {
float f; float f;
f=2.0*y/x;; f=2.0*y/x;;
return f; return f;
} }
int main() int main()
{ {
int i, n; int i,n;
float x0, y0, xp, h, m1, m2; float x0, y0, xp, h, m1, m2, m3, m4;
printf("Enter initial value of x and y:"); printf("Enter initial value of x and y:");
scanf("%f %f", &x0,&y0); scanf("%f %f", &x0,&y0);
printf("Enter x at which y is required:"); printf("Enter x at which y is required:");
scanf("%f", &xp); scanf("%f", &xp);
printf("Enter stepsize,h: "); printf("Enter stepsize,h: ");
scanf("%f", &h); scanf("%f", &h);
n = (xp - x0)/h; n = (xp - x0)/h;
for(i=1; i<=n; i++) for(i=1; i<=n; i++)
{ {
m1 = func(x0,y0); m1 = func(x0,y0);
m2 = func(x0+h, y0+m1*h); m2 = func(x0+0.5*h, y0+0.5*m1*h);
x0 = x0+h; m3 = func(x0+0.5*h, y0+0.5*m2*h);
y0 = y0+0.5*h*(m1+m2); m4 = func(x0+h, y0+m3*h);
printf("%f \t%f\n", x0,y0); x0 = x0+h;
} y0 = y0+(m1+2*m2+2*m3+m4)*h/6;
printf("\nValue of y at x=%f is %f",x0,y0); printf("%f\t%f\n", x0, y0);
getch(); }
return 0; printf("\nValue of y at x=%f is %f",x0, y0);
} getch();
return 0;
}

Prepared By: Jayanta Poudel


7 Numerical Methods BSc.CSIT

Gauss Elimination Method Gauss Jordan Method

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


#include<conio.h> #include<conio.h>
int main() int main()
{ {
int i,j,k,n; int i,j,k,n;
float A[20][20],r,x[10],sum=0.0;
printf("\nEnter the order of matrix: ");
float A[20][20],r,x[10];
scanf("%d",&n); printf("\nEnter the size of matrix: ");
printf("\nEnter the elements of augmented scanf("%d",&n);
matrix row-wise:\n\n"); printf("\nEnter the elements of augmented
for(i=1; i<=n; i++) matrix row-wise:\n");
{ for(i=1; i<=n; i++)
for(j=1; j<=n+1; j++) {
{ for(j=1; j<=n+1; j++)
printf("A[%d][%d] : ",i, j); {
scanf("%f",&A[i][j]); printf(" A[%d][%d]:", i,j);
}
scanf("%f",&A[i][j]);
}
}
/*Generation of upper triangular matrix*/
for(j=1; j<=n; j++) }
{ /* finding diagonal matrix */
for(i=1; i<=n; i++) for(j=1; j<=n; j++)
{ {
if(i>j) for(i=1; i<=n; i++)
{ {
r=A[i][j]/A[j][j]; if(i!=j)
for(k=1; k<=n+1; k++) {
{ r=A[i][j]/A[j][j];
A[i][k]=A[i][k]-r*A[j][k];
for(k=1; k<=n+1; k++)
}
{
}
} A[i][k]=A[i][k]-r*A[j][k];
} }
x[n]=A[n][n+1]/A[n][n]; }
/*backward substitution*/ }
for(i=n-1; i>=1; i--) }
{ printf("\nThe solution is:\n");
sum=0; for(i=1; i<=n; i++)
for(j=i+1; j<=n; j++) {
{ x[i]=A[i][n+1]/A[i][i];
sum=sum+A[i][j]*x[j]; printf("\n x%d=%f\n",i,x[i]);
}
}
x[i]=(A[i][n+1]-sum)/A[i][i];
} getch();
printf("\nThe solution is: \n"); return 0;
for(i=1; i<=n; i++) }
{
printf("\nx%d=%f\t",i,x[i]);
}
getch();
return 0;
}

Prepared By: Jayanta Poudel


8 Numerical Methods BSc.CSIT

Gauss Jacobi Iteration Method Gauss Seidal Iteration Method

/* Arrange system of linear equations in /* Arrange system of linear equations in


diagonally dominant form and convert the diagonally dominant form and convert the
1st equation in tems of 1st variable (f1), 2nd 1st equation in tems of 1st variable (f1), 2nd
equation in terms of 2nd variable (f2) and so equation in terms of 2nd variable (f2) and so
on */ on */

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
#define f1(x,y,z) (15-y-z)/10 #define f1(x,y,z) (15-y-z)/10
#define f2(x,y,z) (24-x-z)/10 #define f2(x,y,z) (24-x-z)/10
#define f3(x,y,z) (33-x-y)/10 #define f3(x,y,z) (33-x-y)/10

int main() int main()


{ {
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2,
e3, e; e3, e;
int i=1; int i=1;
printf("Enter the allowed error:\n"); printf("Enter the allowed error:\n");
scanf("%f", &e); scanf("%f", &e);
printf("\ni\tx\ty\tz\n"); printf("\ni\tx\ty\tz\n");
do do
{ {
/* Calculation */ /* Calculation */
x1 = f1(x0,y0,z0); x1 = f1(x0,y0,z0);
y1 = f2(x0,y0,z0); y1 = f2(x1,y0,z0);
z1 = f3(x0,y0,z0); z1 = f3(x1,y1,z0);
printf("%d\t%f\t%f\t%f\n",i, x1,y1,z1); printf("%d\t%f\t%f\t%f\n",i, x1,y1,z1);

/* Error */ /* Error */
e1 = fabs(x0-x1); e1 = fabs(x0-x1);
e2 = fabs(y0-y1); e2 = fabs(y0-y1);
e3 = fabs(z0-z1); e3 = fabs(z0-z1);
i++; i++;

/* Set value for next iteration */ /* Set value for next iteration */
x0 = x1; x0 = x1;
y0 = y1; y0 = y1;
z0 = z1; z0 = z1;
}while(e1>e && e2>e && e3>e); }while(e1>e && e2>e && e3>e);

printf("\nSolution: x=%f, y=%f and z = printf("\nSolution: x=%f, y=%f and z =


%f\n",x1,y1,z1); %f\n",x1,y1,z1);
getch(); getch();
return 0; return 0;
} }

Prepared By: Jayanta Poudel


9 Numerical Methods BSc.CSIT

For more notes visit:

https://collegenote.pythonanywhere.com/

Prepared By: Jayanta Poudel

You might also like