You are on page 1of 41

1.Write a c program to evaluate value of e correct upto 3 decimal places.

#include<stdio.h>
#include<stdlib.h>
main()
{
int count=1;
float sum1=1,k=1,i=1,sum2=1,term=1;
printf(" Term no. || term || sum || difference\n");

/* format to print the output*/

printf(" %d || %f || %f ||%f ",count,term,sum1,sum2-sum1);


while(1)
{ k=k*i;
term=1/k;
sum1=sum2;
sum2=sum2+term;
if((sum2-sum1)<=0.0005)

/*checking for precision*/

break;
i++;
printf("\n---------------------------------------\n");
count++;
printf(" %d || %f || %f ||%f ",count,term,sum1,sum2-sum1);
}
printf("\nValue of e is:: %0.3f\n",sum1);
}
OUTPUT

Term no. || term ||


1

sum

|| difference

|| 1.000000 || 1.000000 ||0.000000

--------------------------------------2

|| 1.000000 || 1.000000 ||1.000000

/*printing output*/

--------------------------------------3

|| 0.500000 || 2.000000 ||0.500000

--------------------------------------4

|| 0.166667 || 2.500000 ||0.166667

--------------------------------------5

|| 0.041667 || 2.666667 ||0.041667

--------------------------------------6

|| 0.008333 || 2.708333 ||0.008333

--------------------------------------7

// 0.001389 || 2.716667 ||0.001389

Value of e is:: 2.718

2..Write a program to implement bisection method for finding root of an non linear
equation.

Equation: 3x cosx = 1

/* equation from which value of x is to be found*/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) (3*x-cos(x)-1)
main()
{ float a,b,m,e,error=0,x,error1;
double n;
int d=1,c=0,i=0;
while(d==1)
{ printf("Enter value of a and b::");
scanf("%f %f",&a,&b);
if(f(a)*f(b)<0)
d=0;

/* getting initial interval as input*/

else if(f(a)==0)
{
printf("Interval limit %f is the result",a);
exit(0);
}
else if(f(b)==0)
{
printf("Interval limit %f is the result",b);
exit(1)

else
printf("No root exists in this interval\n");
}
printf("Enter degree of precision::");

/*takes degree of precision as input*/

scanf("%f",&e);
e=5*pow(0.1,(e+1));
printf(" i\t a\t\t b\t\t m\t abserror\t\t order of convergence \n\n");
while(fabs(a-b)>e)
{ if(i==0)
{ m=(a+b)/2;
printf(" %d\t %f\t %f\t %f\t ------\t\t -----\n",i,a,b,m) ;
}
else { x=m;
m=(a+b)/2;
error1=error;
error=fabs(x-m);
printf(" %d\t %f\t %f\t %f\t %f\t",i,a,b,m,error) ;
if(i==1)
{

/* format to print the output*/

printf("------\n");
}
else
{
n=log(error)/log(error1);

/* calculates order of convergence*/

printf("%lf\n",n);
}
}

if(f(m)==0)
{ c=1;
break;
}
else
{ if(f(a)*f(m)<0)
b= m;
else
a= m;
}
i++;
}
if(c==1)
printf("one of the root is:: %f",m);
else
{ m=(a+b)/2;
printf("one of the root is:: %f",m);
}
}
OUTPUT

/* prints output as result*/

Enter value of a and b::0 1


Enter degree of precision::4
i

abserror

order of convergence

0.000000

1.000000

0.500000

------

-----

0.500000

1.000000

0.750000

0.250000

------

0.500000

0.750000

0.625000

0.125000

1.500000

0.500000

0.625000

0.562500

0.062500

1.333333

0.562500

0.625000

0.593750

0.031250

1.250000

0.593750

0.625000

0.609375

0.015625

1.200000

0.593750

0.609375

0.601563

0.007813

1.166667

0.601563

0.609375

0.605469

0.003906

1.142857

0.605469

0.609375

0.607422

0.001953

1.125000

0.605469

0.607422

0.606445

0.000977

1.111111

10

0.606445

0.607422

0.606934

0.000488

1.100000

11

0.606934

0.607422

0.607178

0.000244

1.090909

12

0.606934

0.607178

0.607056

0.000122

1.083333

13

0.607056

0.607178

0.607117

0.000061

1.076923

14

0.607056

0.607117

0.607086

0.000031

1.071429

one of the root is:: 0.607101

3. Write a program to implement Regular falsi method for finding root of an non linear equation.

Equation: 3x cosx = 1

/* equation from which value of x is to be found*/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) (3*x-cos(x)-1)
main()
{ float a,b,m1=0,m2=0,error1,error=0,e;

/*macro defining the function*/

double n;
int d=1,i=0;
while(d==1)
{ printf("Enter value of a and b::");
scanf("%f %f",&a,&b);
if(f(a)*f(b)<0)
d=0;
else if(f(a)==0)
{
printf("Interval limit %f is the result",a);
exit(0);
}
else if(f(b)==0)
{
printf("Interval limit %f is the result",b);
exit(1);
}
else if(a<b)
{ while(a<b)
{a++;
if(f(a)*f(b)<0)
break;
}
break;
}

else
printf("No root exists in this interval\n");
}

/* getting initial interval as input*/

printf("Enter degree of precision::");

/*takes degree of precision as input*/

scanf("%f",&e);
e=5*pow(0.1,(e+1));
printf(" i\t |b-a|\t\t m\t\t abserror\t order of convergence \n\n");
do
{
if(i==0)
{ m2=(a*f(b)-b*f(a))/(f(b)-f(a));
printf(" %d\t %f\t %f\t ------\t\t -----\n",i,fabs(b-a),m2) ;
}
else
{ m1=m2;
m2=(a*f(b)-b*f(a))/(f(b)-f(a));

/* calculation of approximation to root*/

error1=error;
error=fabs(m2-m1);
printf(" %d\t %f\t %f\t %f\t",i,fabs(b-a),m2,error) ;
if(i==1)
{

printf("------\n");

}
else
{

n=log(error)/log(error1);
printf("%lf\n",fabs(n));

}
}
If ( f(m2)==0)
break;

else
{ if ( f(a)*f(m2)<0)

/* calculation of order of convergence*/

b= m2;
else
a= m2;
}
i++;
}while(fabs(m2-m1)>e);
printf("one of the root is:: %f\n",m2);

/*prints output as result*/

}
OUTPUT
Enter value of a and b::0 1
Enter degree of precision::4
i

