Numerical Lab Full
Numerical Lab Full
Assignment No: 01
Write a C program to find the value of integration of a
function using Trapezoidal Rule.
Program:
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(x*x*x);
}
void main()
{
float x0, xn, h, sum=0.0, sum1=0.0, res;
int n, i;
clrscr();
printf(“\nEnter the lower limit and upper limit:\n”);
scanf(“%f%f”, &x0, &xn);
printf(“\nEnter the no. of intervals:\n”);
scanf(“%d”, &n);
h=(xn-x0)/n;
sum+=f(x0)+f(xn);
for(i=1;i<n;i++)
sum1+=2.0*f(x0+i*h);
res=(h/3.0)*(sum+sum1);
printf(“\nThe value of the integration is=%f”, res);
getch();
}
Page 2
Output:
Page 3
Assignment No: 02
Write a C program to find the value of integration of a
function using Simpson’s 1/3 Rule.
Program:
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(x*x*x);
}
void main()
{
float x0, xn, h, sum=0.0, sum1=0.0, res;
int n, i;
clrscr();
printf(“\nEnter the lower limit and upper limit:\n”);
scanf(“%f%f”, &x0, &xn);
printf(“\nEnter the no. of intervals:\n”);
scanf(“%d”, &n);
h=(xn-x0)/n;
sum+=f(x0)+f(xn);
for(i=1;i<n;i++)
{
if(i%2!=0)
sum1+=4.0*f(x0+i*h);
else
sum1+=2.0*f(x0+i*h);
}
res=(h/3.0)*(sum+sum1);
printf(“\nThe value of the integration is=%f”, res);
getch();
}
Page 4
Output:
Page 5
Assignment No: 03
Write a C program to find the root of an equation using
Regula Falsi Method.
Program:
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(x*x*x-4*x-9.0);
}
void main()
{
float a,b,x;
clrscr();
y:printf(“\nEnter two given interval values:\n”);
scanf(“%f%f”, &a, &b);
if(f(a)==0)
{
printf(“\n the root=%f”,a);
goto x;
}
if(f(b)==0)
{
printf(“\n the root=%f”,b);
Page 6
goto x;
}
if(f(a)*f(b)>0)
{
printf(“\nWrong interval select,Enter again:\n”);
goto y;
}
do
{
x=(a*f(a)-b*f(b))/(f(a)-f(b));
if(f(x)==0)
{
printf(“\n the root=%f”,x);
goto x;
}
else
{
if(f(a)*f(x)>0)
b=x;
else;
a=x;
}
Page 7
}while((b-a)/b>0.000001);
printf(“Root=%f”,x);
x:getch();
}
Output:
Page 8
Assignment No: 04
Write a C program to find the root of an equation using
Iteration Method.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(pow((x+1.0),(1.0/3.0)));
}
void main()
{
float x,xnew,EPS;
clrscr();
printf("\nEnter the value for approx root & EPS:\n");
scanf("%f%f", &x, &EPS);
xnew=f(x);
while(fabs(x-xnew)>EPS)
{
x=xnew;
xnew=f(x);
}
printf("\n Root=%f",xnew);
getch();
}
Page 9
Output:
Page 10
Assignment No: 05
Write a C program to find the solution of differential
equation of the form y’=f(x,y), y(x0)=y0 by Euler’s
method.
Program:
#include<stdio.h>
#include<conio.h>
float f(float x, float y)
{
return(x*x+y);
}
void main()
{
float x0=0, y0=1,xlast,h;
clrscr();
printf("\nEnter the value h and last value of x::");
scanf("%f%f", &h,&xlast);
printf("\n\tx-value\ty-value\n");
while(x0<xlast)
{
y0=y0+h*f(x0,y0);
x0=x0+h;
printf("\t%.2f\t%.6f\n",x0,y0);
}
getch();
}
Page 11
Output:
Page 12
Assignment No: 06
Write a C program to find the value of integration of a
function using Simpson’s 3/8 Rule.
Program:
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(x*x*x);
}
void main()
{
float x0, xn, h, sum=0.0, sum1=0.0, res;
int n, i;
clrscr();
printf(“\nEnter the lower limit and upper limit:\n”);
scanf(“%f%f”, &x0, &xn);
printf(“\nEnter the no. of intervals:\n”);
scanf(“%d”, &n);
h=(xn-x0)/n;
sum+=f(x0)+f(xn);
for(i=1;i<n;i++)
{
if(i%3!=0)
sum1+=3.0*f(x0+i*h);
else
sum1+=2.0*f(x0+i*h);
}
res=(3.0*h/8.0)*(sum+sum1);
printf(“\nThe value of the integration is=%f”, res);
getch();
}
Page 13
Output:
Page 14
Assignment No: 07
Write a C program to find the root of an equation using
Bisection Method.
Program:
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(x*x*x+x-5.0);
}
void main()
{
float a,b,m;
clrscr();
y:printf("\nEnter two given interval values:\n");
scanf("%f%f",&a,&b);
if(f(a)==0)
{
printf("\n the root=%f",a);
goto x;
}
if(f(b)==0)
{
printf("\n the root=%f",b);
goto x;
}
if(f(a)*f(b)>0)
{
printf("\nWrong interval select,Enter again:\n");
goto y;
}
do
{
m=(a+b)/2.0;
Page 15
if(f(m)==0)
{
printf("\n the root=%f",m);
goto x;
}
else
{
if(f(a)*f(m)>0)
b=m;
else;
a=m;
}
}while((b-a)/b>0.000001);
printf("Root=%f",m);
x:getch();
}
Output:
………………………………………
Teacher’s Signature