0% found this document useful (0 votes)
7 views

Antc Lab Report for Others

The document contains multiple experiments demonstrating numerical methods for solving mathematical problems, including non-linear equations using Bisection and Newton-Raphson methods, solving systems of equations with Gauss elimination and Gauss-Seidel methods, and numerical solutions of ordinary differential equations using Euler's and Runge-Kutta methods. Additionally, it covers numerical integration techniques using the Trapezoidal and Simpson rules. Each experiment includes code snippets, user input prompts, and outputs illustrating the results of the computations.

Uploaded by

ARNAV SINGH
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)
7 views

Antc Lab Report for Others

The document contains multiple experiments demonstrating numerical methods for solving mathematical problems, including non-linear equations using Bisection and Newton-Raphson methods, solving systems of equations with Gauss elimination and Gauss-Seidel methods, and numerical solutions of ordinary differential equations using Euler's and Runge-Kutta methods. Additionally, it covers numerical integration techniques using the Trapezoidal and Simpson rules. Each experiment includes code snippets, user input prompts, and outputs illustrating the results of the computations.

Uploaded by

ARNAV SINGH
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/ 13

Experiment No- 1

/* Solution Of Non-Linear equation in single variable using Bisection


Method */

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (x*x*x-4*x-9);
}
void bisect(float *x,float a,float b,int *itr)
{
*x=(a + b)/2;
++(*itr);
printf("iteration no. %3d X = %7.5f\n",*itr,*x);
}
void main()
{
clrscr();
int itr=0,maxitr;
float x,a,b,aerr,x1;
printf("enter the values of a,b,allowed error,maximum iterations\
n");
scanf("%f %f %f %d",&a,&b,&aerr,&maxitr);
bisect(&x,a,b,&itr);
do
{
if (f(a)*f(x) < 0 )
b=x;
else
a=x;
bisect (&x1,a,b,&itr);
if (fabs(x1-x) < aerr)
{
printf("after %d iterations, root = %6.4f\n",itr,x1);
//return 0;
break;
}
x=x1;
} while (itr < maxitr);
if (itr==maxitr)
{
printf("solution does not converge,need more iterations");
}
//return 1;
getch();
}

0904047
Output:-
enter the values of a,b,allowed error,maximum iterations
4 3 .0001 20
iteration no. 1 X = 3.50000
iteration no. 2 X = 3.25000
iteration no. 3 X = 3.12500
iteration no. 4 X = 3.06250
iteration no. 5 X = 3.03125
iteration no. 6 X = 3.01562
iteration no. 7 X = 3.00781
iteration no. 8 X = 3.00391
iteration no. 9 X = 3.00195
iteration no. 10 X = 3.00098
iteration no. 11 X = 3.00049
iteration no. 12 X = 3.00024
iteration no. 13 X = 3.00012
iteration no. 14 X = 3.00006
after 14 iterations, root = 3.0001

0904047
Experiment No- 2
/* Solution of Non-Linear equation in single variable using Newton Raphson
Method */

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

float f(float x)
{
return (x*log10(x)-1.2);
}
float df(float x)
{
return (log10(x)+0.43429);
}
void main()
{
clrscr();
int itr,maxitr;
float h,x0,x1,aerr;
printf("enter x0,allowed error,maximum iterations\n");
scanf("%f %f %d",&x0,&aerr,&maxitr);
for (itr=1;itr<=maxitr;itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf("iteration no. %3d,x=%9.6f\n",itr,x1);
if (fabs(h) < aerr)
{
printf("after %3d iterations ,root = %8.6f\n",itr,x1);
//return 0;
break;
}
x0=x1;
}
if (itr==maxitr)
{
printf("solution doesn't converge,need more iterations");
}
//return 1;
getch();
}

0904047
Output:-
enter x0,allowed error,maximum iterations
5 .000001 30
iteration no. 1,x= 2.975001
iteration no. 2,x= 2.745182
iteration no. 3,x= 2.740648
iteration no. 4,x= 2.740646
iteration no. 5,x= 2.740646
after 5 iterations ,root = 2.740646

0904047
Experiment No- 3
/* Solution of a system of simultaneous algebric equation using the Gauss
Elimination Procedure */

