Professional Documents
Culture Documents
Numerical Analysis
Numerical Analysis
*/
NAME- sami raza khan
REG.NO.-Gme/105504
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (x*x*x-x-10);
}
float bisect(float x0,float x1)
{
float x;
x=(x0+x1)/2;
return (x);
//printf("%f\n",x);
}
void main()
{
int it=0,max_it=20;
double x0,x1,x2,x3,err=.0001;
clrscr();
x0=0;
x1=1;
while(f(x0)*f(x1)>0)
{
x0=x0+1;
x1=x1+1;
}
x2=bisect(x0,x1);
it++;
while(it<max_it)
{
if(f(x2)<0)
x0=x2;
else
x1=x2;
x3=bisect(x0,x1);
if(fabs(x3-x2)>err)
{
printf("In %d iteration root is =%f \n",it,x2);
x2=x3;
it++;
}
else
{
printf("In %d iteration root is =%f \n",it,x3);
printf("After %d iteration root is =%f\n",it,x3);
it=max_it+1;
}
}
getch();}
/******OUTPUT******/
/*
In 1
iteration root is =1.500000
In 2
iteration root is =1.750000
In 3
iteration root is =1.875000
In 4
iteration root is =1.812500
In 5
iteration root is =1.843750
In 6
iteration root is =1.859375
In 7
iteration root is =1.851562
In 8
iteration root is =1.855469
In 9
iteration root is =1.857422
In 10 iteration root is =1.856445
In 11 iteration root is =1.855957
In 12 iteration root is =1.855713
In 13 iteration root is =1.855530
After 13 iteration root is =1.855530
*/
/* AIM:- To find the root of the equation (x*x*x-2*x-5) using by secant method */
NAME- sami raza khan
REG.NO.-Gme/105504
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(x*x*x-2*x-5);
}
float secant(float x0,float x1)
{
float x;
x=((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0));
return (x);
//printf("%f\n",x);
}
void main()
{
int it=0,max_it=20;
float x0,x1,x2,x3,err=.0001,check;
clrscr();
x0=0;
x1=1;
while(f(x0)*f(x1)>0)
{
x0=x0+1;
x1=x1+1;
}
x2=secant(x0,x1);
it++;
while(it<max_it)
{
if(f(x2)<0)
x0=x2;
else
x1=x2;
x3=secant(x0,x1);
check=x2-x3;
if(fabs(check)>err)
{
printf("In %d iteration root is =%f \n",it,x2);
x2=x3;
it++;
}
else
{
printf("After %d iteration root is =%f\n",it,x3);
it=max_it+1;
}
}
getch();
}
/*********OUTPUT**********/
/*
In 1
In 2
In 3
In 4
In 5
In 6
After
*/
getch();
return(0);
}
x=x1;
}while(itr<50);
printf("\nThe Process is too Large (Try Some Other Method)");
getch();
return(1);
}
/*********OUTPUT**********/
/*
Enter The Max. Allowed Error = .00001
If you want to find +ve root press (1) else press (0) = 1
At 1 Iteration root is = 0.51657
At 2 Iteration root is = 0.51768
At 3 Iteration root is = 0.51775
At 4 Iteration root is = 0.51776
After 4 Iteraion Root is = 0.5178
}
/*********OUTPUT**********/
/*
NEWTON-RAPHSON METHOD
Root is between 0 and 1
Approxmn No.
x
f(x)
1
0.5000
-0.3776
2
0.6085
0.0051
3
0.6071
0.0000
4
0.6071
0.0000
Root of equation 3x -cos x -1 is 0.607
*/