Upload
glen-flav-pais
View
213
Download
0
Embed Size (px)
Citation preview
8/2/2019 Program No(1)
1/38
*********************************************************************
Program No:1
Program using Newtons Backward Interpolation Formula
*********************************************************************
#include
#include
#define Err 0.000005
long Fact(int x);
main()
{
int i,j,m;
float P,Z,P1,Poly,Diff,X[20],Y[20];
printf(" ***************************************\n");
printf(" Newton's Backward Interpolation formula.\n");
printf(" ***************************************\n");
printf(" Enter the number of points.");scanf("%d",&m);
printf(" Enter the number to be searched.");
scanf("%f",&Z);
printf(" Enter the points.\n");
for(i=1;i
8/2/2019 Program No(1)
2/38
printf("\t X\t\t Y\n");
printf(" ***************************************\n");
for(i=1;i
8/2/2019 Program No(1)
3/38
Output
***************************************
Newton's Backward Interpolation formula.
***************************************
Enter the number of points.5
Enter the number to be searched.1975
Enter the points.
X[1]=1941
Y[1]=46
X[2]=1951
Y[2]=65
X[3]=1961
Y[3]=81
X[4]=1971
Y[4]=93
X[5]=1981Y[5]=101
***************************************
Newton's Backward Interpolation formula.
***************************************
***************************************
X Y
***************************************
1941.0000 46.00001951.0000 65.0000
1961.0000 81.0000
1971.0000 93.0000
1981.0000 101.0000
**************************************
Value of F(1975.00) : 96.646400
**************************************
8/2/2019 Program No(1)
4/38
*********************************************************************
Program No:2
Program using trapezoidal rule to find the integral value when function is given
*********************************************************************
#include
#include
float F(float x);
main()
{
int i;
float a,b,h,N,Trap,Int1;
printf(" *********************************************************\n");
printf(" Program to find integral value using trapezoidal rule.\n");
printf(" when function is given. \n");
printf(" *********************************************************\n");
printf(" Enter the lower limit of integral.");scanf("%f",&a);
printf(" Enter the upper limit of integral.");
scanf("%f",&b);
printf(" Enter the divided point.");
scanf("%f",&h);
N=(b-a)/h;
Int1=0;
Trap=0;
for(i=1;i
8/2/2019 Program No(1)
5/38
Output
***********************************************************
Program to find integral value using trapezoidal rule.
when function is given.
*********************************************************
Enter the lower limit of integral.0
Enter the upper limit of integral.1
Enter the divided point.0.5
*********************************************************
Program to find integral value using trapezoidal rule.
when function is given.
*********************************************************
Lower limit : 0.00
Upper limit : 1.00
Divided Point : 0.50
Function : 1/(1+x)Integral Value : 0.731370
8/2/2019 Program No(1)
6/38
*********************************************************************
Program No:3
Program using Newtons Divided Difference Formula
*********************************************************************
#include
#include
main()
{
int i,j,k,m;
float Z,Factor,Diff,Dr,Poly,X[20],Y[20];
printf(" *******************************************\n");
printf(" Newton's Divided Difference formula.\n");
printf(" *******************************************\n");
printf(" Enter the number of points.");
scanf("%d",&m);printf(" Enter the number to be searched.");
scanf("%f",&Z);
printf(" Enter the points.\n");
for(i=1;i
8/2/2019 Program No(1)
7/38
for(i=1;i
8/2/2019 Program No(1)
8/38
Output
*******************************************
Newton's Divided Difference formula.
*******************************************
Enter the number of points.4
Enter the number to be searched.301
Enter the points.
X[1]=300
Y[1]=2.4771
X[2]=304
Y[2]=2.4829
X[3]=305
Y[3]=2.4843
X[4]=307
Y[4]=2.4871
*****************************************
La Granges' Inverse Interpolation formula.
***************************************
Y X
***************************************
2.4771 300.0000
2.4829 304.0000
2.4843 305.0000
2.4871 307.0000
**************************************
Value of X when Y=301.00 : 2.477100
**************************************
8/2/2019 Program No(1)
9/38
*********************************************************************
Program No: 4
Program to implement Simpsons 1/3rd
rule .
*********************************************************************
#include
#include
main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
printf("\nhow many record you will be enter: ");
scanf("%d",&n);
for(i=0; i
8/2/2019 Program No(1)
10/38
Output
-------------------------------------------------------------------------------------------------------
how many record you will be enter: 5
enter the value of x0: 1
enter the value of f(x0): 1
enter the value of x1: 1.5
enter the value of f(x1): .666
enter the value of x2: 2
enter the value of f(x2): .5
enter the value of x3: 2.5
enter the value of f(x3): .4
enter the value of x4: 3
enter the value of f(x4): .333
I = 1.099500
-------------------------------------------------------------------------------------------------------
8/2/2019 Program No(1)
11/38
*********************************************************************
Program No:5
Program to fit an exponential function by the method of least squares
*********************************************************************
#include
#include
main()
{
int N,i;
float A[20],B[20],SumX,SumY,SumXX,SumXY,A0,A1,P;
printf("Program to fit an exponential function\n");
printf("=======================================\n\n\n");
printf("Enter the No. of points :");
scanf("%d",&N);
printf("Enter the points :\n");
for(i=1;i
8/2/2019 Program No(1)
12/38
Output
----------------------------------------------
Program to fit an exponential function
----------------------------------------------
Enter the No. of points :5
Enter the points :
0
0.1
0.5
0.45
1.0
2.15
1.5
9.15
2
40.35
-------------------------------------------------------------------------------------------------------PROGRAM TO FIT AN EXPONENTIAL FUNCTION
-------------------------------------------------------------------------------------------------------
No. of points : 5
The entered points are:
x y
===============
0.00 0.10
0.50 0.451.00 2.15
1.50 9.15
2.00 40.35
The constants P and A1 which best fit an exponential function are:
P= 0.1015
A1= 3.0025
The exponential function is : Y=0.1015*e^3.0025X
8/2/2019 Program No(1)
13/38
*********************************************************************
Program No:6
Program to fit a straight line
*********************************************************************
#include
#include
main()
{
int i,j,n;
float a[10],b[10],sumx,sumy,sumxx,sumxy,A0,A1;
printf(" ************************************\n");
printf(" Program for fitting a straight line.\n");
printf(" ************************************\n");
printf(" Enter the number of data elements.");
scanf("%d",&n);
printf(" Enter the values for a and b.\n");
for(i=1;i
8/2/2019 Program No(1)
14/38
Output
************************************
Program for fitting a straight line.
************************************
Enter the number of data elements.6
Enter the values for a and b.
A[1] =2
B[1] =7.32
A[2] =4
B[2] =8.24
A[3] =6
B[3] =9.2
A[4] =8
B[4] =10.19
A[5] =10
B[5] =11.01
A[6] =12
B[6] =12.05*****************************************
Program for fitting a straight line.
*****************************************
Points are ....
A B
2.0000 7.3200
4.0000 8.2400
6.0000 9.2000
8.0000 10.1900
10.0000 11.0100
12.0000 12.0500******************************************
The calculated value of A0 : 6.3733
The calculated value of A1 : 0.4707
******************************************
Equation of the straight line : (A0+A1*X)
6.3733 + 0.4707 X
******************************************
8/2/2019 Program No(1)
15/38
*********************************************************************
Program No:7
Program using Newtons Forward Interpolation formula Formula
*********************************************************************
#include
#include
#define Err 0.000005
long Fact(int x);
main()
{
int i,j,m;
float P,Z,P1,Poly,Diff,X[20],Y[20];
printf(" ***************************************\n");
printf(" Newton's Forward Interpolation formula.\n");
printf(" ***************************************\n");
printf(" Enter the number of points.");scanf("%d",&m);
printf(" Enter the number to be searched.");
scanf("%f",&Z);
printf(" Enter the points.\n");
for(i=1;i
8/2/2019 Program No(1)
16/38
for(i=1;i
8/2/2019 Program No(1)
17/38
Output
***************************************
Newton's Forward Interpolation formula.
***************************************
Enter the number of points.4
Enter the number to be searched.1.7
Enter the points.
X[1]=1.6
Y[1]=0.625
X[2]=1.8
Y[2]=0.5556
X[3]=2.0
Y[3]=0.5
X[4]=2.2
Y[4]=0.4555
***************************************
Newton's Forward Interpolation formula.
***************************************
*****************************
X Y
*****************************
1.6000 0.62501.8000 0.5556
2.0000 0.5000
2.2000 0.4555
*****************************
Value of F(1.70) : 0.588406
*****************************
8/2/2019 Program No(1)
18/38
*********************************************************************
Program No:8
Program using Gauss Elimination method(solution of linear squares)
*********************************************************************
#include
#include
main()
{
int i,j,k,N;
float X[10],A[10][10],B[10][10];
printf(" *********************************************************\n");
printf(" Gaussian Elimination Method(Solution of linear systems).\n");
printf(" *********************************************************\n");
printf(" Enter the order of system N.");
scanf("%d",&N);
printf(" Enter the argmented matrix.\n\t");
for(i=1;i
8/2/2019 Program No(1)
19/38
for(i=1;i
8/2/2019 Program No(1)
20/38
Output
*********************************************************
Gaussian Elimination Method(Solution of linear systems).
*********************************************************
Enter the order of system N.3
Enter the argmented matrix.
2
1
1
10
3
2
3
18
1
49
16
*********************************************************
Gaussian Elimination Method(Solution of linear systems).
*********************************************************
The argmented matrix.
2.000000 1.000000 1.000000 10.000000
3.000000 2.000000 3.000000 18.000000
1.000000 4.000000 9.000000 16.000000
Roots are...
x1 = 7.0000
x2 = -9.0000
x3 = 5.0000
8/2/2019 Program No(1)
21/38
*********************************************************************
Program No:9
Program using LaGranges Inverse Interpolation Formula
*********************************************************************
#include
#include
main()
{
int i,j,m;
float Z,L,Poly,X[20],Y[20];
printf(" *******************************************\n");
printf(" La Granges' Inverse Interpolation formula.\n");
printf(" *******************************************\n");
printf(" Enter the number of points.");
scanf("%d",&m);
printf(" Enter the number to be searched.");scanf("%f",&Z);
printf(" Enter the points.\n");
for(i=1;i
8/2/2019 Program No(1)
22/38
Output
*******************************************
La Granges' Inverse Interpolation formula.
*******************************************
Enter the number of points.3
Enter the number to be searched.2.6
Enter the points.
X[1]=1
Y[1]=0
X[2]=3
Y[2]=1
X[3]=55
Y[3]=3
*****************************************
La Granges' Inverse Interpolation formula.
*****************************************
***************************************Y X
***************************************
0.0000 1.0000
1.0000 3.0000
3.0000 55.0000
**************************************
Value of X when Y=2.60 : 39.479996
**************************************
8/2/2019 Program No(1)
23/38
*********************************************************************
Program No:10
Program to find the root of the equation x3-2x-5 by Newtons Rapsons method
*********************************************************************
#include
#include
#define ERR 0.000005
#define F(X) ( pow(X,3)-(2*X)-5)
#define dF(X) (3*pow(X,2)-2)
main()
{
float X,X1;
printf("---------------------------------------------------------------------------------\n");
printf(" PROGRAM TO FIND THE ROOTS USING NEWTON RAPSON
METHOD \n");
printf("-------------------------------------------------------------------------------------\n");printf("\n\n");
printf("The given equation is x^3-2x-5 \n");
printf("\n\n");
printf("Enter the initial approximation: ");
scanf("%f",&X);
printf("\nThe approximations are:\n");
X1=X-((F(X))/(dF(X)));
while( fabs(X1-X)>ERR)
{
X=X1;printf("%3.5f\n",X);
X1=X-((F(X))/(dF(X)));
}
printf("\nThe root is %3.5f",X);
printf("\n-------------------------------------------------------------------------------------");
}
8/2/2019 Program No(1)
24/38
Output
-------------------------------------------------------------------------------------------------------
PROGRAM TO FIND THE ROOTS USING NEWTON RAPSON METHOD
-------------------------------------------------------------------------------------------------------
The given equation is x^3-2x-5
Enter the initial approximation: 3
The approximations are:
2.36000
2.12720
2.09514
2.09455
The root is 2.09455
-------------------------------------------------------------------------------------------------------
8/2/2019 Program No(1)
25/38
*********************************************************************
Program No : 11
Program for power function
*********************************************************************
#include
main()
{
int count,n;
float x,y;
printf("Enter the values of x&n:");
scanf("%f%d",&x,&n);
y=1.0;
count=1;
while(count
8/2/2019 Program No(1)
26/38
*********************************************************************
Program No:12
Program to fit a power function
*********************************************************************
#include
#include
main()
{
int i,n;
float a[10],b[10],sumx,sumy,sumxx,sumxy,A0,A1;
printf(" ************************************\n");
printf(" Program to fit a power function.\n");
printf(" ************************************\n");
printf(" Enter the number of data elements.");
scanf("%d",&n);
printf(" Enter the values for a and b.\n");
for(i=1;i
8/2/2019 Program No(1)
27/38
Output
************************************
Program to fit a power function.
************************************
Enter the number of data elements.6
Enter the values for a and b.
A[1] =2
B[1] =43
A[2] =4
B[2] =25
A[3] =7
B[3] =18
A[4] =10
B[4] =13
A[5] =20
B[5] =8A[6] =40
B[6] =5
*****************************************
Program to fit a power fuction.
*****************************************
Points are ....
A B
2.0000 43.0000
4.0000 25.0000
7.0000 18.0000
10.0000 13.000020.0000 8.0000
40.0000 5.0000
******************************************
The calculated value of A0 : 4.2452
The calculated value of A1 : -0.7183
******************************************
Power function : (A0*X^A1)
4.2452*X^-0.7183
******************************************
8/2/2019 Program No(1)
28/38
*********************************************************************
Program No: 13
Programto implement Cramers rule.
*********************************************************************
#include
double finddet(double a1,double a2, double a3,double b1, double b2,double b3,double c1, double c2, double c3);
main()
{
double a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3,det, detx, dety, detz;
printf("\n a1?");
scanf("%lf",&a1);
printf("\n b1?");
scanf("%lf",&b1);
printf("\n c1?");
scanf("%lf",&c1);
printf("\n d1?");
scanf("%lf",&d1);
printf("\n a2?");
scanf("%lf",&a2);
printf("\n b2?");
scanf("%lf",&b2);
printf("\n c2?");
scanf("%lf",&c2);
printf("\n d2?");
scanf("%lf",&d2);
printf("\n a3?");scanf("%lf",&a3);
printf("\n b3?");scanf("%lf",&b3);
printf("\n c3?");
scanf("%lf",&c3);
printf("\n d3?");
scanf("%lf",&d3);
det=finddet(a1,a2,a3,b1,b2,b3,c1,c2,c3);
detx=finddet(d1,d2,d3,b1,b2,b3,c1,c2,c3);
dety=finddet(a1,a2,a3,d1,d2,d3,c1,c2,c3);detz=finddet(a1,a2,a3,b1,b2,b3,d1,d2,d3);
if(d1==0 && d2==0 && d3==0 && det==0)
printf("\n Infinite Solutions\n ");
else if(d1==0 && d2==0 && d3==0 && det!=0)
printf("\n x=0\n y=0, \n z=0\n ");
else if(det!=0)
printf("\n x=%lf\n y=%lf\n z=%lf\n", (detx/det), (dety/det), (detz/det));
else if(det==0 && detx==0 && dety==0 && detz==0)
printf("\n Infinite Solutions\n ");
elseprintf("No Solution\n ");
}
8/2/2019 Program No(1)
29/38
double finddet(double a1,double a2, double a3,double b1, double b2,double b3,
double
c1, double c2, double c3)
{
return ((a1*b2*c3)-(a1*b3*c2)-(a2*b1*c3)+(a3*b1*c2)+(a2*b3*c1)-(a3*b2*c1));
}
8/2/2019 Program No(1)
30/38
Output
-------------------------------------------------------------------------------------------------------
a1?1
b1?1
c1?1
d1?7
a2?2
b2?3
c2?2
d2?17
a3?4
b3?9
c3?1
d3?37
x=2.000000
y=3.000000
z=2.000000
------------------------------------------------------------------------------------------------------
8/2/2019 Program No(1)
31/38
*********************************************************************
Program No:14
Program to fit a quadratic equation
*********************************************************************
#include
#include
main()
{
int i,j,k,n;
float x[10],y[10],a[10][10];
float sumx,sumx2,sumx3,sumx4,sumy,sumxy,sumxyz;
printf(" ************************************\n");
printf(" Program to fit a quadratic equation.\n");
printf(" ************************************\n");
printf(" Enter the number of data elements.");
scanf("%d",&n);printf(" Enter the values for arrays x and y.\n");
for(i=1;i
8/2/2019 Program No(1)
32/38
printf(" *****************************************\n");
printf(" Points are ....\n");
printf(" X Y\n");
for(i=1;i
8/2/2019 Program No(1)
33/38
8/2/2019 Program No(1)
34/38
*********************************************************************
Program No:15
Program to find the roots of the equation x3-2x-5 using regular falsi method
*********************************************************************
#include
#include
#define ERR 0.000005
#define F(X) (pow(X,3)-(2*X)-5)
main()
{
float A,B,mid;
printf("---------------------------------------------------------------\n");
printf(" PROGRAM TO FIND THE ROOT USING REGULAR FALSING
METHOD \n");
printf("-----------------------------------------------------------------------------------\n");printf("\n\n");
printf("The given equation is x^3-2x-5 \n");
printf("\n");
printf("Enter the values of A & B: ");
scanf("%f%f",&A,&B);
printf("\nThe approximations are:\n");
if((F(A)0) || (F(A)>0 && F(B) ERR )){
if(F(mid)
8/2/2019 Program No(1)
35/38
Output
-------------------------------------------------------------------------------------------------------
PROGRAM TO FIND THE ROOT USING REGULAR FALSING METHOD
-------------------------------------------------------------------------------------------------------
The given equation is x^3-2x-5
Enter the values of A & B: 2 3
The approximations are:
2.08126
2.08964
2.09274
2.09388
2.09431
2.09446
2.094522.09454
2.09455
2.09455
2.09455
2.09455
The root is 2.09455
-------------------------------------------------------------------------------------------------------
8/2/2019 Program No(1)
36/38
*********************************************************************
Program No : 16
Program using Simpsons 3/8 rule to find the integral value when function is given
*********************************************************************
#include
#include
float F(float x);
main()
{
int i,N,S;
float a,b,h,Int1,Int2,Simp;
printf(" *********************************************************\n");
printf(" Program using Simpson's 3/8th rule to find integral value \n");
printf(" when function is given. \n");
printf(" *********************************************************\n");printf(" Enter the lower limit of integral.");
scanf("%f",&a);
printf(" Enter the upper limit of integral.");
scanf("%f",&b);
printf(" Enter the divided point.");
scanf("%f",&h);
N=(b-a)/h;
Int1=0;
Int2=0;
Simp=0;
S=(int)N;if (S%2==0) {
i=1;
do{
if (i%3==0)
Int2=Int2+F(a+i*h);
else
Int1=Int1+F(a+i*h);
i=i+1;
}while(i
8/2/2019 Program No(1)
37/38
}
float F(float x)
{
return (1/(1+x));
}
8/2/2019 Program No(1)
38/38
Output
*********************************************************
Program using Simpson's 3/8th rule to find integral value
when function is given.
*********************************************************
Enter the lower limit of integral.0
Enter the upper limit of integral.1
Enter the divided point.0.5
*********************************************************
Program to find integral value using Simpson's 3/8th rule.
when function is given.
*********************************************************
Lower limit : 0.00
Upper limit : 1.00
Divided Point : 0.50Function : 1/x+1
Integral Value : 0.656250