Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

/* AIM:- To find a root of equation (x*x*x-x-10)=0; using the bisection 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-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
*/

iteration root is =2.058824


iteration root is =2.081264
iteration root is =2.089639
iteration root is =2.092740
iteration root is =2.093884
iteration root is =2.094306
7 iteration root is =2.094518

/*REGULA FALSI METHOD */


NAME- sami raza khan
REG.NO.-Gme/105504
#include <stdio.h>
#include <conio.h>
#include <math.h>
float f(float x)
{
return(x*exp(x)-cos(x));
}
void app(float *x,float a, float b, int *itr)
{
float f(float);
*x=a-((a-b)/(f(a)-f(b))*f(a));
(*itr)++;
printf("\nAt %d Iteration root is = %8.5f",*itr,*x);
}
main()
{
int itr=0,c;
float f(float),x,a,b,aerr,x1=0,d=0;
void app(float *, float,float,int *);
clrscr();
printf("\nEnter The Max. Allowed Error = ");
scanf("%f",&aerr);
printf("\nIf you want to find +ve root press (1) else press (0) = ");
scanf("%d",&c);
if(c==1)
{
while(f(d)*f(d+0.1)>0)
d=d+0.1;
a=d;
b=d+0.1;
}
else if(c==0)
{
while(f(d)*f(d-0.1)>0)
d=d-0.1;
a=d;
b=d-0.1;
}
app(&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
b=x;
else
a=x;
app(&x1,a,b,&itr);
if(fabs(x1-x)<aerr)
{
printf("\n\nAfter %d Iteraion Root is = %7.4f",itr,x);

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

NAME- sami raza khan


REG.NO.-Gme/105504
/* NEWTON-RAPHSON METHOD */
//PROBLEM: 3x -cos x -1
# define f(x) (-((cos(x))-(3*x)+1))
# define f1(x) (3+(sin(x)))
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <math.h>
void main()
{
int app=1;
float x=0,first,second,current,next;
clrscr();
printf("\t NEWTON-RAPHSON METHOD\n");
while(1)
{
if (f(x)==0)
{
printf ("Root is %f",x);
exit(1);
}
else if (f(x)<0)
first=x;
else
{
second=x;
break;
}
x++;
}
printf ("\nRoot is between %.0f and %.0f\n",first,second);
current=(first+second)/2;
printf("\nApproxmn No.
x
f(x)\n");
while(1)
{
next=(current-(f(current)/f1(current)));
printf(" %d\t\t%.4f\t\t%.4f\n",app,current,f(current));
app++;
if(fabs(next-current)< 0.0001)
break;
current=next;
}
printf(" %d\t\t%.4f\t\t%.4f",app,next,f(next));
printf("\n\nRoot of equation 3x -cos x -1 is %.3f",next);
getch();

}
/*********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
*/

You might also like