|b-a|

abserror

order of convergence

1.000000

0.578085

------

----

0.421915

0.605959

0.027873

------

0.394041

0.607057

0.001099

1.903251

0.392943

0.607100

0.000043

1.476279

one of the root is:: 0.607100

4.. Write a program to implement Repeated substitution method for finding root of an non linear
equation.
Equation:: Log10(x)-2*x+7=0

/* equation from which value of x is to be found*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

float f(float x)

/* expressing as x = g(x)*/

{ return((log10(x)+7)/2);
}
float der(float x)

/* finds derivative of g(x)*/

{
return (1/(2*x*log(10)));
}
main()
{ double x1,x2,error=0,error1;
int i=0,e;
double n;
printf("Enter initial approximation to root::");

/* inputs initial root from user*/

scanf("%lf",&x1);
if(x1<=0)
{ printf("root can not be find for this initial approximation.");
exit(0);
}
printf("Enter degree of precision::");
scanf("%d",&e);
e=5*pow(0.1,(e+1));

/*takes degree of precision as input*/

printf(" i\t xi\t\t f(xi)\t\t abserror\t\t order of convergence \n");

if((fabs(der(x1)))>1)

/*checks for condition of convergence*/

{
printf("condition of convergence is not satisfied.");
exit(0);
}
x2=f(x1);
while(fabs(x2-x1)>e)
{ if((fabs(der(x2)))>1)

/*checks for condition of convergence*/

{
printf("condition of convergence is not satisfied.");

exit(0);
}

error1=error;
error=fabs(x2-x1);
printf(" %d\t %lf\t %lf\t %lf\t",i,x1,x2,error);
if(i==0)
{ printf("--------\n");
}
else
{ n=log(error)/log(error1);
printf("%lf\n",fabs(n));
}
x1=x2;
x2=f(x1);
i++;
}
printf(" root is %lf\n",x2);

/*prints output as result*/

}
OUTPUT
Enter initial approximation to root::1
Enter degree of precision::4
i

xi

f(xi)

abserror

order of convergence

1.000000

3.500000

2.500000

--------

3.500000

3.772034

0.272034

1.420759

3.772034

3.788288

0.016254

3.164333

3.788288

3.789222

0.000934

1.693543

3.789222

3.789275

0.000053

1.410122

3.789275

3.789278

0.000003

1.289369

3.789278

3.789278

0.000000

1.202215

root is 3.789278

5. Write a program to implement Newton - Raphson method for finding root of an non linear
equation.
a) Equation: x e^(--x) = 0

/* equation from which value of x is to be found*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
float f(float x)

/* finds value of f(x)*/

{ float k;
k= x*exp(-x);
return k;
}
float der(float x)

/* finds derivative of f (x) */

{
float k;
k=exp(-x)*(1-x);
return k; }
float f2(float x)
{
float k;
k=exp(-x)*(x-2);
return k;
}
main()
{ double x1,x2,error=0,error1,e;
int i=0;

/* finds second order derivative of f(x) */

double n;
printf("Enter initial approximation to root::");

/* inputs initial root from user*/

scanf("%lf",&x1);
printf("Enter degree of precision::");

/*takes degree of precision as input*/

scanf("%lf",&e);
e=5*pow(0.1,(e+1));
printf(" i\t Xi\t\t f(Xi)\t\tf'(Xi)\t\tabserror\t order of convergence \n");
if(der(x1)==0)

/* if der(x1)==0,process is stopped and no root exists*/

{
printf("condition of convergence is not satisfied. \n value of f'(x) is zero at at x=%lf",x1);
exit(0);
}
if(!(fabs((f(x1)*f2(x1))/pow(der(x1),2))<1))

/*checks for condition of convergence*/

{
printf("Condition of convergence is not satisfied. \n \n value of |g'(x)| is greater than 1 at x=%lf",x2);
exit(1);
}

x2=x1-(f(x1)/der(x1));
if(i==0)
{ printf(" %d\t%lf\t %lf\t %lf\t%lf ",i,x1,f(x1),der(x1),error);
}
while(fabs(x2-x1)>e)
{
if(der(x1)==0)
{
printf("condition of convergence is not satisfied. \n value of f'(x) is zero at at x=%lf",x1);
exit(0);
}

if(!(fabs((f(x2)*f2(x2))/pow(der(x2),2))<1))

/*checks for condition of convergence*/

{
printf("Condition of convergence is not satisfied. \n \n value of |g'(x)| is greater than 1 at x=%lf",x2);
exit(1);
}
error1=error;
error=fabs(x2-x1);
if(i==0)
{ printf("--------\n");
}
else
{ printf(" %d\t %lf\t %lf\t %lf\t%lf ",i,x1,f(x1),der(x1),error);
n=log(error)/log(error1);

/* calculation of order of convergence*/

printf("%lf\n",fabs(n));
}
x1=x2;
x2=x1-(f(x1)/der(x1));
i++;
}
printf("Root is %lf\n",x2);
}
OUTPUT
Enter initial approximation to root::10
Enter degree of precision::4
i

