Vous êtes sur la page 1sur 14

ITM UNIVERSE VADODARA

COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Practical 1

Aim
Implement Bisection menthod in c programming.

Algorithm
Start
1. Read x1, x2, e
*Here x1 and x2 are initial value
e is the absolute error i.e. the desired degree of accuracy
2. Compute: f1 = f(x1) and f2 = f(x2)
3. If (f1*f2) > 0, then display initial guesses are wrong and goto (11).
Otherwise continue.
4. x = (x1 + x2)/2
5. If ( [ (x1 – x2)/x ] < e ), then display x and goto (11).
* Here [ ] refers to the modulus sign. *
6. Else, f = f(x)
7. If ((f*f1) > 0), then x1 = x and f1 = f.
8. Else, x2 = x and f2 = f.
9. Goto (5).
*Now the loop continues with new values.*
10. Stop

170950107070
Batch:2 Page 1
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Program Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
float f(float x)
{ return (x*x*x-4*x-9);
}
void bisection(float *x, float a, float b, int *itr)
{ *x=(a+b)/2;
++(*itr);
printf("\nAt iteration %d, x=%f",*itr,*x);
}
int main()
{ int itr=0,mitr;
float a,b,x1,x2,allerr;
printf("\nEnter the value of lower limit and upper limit: ");
scanf("%f %f",&a,&b);
if(f(a)*f(b)<=0)
{
printf("\nEnter the accuracy needed: ");
scanf("%f",&allerr);
printf("\nEnter max iterations allowed: ");
scanf("%d",&mitr);
bisection(&x1,a,b,&itr);
do
{ if(f(a)*f(x1)<0)
b=x1;
else
a=x1;
bisection(&x2,a,b,&itr);
if(fabs(x2-x1)<allerr)
{
printf("\nAfter %d iterations, root is: %f\n",itr,x2);
return 0;

170950107070
Batch:2 Page 2
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

}
x1=x2;
}while(itr<mitr);
printf("\nSolution does not converge!!!\nInsufficient iterations!!!\n");
return 1;
}
else
{ printf("\nInappropriate interval!!!\n");
return 1;
}
}

Output(1):

Output(2):

170950107070
Batch:2 Page 3
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Output(3):

170950107070
Batch:2 Page 4
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Practical 2

Aim
Implement Regula-Falsi menthod. in c program

Algorithm
Start
1. Read values of x0, x1 and e
*Here x0 and x1 are the two initial guesses
e is the degree of accuracy or the absolute error i.e. the stopping criteria*
2. Computer function values f(x0) and f(x1)
3. Check whether the product of f(x0) and f(x1) is negative or not.
If it is positive take another initial guesses.
If it is negative then goto step 5.
4. Determine:
x = [x0*f(x1) – x1*f(x0)] / (f(x1) – f(x0))
5. Check whether the product of f(x1) and f(x) is negative or not.
If it is negative, then assign x0 = x;
If it is positive, assign x1 = x;
6. Check whether the value of f(x) is greater than 0.00001 or not.
If yes, goto step 5.
If no, goto step 8.

170950107070
Batch:2 Page 5
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

*Here the value 0.00001 is the desired degree of accuracy, and hence the stopping
criteria.*
7. Display the root as x.
8. Stop

Program Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
float f(float x)
{
return cos(x)-x*exp(x);
}
void regula (float *x, float x0, float x1,float fx0, float fx1, int *itr)
{ *x=x0-((x1-x0)/(fx1-fx0))*fx0;
++(*itr);
printf("\nAt iteration %d, x=%f",*itr,*x);
}
int main()
{
int itr=0,maxitr;
float x0,x1,x2,x3,allerr;
printf("\nEnter the value of lower limit and upper limit: ");
scanf("%f %f",&x0,&x1);
if(f(x0)*f(x1)<=0)
{
printf("\nEnter the accuracy needed: ");
scanf("%f",&allerr);
printf("\nEnter max iterations allowed: ");
170950107070
Batch:2 Page 6
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

scanf("%d",&maxitr);
regula (&x2,x0,x1,f(x0),f(x1),&itr);
do
{ if(f(x0)*f(x2)<0)
x1=x2;
else
x0=x2;
regula (&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2)<allerr)
{
printf("\nAfter %d iterations, root is: %f\n",itr,x2);
return 0;
}

x2=x3;
}while(itr<maxitr);
printf("\nSolution does not converge!!!\nInsufficient iterations!!!\n");
return 1;
}
else
{ printf("\nInappropriate interval!!!\n");
return 1;
}
}

Output(1):

170950107070
Batch:2 Page 7
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Output(2):

Ouput(3):

170950107070
Batch:2 Page 8
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

170950107070
Batch:2 Page 9
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Practical 3

Aim
Implement secant method in c program

Algorithm

Start
1. Get values of x0, x1 and e
*Here x0 and x1 are the two initial guesses
e is the stopping criteria, absolute error or the desired degree of accuracy*
2. Compute f(x0) and f(x1)
3. Compute x2 = [x0*f(x1) – x1*f(x0)] / [f(x1) – f(x0)]
4. Test for accuracy of x2
If [ (x2 – x1)/x2 ] > e, *Here [ ] is used as modulus sign*
then assign x0 = x1 and x1 = x2
goto step 4
Else,
goto step 6
5. Display the required root as x2.
6. Stop

170950107070
Batch:2 Page 10
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Program code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
float f(float x)
{
return cos(x)-x*exp(x);
}

void secant(float *x, float x0,float x1,float fx0,float fx1, int *itr)
{
*x=x1-((x1-x0)/(fx1-fx0)*fx1);
++(*itr);
printf("\n at iteration %d, x=%f:\n",*itr,*x);
}
int main()
{
int itr=0,maxitr;
float x0,x1,x2,x3,allerr;
printf("\n enter the value of upper limit and lower limit");
scanf("%f%f",&x0,&x1);

printf("enter the accuracy:");


scanf("%f",&allerr);
printf("enter the maxitr");
scanf("%d",&maxitr);

if(f(x1)*f(x0)<=0)
{
secant(&x2,x0,x1,f(x0),f(x1),&itr);

do{

170950107070
Batch:2 Page 11
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

if(f(x0)*f(x2)<0)
x1=x2;
else
x0=x2;
secant(&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2)<allerr)
{
printf("\n after %d iteration,root is %f \n",itr,x3);
return 0;
}
x2=x3;
}
while(itr<maxitr);
printf("\n solution does not converge.Iteration are not sufficient\n");
return 1;

}
else
{
printf("inappropriate interval\n");
}
return 1;

Output(1):

170950107070
Batch:2 Page 12
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Output(2):

Output(3):

170950107070
Batch:2 Page 13
ITM UNIVERSE VADODARA
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

170950107070
Batch:2 Page 14

Vous aimerez peut-être aussi