0% found this document useful (0 votes)
131 views30 pages

Practical C++

The document discusses Newton's forward interpolation method. It includes code to calculate the forward difference table for a given set of data points, and then uses the difference table to calculate the interpolated value of a function at a given x-value using Newton's forward formula. The code takes input data points, calculates the forward difference table, and then interpolates the function value for a given x-value entered by the user.

Uploaded by

Harman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views30 pages

Practical C++

The document discusses Newton's forward interpolation method. It includes code to calculate the forward difference table for a given set of data points, and then uses the difference table to calculate the interpolated value of a function at a given x-value using Newton's forward formula. The code takes input data points, calculates the forward difference table, and then interpolates the function value for a given x-value entered by the user.

Uploaded by

Harman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

1.

Bisection Method

#include <stdio.h>
#include <stdlib.h> #include
<math.h> #define f(x)
((x*x*x)-18) int main(){
float x0=0,x1=0,error=0,m,prevm; int i=0;
printf("Input Interval:");
scanf("%f %f",&x0,&x1);
if((f(x0)*f(x1))>0){
printf("Invalid Interval Exit!");
exit(1);
}
else if(f(x0)==0 || f(x1)==0){
printf("Root is one of interval bounds. Root is %f\n",f(x0)==0?x0:x1); exit(0);
}
do{
prevm=m; m=(x0+x1)/2;
printf("iter=%2d x0=%4.6f x1=%4.6f m=%4.6f f(x0)=%4.6f f(x1)=%4.6f f(m)%4.6f
",i++,x0,x1,m,f(x0),f(x1),f(m));
if(f(m)==0){
printf("Root is %4.6f\n",m);
}
else if ((f(x0)*f(m))<0){ x1=m;
}
else
x0=m;
error=fabs(m-prevm); if(i==1){
printf(" ----- \n");
}else printf("err = %4.6f\n",error);
}while(error>0.00005); printf("Approximate
Root is %4.6f",m); return 0;
}
Output:

2. FALSE POSITION

#include <stdio.h>
#include <stdlib.h>
#include <math.h> #define
f(x) ((x*x*x)-18) int main()
{
float a=0,b=0,error=0,c,cold; int
i=0;
printf("Input Interval: ");
scanf("%f %f",&a,&b);
if((f(a)*f(b))>0){
printf("Invalid Interval Exit!");
exit(1);
}
else if(f(a)==0 || f(b)==0){
printf("Root is one of interval bounds. Root is %f\n",f(a)==0?a:b); exit(0);
}
printf("Ite\ta\t\tb\t\tc\t \tf(a)\t \tf(b)\t\t f(c)\t error\n"); do{
cold=c;
c=(((a*f(b))-(b*f(a)))/(f(b)-f(a)));
printf("%2d\t%4.6f\t%4.6f\t%4.6f\t%4.6f\t%4.6f\t%4.6f",i++,a,b,c,f(a),f(b),f(c)); if(f(c)==0){
break;
}else if(f(a)*f(c)<0){ b=c;
}else a=c;
error=fabs(c-cold);
if(i==1){
printf(" ------- \n");
}else printf(" %4.6f\n",error);

}while(error>0.00005);
printf(" Root is %4.6f \n",c);
return 0;
}

Output:

3.

#include<stdio.h>
#include<math.h>
float f(floatx)
{
return x*log10(x) -1.2;
}
float df (floatx)
{
return log10(x) + 0.43429;
}
int main()
{
int itr, maxmitr; float
h,x0,x1,allerr;
printf("\nEnter x0, allowed error and maximum iterations\n");
scanf("%f %f %d", &x0, &allerr, &maxmitr);
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf("AtIterationno.%3d,x=%9.6f\n",itr,x1); if
(fabs(h) <allerr)
{
printf("After %3d iterations, root = %8.6f\n", itr, x1); return 0;
}
x0=x1;
}
printf(" The required solution does not converge or iterations are insufficient\n"); return 1;
}

Output:

4.

#include<iostream>

#include<math.h> using

namespace std; int

main()

int i,j,k,n;

cout<<"\nEnter the no. of equations: ";

cin>>n;

float mat[n][n+1];

float res[n];

cout<<"\nEnter the elements of the augmented matrix: "; for(i=0;i<n;i++)

for(j=0;j<n+1;j++)

{
cin>>mat[i][j];

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

for(j=i+1;j<n;j++)

if(abs(mat[i][i]) < abs(mat[j][i]))

for(k=0;k<n+1;k++)

/* swapping mat[i][k] and mat[j][k] */

mat[i][k]=mat[i][k]+mat[j][k];

mat[j][k]=mat[i][k]-mat[j][k];

mat[i][k]=mat[i][k]-mat[j][k];

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)

float f=mat[j][i]/mat[i][i];

for(k=0;k<n+1;k++)

{
mat[j][k]=mat[j][k]-f*mat[i][k];

for(i=n-1;i>=0;i--)

res[i]=mat[i][n];

for(j=i+1;j<n;j++)

if(i!=j)

res[i]=res[i]-mat[i][j]*res[j];

res[i]=res[i]/mat[i][i];

cout<<"\nThe values of unknowns for the above equations=>\n";

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

cout<<res[i]<<"\n";

return 0;

Output:
5.

#include<iostream>
#include<cmath>
using namespacestd;

float mathFunc(float x)
{
return(x+(1/x)); //function1+1/x
}

float integrate(float a, float b, int n) {


floath,res=0.0,oddSum=0.0,evenSum=0.0,lim; int i;
h = (b-a)/n;
res = (mathFunc(a)+mathFunc(b)); lim =
n/2;

for(i = 1; i<=lim; i++)


oddSum += mathFunc(a+(2*i-1)*h); oddSum *= 4;

for(i = 1; i<lim; i++)


evenSum += mathFunc(a+(2*i)*h);
evenSum *= 2;
res += oddSum+evenSum; res
*= (h/3);
return res;
}
main()
{
float result, lowLim, upLim; int
interval;
cout << "Enter Lower Limit, Upper Limit and interval: "; cin
>>lowLim >>upLim >>interval;
result=integrate(lowLim,upLim,interval); cout
<< "The answer is: " << result;
}

Output:

6.

#include<iostream>
#include<math.h>
#define f(x) 1/(1+pow(x,2))

using namespace std; int


main()
{
float lower, upper, integration=0.0, stepSize, k; int i,
subInterval;
cout<<"Enter lower limit of integration: "; cin>>lower;
cout<<"Enter upper limit of integration: "; cin>>upper;
cout<<"Enter number of sub intervals: "; cin>>subInterval;
stepSize = (upper - lower)/subInterval;
integration = f(lower) + f(upper); for(i=1; i<=
subInterval-1; i++)
{
k=lower+i*stepSize;
if(i%3==0)
{
integration = integration + 2 * (f(k));
}
else
{
integration = integration + 3 * (f(k));
}

integration = integration * stepSize*3.0/8.0;


cout<< endl <<"Required value of integration is: "<< integration;

return 0;
}

Output:

7. Arithmetic individual mean series

#include<iostream>
namespace std; int
main()
{
int num, i, a[50], sum=0;
cout<<"\n Count of Elements ? \n";
cin>>num;
cout<<"\n Enter "<<num<<" Numbers : \n";
for(i=0; i<num; i++)
{
cin>>a[i]; sum=sum+a[i];
}
int arth_mean=sum/num;
cout<<"\n Arithmetic Mean of individual Series = "<<arth_mean; return 0;
}

Output:
7. calculate harmonic mean

#include <bits/stdc++.h>

using namespace std;

floatharmonic_mean(floatarr[],intsize){ float

sum= 0;

for (int i = 0; i < size; i++)

sum = sum + (float)1 / arr[i];

return (float)size/sum;

int main()

float arr[] = {2.0, 3.4, 5.3, 2.1};

int size = sizeof(arr) / sizeof(arr[0]);

cout<<"Harmonic mean is : "<<harmonic_mean(arr, size); return 0;

Output:
Harmonic mean is : 2.74163
8. Newtons interpolation

#include<iostream>

using namespace std; int

main()

int n,i,j;

float x[10],f[10],a,sum=0,mult;

cout<<"Noofsamplepoints?";

cin>>n;

cout<<"\nAll x with corresponding f(x) "; for(i=0;i<n;i++)

cin>>x[i]>>f[i];

cout<<"\nEnter x for calculation ";

cin>>a;

for(j=0;j<n-1;j++)

for(i=n-1;i>j;i--)

f[i]=(f[i]-f[i-1])/(x[i]-x[i-j-1]);

for(i=n-1;i>=0;i--)

mult=1;

for(j=0;j<i;j++)

mult*=(a-x[j]);
mult*=f[j]; sum+=mult;

cout<<"The result is: "<<sum;

// getch();

return 0;

Output:

9.
#include<stdio.h>

#include<math.h> int

main()

float x[10],y[15][15]; int

n,i,j; printf("Enter n :

");

scanf("%d",&n);

printf("X\tY\n");

for(i=0;i<n;i++){
scanf("%f %f",&x[i],&y[i][0]);

//forward difference table

for(j=1;j<n;j++)

for(i=0;i<(n-j);i++)

y[i][j] = y[i+1][j-1] - y[i][j-1]; printf("\n****Forward

Difference Table ****\n");

//display Forward Difference Table

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

printf("\t%.2f",x[i]);

for(j=0;j<(n-i);j++)

printf("\t%.2f",y[i][j]);

printf("\n");

for(j=1;j<n;j++) for(i=n-

1;i>(j-1);i--)

y[i][j] = y[i][j-1] - y[i-1][j-1];

printf("\n****Backward Difference Table ****\n");

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

printf("\t%.2f",x[i]); for(j=0;j<=i;j++)

printf("\t%.2f",y[i][j]);

printf("\n");

}
return 0;

Output:

9. Newton forward interpolation


#include<iostream>

#include<bits/stdc++.h> using

namespace std; int

factorial(intn)

int fact=1;

while(n)

fact=fact*n;

n--;

returnfact;

int main(){

float x[10],y[10],p[10],diff[10]; float

X,f,f2=0,u,h;

int i, j=1,n,k=1;

cout<<"Enter the number of observations: "; cin>>n;

cout<<"\nEnter the values of xi's and f(xi's)\n";

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

cout<<"x["<<i<<"]: ";

cin>>x[i];

cout<<"f["<<i<<"]: ";

cin>>y[i];

}
cout<<"\nx[i's]\tf[i's]\n"; for(i=1;

i<=n; i++){

cout<<x[i]<<"\t"<<y[i]<<"\n";

cout<<"\nEnter the value of x at which you want to interpolate: "; cin>>X;

int m=n;

h=x[2]-x[1];

u=(X-x[1])/h;

f=y[1];

cout<<fixed<<setprecision(2);

cout<<"Forward Difference table for the given set of points is\n"; do{

cout<<"diff.("<<j++<<") f(x)\n"; for(i=1;

i<n; i++){

p[i]=( ( y[i+1]-y[i] ) );

cout<<p[i]<<"\n";

y[i]=p[i];

diff[k++]=p[1];

n--;

cout<<"\n";

}while(n!=1);

float df=u;

float l=1;

for(int i=1; i<m; i++ ){


if(i>1)

df=df/factorial(i);

f2=f2+(df*diff[i]);

for(intj=0;j<=i;j++){

l=l*(u-j);

df=l;

f=f+f2;

cout<<"\nf("<<X<<") = "<<f;

return 0;

Output:

10.
#include <iostream>

#include <conio.h> using

namespace std;

class NewtonForward

public:

void askN();

void askX();

void askF();

void askXX();

void forwardTable();

void calcP();

void findH();

void findS();

private:

double XX, x[10] , f[10][10] , p[10],diff[5][5],P1;

int n; double

h,s;

};

void NewtonForward::askX()

cout << endl;

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


{

cout << "ENter X[" << i << "] : "; cin

>> x[i];

cout << endl;

void NewtonForward::askF()

for(int j = 0; j<n; j++ )

cout << "ENter F[" << j << "] : "; cin >>

f[0][j];

cout << endl;

void NewtonForward::askXX()

cout << "Enter X for which the value is to be found: "; cin >> XX;

void NewtonForward::forwardTable()

{
for(int i = 1; i < n; i++)

for(int j = 0; j< n-i;j++)

f[i][j] = (f[i-1][j+1]-f[i-1][j]);

cout << endl;

cout << "Sn\tXi\tf(Xi)\t";

for(int i = 0; i <n-1;i++)

cout << i+1 << " diff\t";

cout << endl;

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

cout <<i+1 <<"\t" << x[i]<< "\t";

for(int j = 0; j< n-i;j++)

cout << f[j][i] << "\t";

cout << endl;

void NewtonForward::findH()
{

h = x[1] - x[0];

void NewtonForward::findS()

s = (XX - x[0])/h;

void NewtonForward::calcP()

findH();

findS();

P1 = f[0][0];

for(int i = 1;i<n;i++)

double k = s; for(int j =

1; j<i;j++)

k *= (s-j) * 1/(j+1);

k*=f[i][0]; P1

+= k ;

cout <<endl << "The value of P" << n-1 << "(" << XX << "): " <<P1 << endl << endl;

}
void NewtonForward::askN()

cout<<"Enterthenumberofvalues:"; cin >>

n;

int main()

NewtonForward d1;

d1.askN();

d1.askX();

d1.askF();

d1.askXX();

d1.forwardTable();

d1.calcP();

OUTPUT

11.
#include <iostream.h>

#include<conio.h> class

B;

class A

private:

int a;

public:

void setdata(int x)

{ a = x;

friend void fun(A,B);

};

classB

private:

int b;

public:

void setdata(int y)

{ b = y;

friend void fun(A,B);

};

void fun(A a1,Bb1)

cout<<"SUM IS "<<a1.a+b1.b;
}

void main()

clrscr();

A aobj; B

bobj;

aobj.setdata(4);

bobj.setdata(5);

fun(aobj,bobj); getch();

12.
#include<iostream>

#include<math.h>

#define f(x) 1/(1+pow(x,2))

using namespace std;

int main()

float lower, upper, integration=0.0, stepSize, k; int i,

subInterval;

cout<<"Enter lower limit of integration: "; cin>>lower;

cout<<"Enter upper limit of integration: "; cin>>upper;

cout<<"Enter number of sub intervals: "; cin>>subInterval;


stepSize = (upper - lower)/subInterval;

integration = f(lower) + f(upper); for(i=1; i<=

subInterval-1; i++)

k=lower+i*stepSize;

if(i%3==0)

integration = integration + 2 * (f(k));

else

integration = integration + 3 * (f(k));

integration = integration * stepSize*3.0/8.0;

cout<< endl <<"Required value of integration is: "<< integration; return 0;

Output:
13.

#include<iostream>

#include<cmath> using

namespace std;

float mathFunc(float x)

return(x+(1/x)); //function 1 + 1/x

float integrate(float a, float b, int n)

float h, res = 0.0, oddSum = 0.0, evenSum = 0.0, lim; int i;

h = (b-a)/n;

res = (mathFunc(a)+mathFunc(b)); lim

= n/2;

for(i = 1; i<=lim; i++)

oddSum += mathFunc(a+(2*i-1)*h);

oddSum *= 4;

for(i = 1; i<lim; i++)

evenSum += mathFunc(a+(2*i)*h);

evenSum *= 2;

res += oddSum+evenSum; res

*= (h/3);
return res;

main()

float result, lowLim, upLim; int

interval;

cout << "Enter Lower Limit, Upper Limit and interval: "; cin

>>lowLim >>upLim >>interval;

result=integrate(lowLim,upLim,interval); cout

<< "The answer is: " << result;

Output:

14. Program trapezoidal

#include<iostream>
#include<math.h>

#define f(x) 1/(1+pow(x,2))

using namespace std;

int main(){ float lw, up, integral_value=0.0, stepSize, k; int i,

subInterval;

cout<<"Enter lower limit of integral: ";

cin>>lw;

cout<<"Enter upper limit of integral: ";

cin>>up;

cout<<"Enter number of sub intervals: "; cin>>subInterval;

stepSize = (up - lw)/subInterval;

integral_value = f(lw) + f(up); for(i=1;

i<= subInterval-1; i++)

{k = lw + i*stepSize;

integral_value = integral_value + 2 * (f(k)); }

integral_value = integral_value * stepSize/2;

cout<< endl<<"VALUE OF integral is: "<< integral_value; return 0;}

Output:
15. Program simpson one third by 8
#include<iostream>

#include<math.h>

#define f(x) 1/(1+pow(x,2))

using namespace std;

int main()

float lower, upper, integration=0.0, stepSize, k; int i,

subInterval;

cout<<"Enter lower limit of integration: "; cin>>lower;

cout<<"Enter upper limit of integration: "; cin>>upper;

cout<<"Enter number of sub intervals: "; cin>>subInterval;

stepSize = (upper - lower)/subInterval;

integration = f(lower) + f(upper); for(i=1; i<=

subInterval-1; i++)

k = lower + i*stepSize;

if(i%3==0)

integration = integration + 2 * (f(k));

else
{

integration = integration + 3 * (f(k));

integration = integration * stepSize*3.0/8.0;

cout<< endl <<"Required value of integration is: "<< integration;

return 0;

Output:

You might also like