Xi

f(Xi)

f'(Xi)

abserror

order of convergence

10.000000

0.000454

-0.000409

0.000000

--------

11.111111

0.000166

-0.000151

1.098901

0.895124

12.210012

0.000061

-0.000056

1.089206

0.906037

13.299218

0.000022

-0.000021

1.081306

0.914809

14.380524

0.000008

-0.000008

1.074736

0.922029

15.455260

0.000003

-0.000003

1.069179

0.928081

16.524439

0.000001

-0.000001

1.064415

0.933232

17.588853

0.000000

-0.000000

1.060281

0.937677

18.649135

0.000000

-0.000000

1.056660

0.941549

19.705795

0.000000

-0.000000

1.053459

10

20.759254

0.000000

-0.000000

1.050609

0.947979

11

21.809863

0.000000

-0.000000

1.048054

0.950680

12

22.857917

0.000000

-0.000000

1.045750

0.953108

13

23.903667

0.000000

-0.000000

1.043661

0.955303

14

24.947329

0.000000

-0.000000

1.041758

0.957299

15

25.989087

0.000000

-0.000000

1.040017

0.959116

16

27.029104

0.000000

-0.000000

1.038419

0.960790

17

28.067523

0.000000

-0.000000

1.036945

0.962323

18

29.104468

0.000000

-0.000000

1.035582

0.963741

19

30.140049

0.000000

-0.000000

1.034317

0.965055

20

31.174366

0.000000

-0.000000

1.033141

0.966275

21

32.207507

0.000000

-0.000000

1.032044

0.967413

22

33.239550

0.000000

-0.000000

1.031018

0.968472

23

34.270568

0.000000

-0.000000

1.030057

0.969464

24

35.300625

0.000000

-0.000000

1.029154

0.970398

25

36.329779

0.000000

-0.000000

1.028305

0.971270

26

37.358084

0.000000

-0.000000

1.027504

0.972101

27

38.385588

0.000000

-0.000000

1.026748

0.972875

28

39.412336

0.000000

-0.000000

1.026033

0.973611

29

40.438369

0.000000

-0.000000

1.025356

0.974308

30

41.463725

0.000000

-0.000000

1.024713

0.974965

31

42.488439

0.000000

-0.000000

1.024103

0.975596

32

43.512542

0.000000

-0.000000

1.023522

0.976186

0.944956

33

44.536065

0.000000

-0.000000

1.022969

0.976756

34

45.559034

0.000000

-0.000000

1.022442

0.977295

35

46.581476

0.000000

-0.000000

1.021939

0.977813

36

47.603415

0.000000

-0.000000

1.021458

0.978299

37

48.624873

0.000000

-0.000000

1.020997

0.978775

38

49.645870

0.000000

-0.000000

1.020557

0.979224

39

50.666427

0.000000

-0.000000

1.020134

0.979656

40

51.686561

0.000000

-0.000000

1.019729

0.980066

41

52.706290

0.000000

-0.000000

1.019340

0.980464

42

53.725630

0.000000

-0.000000

1.018966

0.980849

43

54.744596

0.000000

-0.000000

1.018606

0.981214

44

55.763203

0.000000

-0.000000

1.018261

45

56.781463

0.000000

-0.000000

1.017927

46

57.799390

0.000000

-0.000000

1.017606

47

58.816996

0.000000

-0.000000

1.017296

0.982548

48

59.834292

0.000000

-0.000000

1.016997

0.982853

49

60.851289

0.000000

-0.000000

1.016708

0.983147

50

61.867997

0.000000

-0.000000

1.016429

0.983432

51

62.884426

0.000000

-0.000000

1.016159

0.983701

52

63.900585

0.000000

-0.000000

1.015898

0.983977

53

64.916483

0.000000

-0.000000

1.015645

0.984232

54

65.932129

0.000000

-0.000000

1.015401

0.984474

55

66.947529

0.000000

-0.000000

1.015164

0.984716

56

67.962693

0.000000

-0.000000

1.014934

0.984959

57

68.977627

0.000000

-0.000000

1.014711

