Escolar Documentos
Profissional Documentos
Cultura Documentos
/*Bisection Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-2.0*x-5.0
void main( )
{
float x,x0,x1,x2,y0,y1,y2,e;
clrscr( );
printf("Input two initial starting roots\n");
scanf("%f%f",&x0,&x1);
printf("Input the tolerance value\n");
scanf("%f",&e);
y0=f(x0);
y1=f(x1);
if(y0*y1>0.0)
{
printf("\n Initial values are unsuitable\n");
}
else
{
while(((x1-x0)/x1)>e)
{
x2=(x0+x1)/2;
y2=f(x2);
if(y0*y2>0)
x0=x2;
else
1
x1=x2;
}
printf("Solution converges to a root\n");
printf("\nRoot of the given equation is = %8.4f",x2);
}
getch( );
}
Output
2
Experiment 2
/*Euler's Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x,y) (y-x)/(y+x)
void main( )
{
float x,y,xn,h;
int i=0;
clrscr( );
printf("Enter initial values of x and y :");
scanf("%f %f",&x,&y);
printf("\n\nEnter the value of x at which y is required :");
scanf("%f",&xn);
printf("\n\nEnter step size h = ");
scanf("%f",&h);
printf("\n\nStep x y\n\n");
while(x<xn)
{
y=y+h*f(x,y);
x=x+h;
i=i+1;
printf("%3d %10.4f %10.4f\n",i,x,y);
}
printf("\nValue of y at x=%10.4f is %10.4f",x,y);
getch( );
}
3
Output
4
Experiment 3
/*Gauss-Elimination Method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int i,j,k,n;
float a[10][10],x[10],sum,temp;
clrscr( );
printf("Enter Number of system of equations\n\n");
scanf("%d",&n);
printf("Number of Equations are=%d\n",n);
printf("Enter coefficients of linear equations/coefficient of
argument matrix\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
scanf("%f",&a[i][j]);
}
for(i=1;i<=n-1;i++)
{
temp=0.0;
for(j=i+1;j<=n;j++)
5
{
temp=a[j][i]/a[i][i];
for (k=i;k<=n+1;k++)
a[j][k]=a[j][k]-(temp*a[i][k]);
}
}
x[n]=a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
sum=0.0;
for(j=i+1;j<=n;j++)
{
sum=sum+(a[i][j]*x[j]);
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
printf("\nSolution of the equation is:\n\n");
for(i=1;i<=n;i++)
printf("x[%d]=%8.3f\n",i,x[i]);
getch( );
}
6
Output
7
Experiment 4
8
Output
9
Experiment 5
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x+x-1.0
void main( )
{
float x0,x1,x2,y0,y1,y2;
int max,i;
clrscr( );
printf("Input two initial roots, maximum iterations \n");
scanf("%f%f%d",&x0,&x1,&max);
y0=f(x0);
y1=f(x1);
if(y0*y1>0.0)
{
printf("Initial roots are unsuitable");
}
else
{
for(i=1;i<=max;i++)
{
x2=(x0*y1-x1*y0)/(y1-y0);
y2=f(x2);
if(y2*y0>0.0)
{
x0=x2;
y0=y2;
10
}
else
{
x1=x2;
y1=y2;
}
}
printf("\Solution converges to a root\n");
printf("Root of given equation is = %8.3f\n",x2);
}
getch( );
}
Output
11
Experiment 6
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
float f(float x,float y);
float x0,y0,h,xn,k1,k2,k3,k4,k,x,y;
int i,n;
clrscr( );
printf("Enter the initial value of x and y:");
scanf("%f %f",& x0,& y0);
printf("\nEnter x at which y is required:");
scanf("%f",&xn);
printf("\n Enter step size:");
scanf("%f",&h);
n=(xn-x0)/h+0.5;
x=x0;
y=y0;
printf("\n\n Step x y\n\n");
for(i=1;i<=n;i++)
{
k1=h*f(x,y);
k2=h*f(x+(h/2.0),y+(k1/2.0));
k3=h*f(x+(h/2.0),y+(k2/2.0));
k4=h*f(x+h,y+k3);
k=(k1+(2.0*k2)+(2.0*k3)+k4)/6.0;
12
x=x+h;
y=y+k;
printf("%3d %10.3f %10.3f\n\n",i,x,y);
}
printf("\n Value of y at x = %f is %f\n\n",x,y);
getch( );
}
Output
Enter the initial value of x and y: 1 1
Enter x at which y is required: 1.5
Enter step size: 0.1
Step x y
1 1.100 1.010
2 1.200 1.042
3 1.300 1.099
4 1.400 1.190
5 1.500 1.333
Value of y at x = 1.500000 is 1.333334
13
Experiment 7
6
Using C-program, find the value of dx 2 by Simpson’s one-third
1 x
0
rule.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
float f(float x);
float x0,xn,h,sum=0.0,result;
int i,n;
clrscr( );
printf("Enter lower limit of the integral:");
scanf("%f",&x0);
printf("Enter upper limit of the integral:");
scanf("%f",&xn);
printf("\n Enter the number of segments:");
scanf("%d",&n);
if(n%2!=0)
printf("\n Number of segment not even number \n");
else
{
h=(xn-x0)/n;
sum=f(x0)+f(xn);
for(i=1;i<n;i++)
{
if(i%2==0)
sum=sum+2.0*f(x0+i*h);
else
sum=sum+4.0*f(x0+i*h);
14
}
result=(h/3.0)*sum;
printf("\n\n Value of integral is = %f\n\n",result);
}
getch( );
}
float f(float x)
{
return(1.0/(1.0+x*x));
}
Output
15
Experiment 8
6
Using C-program and evaluate dx 2 by Trapezoidal rule.
1 x
0
/*Ttapezoidal Rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
float f(float x);
float x0,xn,h,sum,result;
int i,n;
clrscr( );
printf("Enter the lower and upper limits of integral:");
scanf("%f %f",&x0,&xn);
printf("\n Enter the segment width:");
scanf("%f",&h);
n=(xn-x0)/h+0.5;
sum=(f(x0)+f(xn))/2.0;
for(i=1;i<n;i++)
{
sum=sum+f(x0+i*h);
}
result=sum*h;
printf("\n\n Value of integral using trapezoidal rule is = %f\n\n",result);
getch( );
}
float f(float x)
{
return(1.0/(1.0+x*x));
}
16
Output
17