#include<stdio.h>
#include<conio.h>
#define N 4
void main()
{
clrscr();
float a[N] [N+1],x[N],t,s;
int i,j,k;
printf("enter the eliminates of the augmented matrix rowwise \n");
for (i=0;i<N;i++)
for (j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
for (j=0;j<N-1;j++)
for (i=j+1;i<N;i++)
{
t=a[i][j]/a[j][j];
for (k=0;k<N+1;k++)
a[i][k]-=a[j][k]*t;
}
/* Now printing the upper triangular matrix */
printf("the upper triangular matrix is:-\n");
for (i=0;i<N;i++)
{
for (j=0;j<N+1;j++)
printf("%8.4f",a[i][j]);
printf("\n");
}
/* Now performing back subsititution */
for (i=N-1;i>=0;i--)
{
s=0;
for (j=i+1;j<N;j++)
s+=a[i][j]*x[j];
x[i]=(a[i][N]-s)/a[i][i];
}
/* Now printing the results */
printf("the solution is:-\n");
for (i=0;i<N;i++)
printf("x[%3d] = %7.4f\n",i+1,x[i]);
getch();
}

0904047
Output:-
enter the eliminates of the augmented matrix rowwise
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7
the upper triangular matrix is:-
10.0000 -7.0000 3.0000 5.0000 6.0000
0.0000 3.8000 0.8000 -1.0000 8.6000
-0.0000 -0.0000 2.4474 10.3158 -6.8158
0.0000 -0.0000 -0.0000 9.9247 9.9247
the solution is:-
x[ 1] = 5.0000
x[ 2] = 4.0000
x[ 3] = -7.0000
x[ 4] = 1.0000

0904047
Experiment No- 4
/* Solution of a system of simultaneous algebric equations using the
Guass-seidal iterative method */

#include<stdio.h>
#include<conio.h>
#include<math.h>
# define N 4
main()
{
float a[N][N+1],x[N],aerr,maxerr,t,s,err;
int i,j,itr,maxitr;
clrscr();
/*first initializing the array x*/
for(i=0;i<N;i++)
x[i]=0;
printf("enter the elements of the augmented matrix rowwise\n");
for(i=0;i<N;i++)
for (j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
printf("enter the allowed error,maximum iterations\n");
scanf("%f %d",&aerr,&maxitr);
printf("iteration x[1] x[2] x[3] x[4]\n");
for(itr=1;itr<=maxitr;itr++)
{
maxerr=0;
for(i=0;i<N;i++)
{
s=0;
for(j=0;j<N;j++)
if(j!=i) s+=a[i][j]*x[j];
t=(a[i][N]-s)/a[i][i];
err=fabs(x[i]-t);
if(err>maxerr) maxerr=err;
x[i]=t;
}
printf("%5d",itr);
for(i=0;i<N;i++)
printf("%9.4f",x[i]);
printf("\n");
if(maxerr<aerr)
{
printf("converges in %3d iterations\n",itr);
for(i=0;i<N;i++)
printf("x[%3d]=%7.4f\n",i+1,x[i]);
getch();
return 0;
}
}
printf("solution does not converge,iterations not sufficient\n");
getch();
return 1;
}

0904047
Output:-
enter the elements of the augmented matrix rowwise
10 -2 -1 -1 3
-2 10 -1 -1 15
-1 -1 10 -2 27
-1 -1 -2 10 -9
enter the allowed error,maximum iterations
.0001 15
iteration x[1] x[2] x[3] x[4]
1 0.3000 1.5600 2.8860 -0.1368
2 0.8869 1.9523 2.9566 -0.0248
3 0.9836 1.9899 2.9924 -0.0042
4 0.9968 1.9982 2.9987 -0.0008
5 0.9994 1.9997 2.9998 -0.0001
6 0.9999 1.9999 3.0000 -0.0000
7 1.0000 2.0000 3.0000 -0.0000
converges in 7 iterations
x[ 1]= 1.0000
x[ 2]= 2.0000
x[ 3]= 3.0000
x[ 4]=-0.0000

0904047
Experiment No- 5
/* Numerical solution of an ordinary differential equation using the
Euler’s method */

#include<stdio.h>
#include<conio.h>
float df(float x,float y)
{
return x+y;
}
void main()
{
clrscr();
float x0,y0,h,x,x1,y1;
printf("enter the values of x0,y0,h,x \n");
scanf("%f%f%f%f",&x0,&y0,&h,&x);
x1=x0;
y1=y0;
while(1)
{
if (x1>x)
{
//return;
break;
}
y1+=h*df(x1,y1);
x1+=h;
printf("when x=%3.1f"
"y=%4.2f \n",x1,y1);
}
getch();
}

0904047
Output:-
enter the values of x0,y0,h,x
0 2 .2 2
when x=0.2y=2.40
when x=0.4y=2.92
when x=0.6y=3.58
when x=0.8y=4.42
when x=1.0y=5.46
when x=1.2y=6.76
when x=1.4y=8.35
when x=1.6y=10.30
when x=1.8y=12.68
when x=2.0y=15.58

0904047
Experiment No- 6
/* Numerical solution of an ordinary differential equation using the
RUNGE-KUTTA METHOD*/

#include<stdio.h>
#include<conio.h>
float f (float x,float y)
{
return x+y*y;
}
main()
{
clrscr();
float x0,y0,h,xn,x,y,k1,k2,k3,k4,k;
printf("enter the values of x0,y0,h,xn\n");
scanf("%f %f %f %f",&x0,&y0,&h,&xn);
x=x0;
y=y0;
while (1)
{
if (x==xn)
break;
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);
k=(k1+(k2+k3)*2+k4)/6;
x+=h;
y+=k;
printf("when x=%8.4f y=%8.4f\n",x,y);
}
getch();
}

Output:-
enter the values of x0,y0,h,xn
0.0 1.0 0.1 0.2
when x= 0.1000 y= 1.1165
when x= 0.2000 y= 1.2736

0904047
Experiment No- 7
/* Numerical solution of an integral equation using the Trapezoidal Rule */

#include<stdio.h>
#include<conio.h>
float y(float x)
{
return 1/(1+x*x);
}
void main()
{
clrscr();
float x0,xn,h,s;
int i,n;
puts("enter x0,xn,no. of subintervals");
scanf("%f %f %d ",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn);
for(i=1;i<=n-1;i++)
s+=2*y(x0+i*h);
printf ("value of integral is %6.4f\n",(h/2)*s);
getch();
}

Output:-
enter x0,xn,no. of subintervals
0 3 5
value of integral is 1.2473

0904047
Experiment No- 8
/* Numerical solution of an integral equation using the Simpson Rule */

#include<stdio.h>
#include<conio.h>
float y(float x)
{
return 1/(1+x*x);
}
void main()
{
clrscr();
float x0,xn,h,s;
int i,n;
puts("enter x0,xn,no. of subintervals");
scanf("%f %f %d ",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn)+4*y(x0+h);
for(i=3;i<=n-1;i+=2)
s+=4*y(x0+i*h)+2*y(x0+(i-1)*h);
printf ("value of integral is %6.4f\n",(h/3)*s);
getch();
}

Output:-
enter x0,xn,no. of subintervals
0 3 5
value of integral is 1.1608

0904047

You might also like