0.985173

58

69.992337

0.000000

-0.000000

1.014494

0.985400

59

71.006832

0.000000

-0.000000

1.014284

0.985610

60

72.021116

0.000000

-0.000000

1.014080

0.985817

61

73.035197

0.000000

-0.000000

1.013882

0.986022

0.981572
0.981904
0.982236

62

74.049079

0.000000

-0.000000

1.013689

0.986211

63

75.062768

0.000000

-0.000000

1.013502

0.986406

64

76.076270

0.000000

-0.000000

1.013320

0.986591

65

77.089590

0.000000

-0.000000

1.013142

0.986767

66

78.102732

0.000000

-0.000000

1.012970

0.986948

67

79.115702

0.000000

-0.000000

1.012801

0.987106

68

80.128504

0.000000

-0.000000

1.012638

0.987287

69

81.141141

0.000000

-0.000000

1.012478

0.987441

70

82.153619

0.000000

-0.000000

1.012322

0.987602

71

83.165942

0.000000

-0.000000

1.012171

0.987755

72

84.178112

0.000000

-0.000000

1.012022

0.987899

73

85.190135

0.000000

-0.000000

1.011878

0.988054

74

86.202012

0.000000

-0.000000

1.011737

0.988186

75

87.213749

0.000000

-0.000000

1.011599

0.988333

76

88.225348

0.000000

-0.000000

1.011464

0.988465

77

89.236813

0.000000

-0.000000

1.011333

0.988605

78

90.248146

0.000000

-0.000000

1.011205

0.988733

79

91.259351

0.000000

-0.000000

1.011079

0.988857

80

92.270430

0.000000

-0.000000

1.010956

0.988982

81

93.281386

0.000000

-0.000000

1.010836

0.989088

82

94.292222

0.000000

-0.000000

1.010719

0.989228

83

95.302941

0.000000

-0.000000

1.010603

0.989270

84

96.313544

0.000000

-0.000000

1.010488

0.989231

85

97.324033

0.000000

-0.000000

1.010391

0.990795

86

98.334424

0.000000

-0.000000

1.010244

0.985923

87

99.344669

0.000000

-0.000000

1.010143

0.990171

88

100.354812

0.000000

-0.000000

1.009730

0.959434

Condition of convergence is not satisfied.


value of |g'(x)| is greater than 1 at x=102.374821

b) Equation :: x^3x 3=0

/* equation from which value of x is to be found*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

float f(float x)

/* finds value of f(x)*/

{ float k;
k= pow(x,3)-x-3;
return k;
}

float der(float x)

/* finds derivative of f (x) */

{
float k;
k=(3*x*x-1);
return k;

}
float f2(float x)
{
float k;
k= (6*x);
return k;

}
main()
{ double x1,x2,error=0,error1,e;
int i=0;

/* finds second order derivative of f(x) */

double n;
printf("Enter initial approximation to root::");

/* inputs initial root from user*/

scanf("%lf",&x1);

printf("Enter degree of precision::");

/*takes degree of precision as input*/

scanf("%lf",&e);
e=5*pow(0.1,(e+1));
printf(" i\t Xi\t\t f(Xi)\t\tf'(Xi)\t\tabserror\t order of convergence \n");
if(der(x1)==0)

/* if der(x1)==0,process is stopped and no root exists*/

{
printf("condition of convergence is not satisfied. \n value of f'(x) is zero at x=%lf",x1);
exit(0);
}
if(!(fabs((f(x1)*f2(x1))/pow(der(x1),2))<1))

/*checks for condition of convergence*/

{
printf("Condition of convergence is not satisfied. \n \n value of |g'(x)| is greater than 1 at x=%lf",x2);
exit(1);
}

x2=x1-(f(x1)/der(x1));
if(i==0)
{ printf(" %d\t%lf\t %lf\t %lf\t%lf ",i,x1,f(x1),der(x1),error);
}
while(fabs(x2-x1)>e)
{ if(der(x1)==0)
{
printf("condition of convergence is not satisfied. \n value of f'(x) is zero at x=%lf",x1);
exit(0);
}

if(!(fabs((f(x2)*f2(x2))/pow(der(x2),2))<1))

/*checks for condition of convergence*/

{
printf("Condition of convergence is not satisfied. \n \n value of |g'(x)| is greater than 1 at x=%lf",x2);
exit(1);
}

error1=error;
error=fabs(x2-x1);

if(i==0)
{ printf("--------\n");
}
else
{ printf(" %d\t %lf\t %lf\t %lf\t%lf ",i,x1,f(x1),der(x1),error);
n=log(error)/log(error1);

/* calculation of order of convergence*/

printf("%lf\n",fabs(n));
}
x1=x2;
x2=x1-(f(x1)/der(x1));
i++;
}
printf("Root is %lf\n",x2);
}
OUTPUT
Enter initial approximation to root::0
Enter degree of precision::4
i

Xi

f(Xi)

0.000000

-3.000000

-3.000000

-27.000000

f'(Xi)
-1.000000
26.000000

abserror
0.000000
1.038462

order of convergence
-------0.034353

Condition of convergence is not satisfied.


value of |g'(x)| is greater than 1 at x=-1.147176

