numlabreport
Transcript of numlabreport
-
8/2/2019 numlabreport
1/41
1.Write a c program to evaluate value of e correct upto 3 decimal places.
#include
#include
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)
-
8/2/2019 numlabreport
2/41
---------------------------------------
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: 3xcosx = 1 /* equation from which value of x is to be found*/
#include
#include
#include
#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::"); /* getting initial interval as input*/
scanf("%f %f",&a,&b);
if(f(a)*f(b)
-
8/2/2019 numlabreport
3/41
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"); /* format to print the output*/
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)
{
-
8/2/2019 numlabreport
4/41
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)
-
8/2/2019 numlabreport
5/41
Enter value of a and b::0 1
Enter degree of precision::4
i a b m abserror order of convergence
0 0.000000 1.000000 0.500000 ------ -----
1 0.500000 1.000000 0.750000 0.250000 ------
2 0.500000 0.750000 0.625000 0.125000 1.500000
3 0.500000 0.625000 0.562500 0.062500 1.333333
4 0.562500 0.625000 0.593750 0.031250 1.250000
5 0.593750 0.625000 0.609375 0.015625 1.200000
6 0.593750 0.609375 0.601563 0.007813 1.166667
7 0.601563 0.609375 0.605469 0.003906 1.142857
8 0.605469 0.609375 0.607422 0.001953 1.125000
9 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: 3xcosx = 1 /* equation from which value of x is to be found*/
#include
#include
#include
#define f(x) (3*x-cos(x)-1) /*macro defining the function*/
main()
{ float a,b,m1=0,m2=0,error1,error=0,e;
-
8/2/2019 numlabreport
6/41
double n;
int d=1,i=0;
while(d==1)
{ printf("Enter value of a and b::"); /* getting initial interval as input*/
scanf("%f %f",&a,&b);
if(f(a)*f(b)
-
8/2/2019 numlabreport
7/41
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); /* calculation of order of convergence*/
printf("%lf\n",fabs(n));
}
}
If ( f(m2)==0)
break;
else
{ if ( f(a)*f(m2)
-
8/2/2019 numlabreport
8/41
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| m abserror order of convergence
0 1.000000 0.578085 ------ ----
1 0.421915 0.605959 0.027873 ------
2 0.394041 0.607057 0.001099 1.903251
3 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
#include
#include
float f(float x) /* expressing as x = g(x)*/
{ return((log10(x)+7)/2);
}
float der(float x) /* finds derivative of g(x)*/
-
8/2/2019 numlabreport
9/41
{
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(x11) /*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.");
-
8/2/2019 numlabreport
10/41
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
0 1.000000 3.500000 2.500000 --------
1 3.500000 3.772034 0.272034 1.420759
2 3.772034 3.788288 0.016254 3.164333
3 3.788288 3.789222 0.000934 1.693543
4 3.789222 3.789275 0.000053 1.410122
5 3.789275 3.789278 0.000003 1.289369
-
8/2/2019 numlabreport
11/41
6 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
#include
#include
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) /* finds second order derivative of f(x) */
{
float k;
k=exp(-x)*(x-2);
return k;
}
main()
{ double x1,x2,error=0,error1,e;
int i=0;
-
8/2/2019 numlabreport
12/41
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))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);
}
-
8/2/2019 numlabreport
13/41
if(!(fabs((f(x2)*f2(x2))/pow(der(x2),2))
-
8/2/2019 numlabreport
14/41
4 14.380524 0.000008 -0.000008 1.074736 0.922029
5 15.455260 0.000003 -0.000003 1.069179 0.928081
6 16.524439 0.000001 -0.000001 1.064415 0.933232
7 17.588853 0.000000 -0.000000 1.060281 0.937677
8 18.649135 0.000000 -0.000000 1.056660 0.941549
9 19.705795 0.000000 -0.000000 1.053459 0.944956
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
-
8/2/2019 numlabreport
15/41
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 0.981572
45 56.781463 0.000000 -0.000000 1.017927 0.981904
46 57.799390 0.000000 -0.000000 1.017606 0.982236
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
-
8/2/2019 numlabreport
16/41
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
-
8/2/2019 numlabreport
17/41
b) Equation :: x^3x3=0 /* equation from which value of x is to be found*/
#include
#include
#include
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) /* 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;
-
8/2/2019 numlabreport
18/41
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))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);
}
-
8/2/2019 numlabreport
19/41
if(!(fabs((f(x2)*f2(x2))/pow(der(x2),2))
-
8/2/2019 numlabreport
20/41
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
#include
#include
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) /* finds second order derivative of f(x) */
{
double k;
k= pow(2,(1+(x*x)));
k=(-2)*(x)/k;
return k;
}
main()
{ double x1,x2,error=0,error1,t1,e;
-
8/2/2019 numlabreport
21/41
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))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);
}
-
8/2/2019 numlabreport
22/41
if(!(fabs((f(x2)*f2(x2))/pow(der(x2),2))
-
8/2/2019 numlabreport
23/41
#include
#include
#include
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::"); /*takes degree of precision as input*/
scanf("%lf",&e);
-
8/2/2019 numlabreport
24/41
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)) 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))
-
8/2/2019 numlabreport
25/41
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
0 1.200000 0.128000 1.320000 0.096970 --------
1 1.103030 0.032939 0.650028 0.050674 1.278135
2 1.052356 0.008367 0.322362 0.025956 1.224328
3 1.026401 0.002109 0.160496 0.013143 1.186366
4 1.013258 0.000530 0.080074 0.006614 1.158513
5 1.006643 0.000133 0.039993 0.003318 1.137463
6 1.003325 0.000033 0.019985 0.001662 1.121138
7 1.001664 0.000008 0.009990 0.000832 1.108179
8 1.000832 0.000002 0.004995 0.000416 1.097663
9 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
-
8/2/2019 numlabreport
26/41
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
#include
#include
#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
-
8/2/2019 numlabreport
27/41
for(i=k+1;imax)
{ max=matrix[i][k];
p=i;
}
}
if(p!=k)
pivot(matrix,p,k);
for(i=k+1;i
-
8/2/2019 numlabreport
28/41
{
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
-
8/2/2019 numlabreport
29/41
}
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
-
8/2/2019 numlabreport
30/41
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
-
8/2/2019 numlabreport
31/41
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
#include
#include
#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
-
8/2/2019 numlabreport
32/41
{ 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
-
8/2/2019 numlabreport
33/41
while(1)
{ count++;
for(i=0;i
-
8/2/2019 numlabreport
34/41
for(i=0;i
-
8/2/2019 numlabreport
35/41
-
8/2/2019 numlabreport
36/41
for(i=0;i
-
8/2/2019 numlabreport
37/41
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
#include
#include
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
-
8/2/2019 numlabreport
38/41
}
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(xe);
printf("Value of the integral is:: %lf\n",I1);
}
OUTPUT
Enter the interval values of the integral::0 1
-
8/2/2019 numlabreport
39/41
Enter degree of precision::4
ITERATION INTERVAL INTEGRAL ERROR
0 1.000000 0.750000 --------
1 0.500000 0.708333 0.041667
2 0.250000 0.697024 0.011310
3 0.125000 0.694122 0.002902
4 0.062500 0.693391 0.000731
5 0.031250 0.693208 0.000183
6 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
#include
#include
#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");
-
8/2/2019 numlabreport
40/41
printf("lower bound::");
scanf("%f",&a);
printf("Upper bound::");
scanf("%f",&b);
if(b
-
8/2/2019 numlabreport
41/41
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
X Y 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