c)
Equation: tan(inverse)x =0

/* equation from which value of x is to be found*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double f(double x)

/* finds value of f(x)*/

{ double k;
k=(double)atan(x);
return k;
}
double der(double x)

/* finds derivative of f (x) */

{
double k;
k=1+(x*x);
k=1/k;
return k;
}
double f2(double x)
{
double k;
k= pow(2,(1+(x*x)));
k=(-2)*(x)/k;
return k;
}
main()
{ double x1,x2,error=0,error1,t1,e;

/* finds second order derivative of f(x) */

int i=0;
double n;
printf("Enter initial approximation to root::");

/* inputs initial root from user*/

scanf("%lf",&x1);
printf("Enter degree of precision::");

/*takes degree of precision as input*/

scanf("%lf",&e);
e=5*pow(0.1,(e+1));
printf(" i\t Xi\t\t f(Xi)\t\tf'(Xi)\t\tabserror\t order of convergence \n");
if(der(x1)==0)

/* if der(x1)==0,process is stopped and no root exists*/

{
printf("condition of convergence is not satisfied. \n value of f'(x) is zero at x=%lf",x1);
exit(0);
}
if(!(fabs((f(x1)*f2(x1))/pow(der(x1),2))<1))

/*checks for condition of convergence*/

{
printf("Condition of convergence is not satisfied. \n \n value of |g'(x)| is greater than 1 at x=%lf",x2);
exit(1);
}

x2=x1-(f(x1)/der(x1));
if(i==0)
{ printf(" %d\t%lf\t %lf\t %lf\t%lf ",i,x1,f(x1),der(x1),error);
}
while(fabs(x2-x1)>e)
{ if(der(x1)==0)
{
printf("condition of convergence is not satisfied. \n value of f'(x) is zero at x=%lf",x1);
exit(0);
}

if(!(fabs((f(x2)*f2(x2))/pow(der(x2),2))<1))

/*checks for condition of convergence*/

{
printf("condition of convergence is not satisfied");
exit(1);
}
error1=error;
error=fabs(x2-x1);
if(i==0)
{ printf("--------\n");
}
else
{ printf("%d\t %lf\t %lf\t %lf\t%lf ",i,x1,f(x1),der(x1),error);
n=log(error)/log(error1);

/* calculation of order of convergence*/

printf("%lf\n",fabs(n));
}
x1=x2;
x2=x1-(f(x1)/der(x1));
i++;
}
printf("Root is %lf\n",x2);
}
OUTPUT
Enter initial approximation to root::1.4
Enter degree of precision::4
i

Xi

f(Xi)

f'(Xi)

abserror

order of convergence

Condition of convergence is not satisfied.


value of |g'(x)| is greater than 1 at x=0.000000
d)
Equation : x^3 3x + 2 =0

/* equation from which value of x is to be found*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
float f(float x)

/* finds value of f(x)*/

{ float k;
k=(x*x*x)-(3*x)+2;
return k;
}
float der(float x)

/* finds derivative of f (x) */

{
float k;
k=(3*x*x)-3;
return k;
}
float f2(float x)

/* finds second order derivative of f(x) */

{
float k;
k= (6*x);
return k;
}
main()
{ double x1,x2,error=0,error1,e;
int i=0;
double n;
printf("Enter initial approximation to root::");

/* inputs initial root from user*/

scanf("%lf",&x1);

printf("Enter degree of precision::");


scanf("%lf",&e);

/*takes degree of precision as input*/

e=5*pow(0.1,(e+1));
printf(" i\t Xi\t\t f(Xi)\t\tf'(Xi)\t\tabserror\t order of convergence \n");
if(der(x1)==0)

/* if der(x1)==0,process is stopped and no root exists*/

{
printf("condition of convergence is not satisfied. \n value of f'(x) is zero at x=%lf",x1);
exit(0);
}
if(!(fabs((f(x1)*f2(x1))/pow(der(x1),2))<1))

/* if der(x1)==0,process is stopped and no root exists*/

{
printf("Condition of convergence is not satisfied. \n \n value of |g'(x)| is greater than 1 at x=%lf",x2);
exit(1);
}
x2=x1-(f(x1)/der(x1));
error=fabs(x2-x1);
if(i==0)
printf(" %d\t%lf\t %lf\t %lf\t%lf ",i,x1,f(x1),der(x1),error);
while( fabs (x2-x1) > e )
{ if(der(x1)==0)
{
printf("condition of convergence is not satisfied. \n value of f'(x) is zero at x=%lf",x1);
exit(0);
}
if(!(fabs((f(x2)*f2(x2))/pow(der(x2),2))<1))
{
printf("conditiuon of convergence is not satisfied");
exit(1);
}
error1=error;
error=fabs(x2-x1)

/*checks for condition of convergence*/

if(i==0)
printf("--------\n");
else
{ printf(" %d\t %lf\t %lf\t %lf\t%lf ",i,x1,f(x1),der(x1),error);
n=log(error)/log(error1);

/* calculation of order of convergence*/

printf("%lf\n",fabs(n));
}
x1=x2;
x2=x1-(f(x1)/der(x1));
i++;
}
printf("Root is %lf\n",x2);
}
OUTPUT
Enter initial approximation to root::1.2
Enter degree of precision::4
i

Xi

f(Xi)

f'(Xi)

abserror

order of convergence

1.200000

0.128000

1.320000

0.096970

--------

1.103030

0.032939

0.650028

0.050674

1.278135

1.052356

0.008367

0.322362

0.025956

1.224328

1.026401

0.002109

0.160496

0.013143

1.186366

1.013258

0.000530

0.080074

0.006614

1.158513

1.006643

0.000133

0.039993

0.003318

1.137463

1.003325

0.000033

0.019985

0.001662

1.121138

1.001664

0.000008

0.009990

0.000832

1.000832

0.000002

0.004995

0.000416

1.097663

1.000416

0.000001

0.002497

0.000208

1.089029

10

1.000208

0.000000

0.001248

0.000104

1.081755

11

1.000104

0.000000

0.000624

0.000052

1.075515

1.108179

Root is 1.000026

6. Write a program to implement GaussElimination method for finding roots of simultaneous


equations.
Equation::
x1 + x2 x3 + x4 =2;
2 x1 + x2 - x3 3 x4 =1;
3 x1 -- x2 x3 + x4 =2;
5 x1 + x2 +3 x32 x4 =7;

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define rows 4
#define cols 5
void printmatrix( float [][cols] );
void pivot(float(*)[cols],int,int);
main()
{

float matrix[rows][cols]={ {1,1,-1,1,2},

/* defining coefficient matrix */

{2,1,1,-3,1},
{3,-1,-1,1,2},
{5,1,3,-2,7},
};
int k=0,i,j,p;
float m,max,result[rows],temp,t;
printf("initial coefficient matrix is:: \n");
printmatrix(matrix);
while(k<rows-1)
{ max=matrix[k][k];
p=k;

/* performs pivotal row technique for each row*/

for(i=k+1;i<rows;i++)
{ if(matrix[i][k]>max)
{ max=matrix[i][k];
p=i;
}
}
if(p!=k)
pivot(matrix,p,k);
for(i=k+1;i<rows;i++)
{

if(matrix[k][k]!=0)

/* checking for linear dependency of equations*/

m=matrix[i][k]/matrix[k][k];

/* generates multiplier for the step*/

else
{printf("solution does not exist\n");
exit(0);
}
for(j=k; j<=rows;j++)
{ matrix[i][j]= matrix[i][j] -- matrix[k][j]*m;

/* reduces coefficient of column k below row k to zero*/

t=fabs(matrix[i][j]);
if(t>0&&t<(pow(0.1,3)))
matrix[i][j]=0;
}
printmatrix(matrix)
}
k++;
}
printf("Final matrix after all arrangements are::\n");
printmatrix(matrix);
printf("Solution is ::\n");
for(i=rows-1;i>=0;i--)

/*finds solution from upper triangular matrix*/

{
temp=matrix[i][cols-1];
for(j=rows-1;j>i;j--)
temp-=matrix[i][j]*result[j];
temp=temp/matrix[i][i];
result[i]=temp;
}
for(i=0;i<rows;i++)
printf(" X%d----%0.2f \n",i+1,result[i]);
}
void printmatrix(float a[][cols])

/* function to print coefficient matrix*/

{ int i,j;
for(i=0;i<rows;i++)
{ for(j=0;j<cols;j++)
printf("%0.2f ",a[i][j]);
printf("\n");
}
printf("\n\n\n");
return;
}
void pivot(float a[][cols],int m,int n)
{
float temp;
int k;
for(k=n;k<cols;k++)
{
temp=a[n][k];
a[n][k]=a[m][k];
a[m][k]=temp;

/* function to implement pivotal row technique*/

}
return;
}
OUTPUT
initial coefficient matrix is::
1.00 1.00 -1.00 1.00 2.00
2.00 1.00 1.00 -3.00 1.00
3.00 -1.00 -1.00 1.00 2.00
5.00 1.00 3.00 -2.00 7.00

5.00 1.00 3.00 -2.00 7.00


0.00 0.60 -0.20 -2.20 -1.80
3.00 -1.00 -1.00 1.00 2.00
1.00 1.00 -1.00 1.00 2.00

5.00 1.00 3.00 -2.00 7.00


0.00 0.60 -0.20 -2.20 -1.80
0.00 -1.60 -2.80 2.20 -2.20
1.00 1.00 -1.00 1.00 2.00

5.00 1.00 3.00 -2.00 7.00


0.00 0.60 -0.20 -2.20 -1.80
0.00 -1.60 -2.80 2.20 -2.20
0.00 0.80 -1.60 1.40 0.60

5.00 1.00 3.00 -2.00 7.00


0.00 0.80 -1.60 1.40 0.60
0.00 0.00 -6.00 5.00 -1.00
0.00 0.60 -0.20 -2.20 -1.80

5.00 1.00 3.00 -2.00 7.00


0.00 0.80 -1.60 1.40 0.60
0.00 0.00 -6.00 5.00 -1.00
0.00 0.00 1.00 -3.25 -2.25

5.00 1.00 3.00 -2.00 7.00


0.00 0.80 -1.60 1.40 0.60
0.00 0.00 1 .00 -3.25 -2.25
0.00 0.00 0.00 -14.50 -14.50

Final matrix after all arrangements ::::


5.00 1.00 3.00 -2.00 7.00
0.00 0.80 -1.60 1.40 0.60
0.00 0.00 1.00 -3.25 -2.25
0.00 0.00 0.00 -14.50 -14.50

Solution is ::
X1----1.00
X2----1.00
X3----1.00

X4----1.00

7. Write a program to implement GaussSeidel Iterative method for finding roots of simultaneous
equations.
x1 +6 x2 =2;
x1 -- 2 x2 -- 6x3 =14;
9 x1 4 x2 + x3 = --17;
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define rows 3
#define cols 4
void printmatrix(float[][cols]);
void pivot(float(*)[cols],int,int);
main()
{
float matrix[rows][cols]={ {1,6,0,4},

/*defining coefficient matrix*/

{1,-2,-6,14},
{9,4,1,-17}
};
int k=0,i,j,p;
float m,max,Xo[rows],Xn[rows],temp,t,sum,e;
printf("initial coefficient matrix is:: \n");
printmatrix(matrix);
while(k<rows-1)
{ max=matrix[k][k];
p=k;
for(i=k+1;i<rows;i++)

/* performs pivoting of each row*/

{ if(matrix[i][k]>max)
{ max=matrix[i][k];
p=i;
}
}
if(p!=k)
pivot(matrix,p,k);
k++;
}
printf("matrix after pivoting is::\n");
printmatrix(matrix);
for(i=0;i<rows;i++)
{ sum=0;
for(j=0;j<rows;j++)
{ if(j!=i)
sum=sum+fabs(matrix[i][j]);
}
If ( sum >= fabs(matrix[i][i]))

/* checking for condition of convergence */

{ printf(" Condition of convergence is not satisfied\n");


exit(0);
}
}
printf("Enter degree of precision:: ");
scanf("%f",&e);
e=0.5*pow(0.1,e);
for(i=0;i<rows;i++)
{Xn[i]=0;
}
int count=0;

/* inputs degree of precision from user*/

while(1)
{ count++;
for(i=0;i<rows;i++)
{Xo[i]=Xn[i];
}
for(i=0;i<rows;i++)
{
sum=0;
for(j=0;j<i;j++)
sum=sum+matrix[i][j]*Xn[j];
for(j=i+1;j<rows;j++)
sum=sum+matrix[i][j]*Xo[j];
if(matrix[i][i]!=0)

/* checking for linear dependency of equations*/

Xn[i]=(matrix[i][rows]-sum)/matrix[i][i];
else
{ printf("Solution does not exist");
exit(0);
}
}
max=fabs(Xn[0]-Xo[0]);
for(i=1;i<rows;i++)
{ if((fabs(Xn[i]-Xo[i]))>max)
max=fabs(Xn[i]-Xo[i]);
}
if(max<e)
break;
}
printf("After %d iterations ",count);
printf(" Solution Set is :: \n");

/* printing solution matrix */

for(i=0;i<rows;i++)
{ printf("X%d= %lf\n",(i+1),Xn[i]);
}
}
void printmatrix(float a[][cols])

/* function to print coefficient matrix*/

{ int i,j;
for(i=0;i<rows;i++)
{ for(j=0;j<cols;j++)
printf("%0.2f ",a[i][j]);
printf("\n");
}
printf("\n\n\n");
return;
}
void pivot(float a[][cols],int m,int n)
{
float temp;
int k;
for(k=n;k<cols;k++)
{
temp=a[n][k];
a[n][k]=a[m][k];
a[m][k]=temp;
}
return;
}
OUTPUT

initial coefficient matrix is::

/* function to implement pivotal row technique*/

1.00 6.00 0.00 4.00


1.00 -2.00 -6.00 14.00
9.00 4.00 1.00 -17.00

matrix after pivoting is::


9.00 4.00 1.00 -17.00
1.00 6.00 0.00 4.00
1.00 -2.00 -6.00 14.00
Enter degree of precision:: 4
After 5 iterations
Solution Set is ::
X1= -1.999999
X2= 1.000000
X3= -3.000000

8. Write a program to implement Lagrange Interpolation method.

X
Y

1
1

2
1

3
2

#include<stdio.h>
#include<stdlib.h>
main()
{ double *X,*Y,x,pro;
int n,i=0,j;
printf("Enter number of set of values in the table::");
scanf("%d",&n);
X=(double*)malloc((n+1)*sizeof(double));
Y=(double*)malloc((n+1)*sizeof(double));
printf("Enter the (x,y) values:\n");

/* dynamic allocation of (x,y) set of values*/

for(i=0;i<n;i++)
{

printf("Enter (X%d,Y%d): ",i,i);


scanf("%lf %lf",&X[i],&Y[i]);
putchar('\n');

}
printf("Enter the value to interpolate: "); /*inputs the value of x for which y is to be found*/
scanf("%lf",&x);

for(i=0;i<n;i++)
{
if(x==X[i])

/* if x is one of tabulated value, then no need to calculate*/

{
printf(" the value for which you want to find the value of y is a tabulated value.value:::::: %lf\n",Y[i]);
exit(0);
}
}
double sum=0;
for(j=0;j<n;j++)
{ pro=1;
for(i=0;i<n;i++)
{ if(j!=i)
pro=pro*((x-X[i])/(X[j]-X[i]));

/* calculation for interpolation*/

}
sum=sum+Y[j]*pro;
}
printf("Corresponding Value of y is:: %lf\n",sum);
}
OUTPUT
Enter number of set of values in the table::3

Enter the (x,y) values:


Enter (X0,Y0): 1 1
Enter (X1,Y1): 2 1
Enter (X2,Y2): 3 2
Enter the value to interpolate: 2.5
Corresponding Value of y is:: 1.375000

9.Write a program to find value of integration by Trapezoidal method.


F(x) = 1/(1+x)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double f(double x)

/* finds value of f(x) */

{ double k;
k=1+x;
k=1/k;
return k;
}
main()
{
int i=0,j,c=1;
double h,a,b,sum,I0,I1,e,k,x,error;
while(c==1)
{printf("Enter the interval values of the integral::");

/* inputs the lower and upper limit of integral */

scanf("%lf %lf",&a,&b);
if(b<=a)
printf("Upper bound must be greater than lower bound\n");
else
c=0;

}
printf("Enter degree of precision::");

/* inputs degree of precision*/

scanf("%lf",&e);
e=5*pow(0.1,(e+1));
h=b-a;
I0=h*(f(b)+f(a))/2;
printf("ITERATION\t INTERVAL\t INTEGRAL\t ERROR \n ");

/* format for printing */

printf("%d\t\t%lf\t %lf\t -------- \n",i,h,I0);


I1=I0;
do
{ i++;
h=h/2;

/* divides the interval into half */

I0=I1;
I1=f(a);
x=a+h;
while(x<b)

/* finds value of integral*/

{
I1=I1+2*f(x);
x=x+h;
}
I1=I1+f(b);
I1=h*I1/2;
error=fabs(I1-I0);

/* calculates difference between results*/

printf("%d\t\t%lf\t %lf\t%lf\n",i,h,I1,error);
} while(error>e);
printf("Value of the integral is:: %lf\n",I1);
}
OUTPUT
Enter the interval values of the integral::0 1

Enter degree of precision::4


ITERATION

INTERVAL

INTEGRAL

ERROR

1.000000

0.750000

--------

0.500000

0.708333

0.041667

0.250000

0.697024

0.125000

0.694122

0.002902

0.011310

0.062500

0.693391

0.000731

0.031250

0.693208

0.000183

0.015625

0.693162

0.000046

Value of the integral is:: 0.693162

10.Write a program to solve a differential equation by Eulers modified method.


dy/dx = 2x^2+2y
y(exact)=1.5e^2xx^2x0.5
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x,y) ((2*x*x)+(2*y))

/* macro for dy/dx */

#define exact(x) (1.5*exp(2*x)-x*x-x-0.5) /* macro for exact value of y*/


main()
{ int c=1;
float a,b,y0,x0,h,e,Yold,Ynew,k,error,x,y;
printf("Enter initial value of y::");
scanf("%f",&y0);

/*inputs initial value of y*/

printf("Enter value of interval in between values of x::");


scanf("%f",&h);

/*inputs interval in between values of x*/

while(c==1)
{
printf("Enter range for the value of x::\n");

printf("lower bound::");
scanf("%f",&a);
printf("Upper bound::");
scanf("%f",&b);
if(b<=a)
printf("Upper bound must be greater than lower bound\n");
else
c=0;
}
printf("Enter degree of precision::");
scanf("%f",&e);
e=5*pow(0.1,(e+1));
x0=a;
printf(" X\t\t Y\t\t Yactual\t ERROR\n");

/* format for printing */

printf(" %f\t%f\t%f\t%f \n",x0,y0,exact(x0),(exact(x0)-y0));


x=x0+h;
while(x0<=b)
{ Ynew=y0+h*f(x0,y0);
while(1)
{ Yold=Ynew;
Ynew=y0+h*(f(x0,y0)+f(x,Yold))/2;

/*predicting values of y*/

k=fabs(Yold-Ynew);

/*calculating difference from previous value*/

if(k<e)
break;
}
k=exact(x);
error=fabs(k-Ynew);

/* calculating relative error*/

error=error/k;
printf(" %f\t%f\t%f\t%f \n",x,Ynew,k,error);

y0=Ynew;
x0=x;
x=x+h;
}
}
OUTPUT
Enter initial value of y::1
Enter value of interval in between values of x::0.1
Enter range for the value of x::
lower bound::0
Upper bound::1
Enter degree of precision::4

Yactual

ERROR

0.000000

1.000000

1.000000

0.000000

0.100000

1.223331

1.222104

0.001004

0.200000

1.500735

1.497737

0.002002

0.300000

1.848672

1.843178

0.002981

0.400000

2.287261

2.278311

0.003928

0.500000

2.841096

2.827423

0.004836

0.600000

3.540227

3.520175

0.005696

0.700000

4.421388

4.392800

0.006508

0.800000

5.529473

5.489550

0.007273

0.900000

6.919354

6.864473

0.007995

1.000000

8.658097

8.583587

0.008681

You might also like