0% found this document useful (0 votes)
25 views21 pages

NM Complete

This document contains 10 programs written in C++ to solve various numerical methods problems commonly found in chemical engineering. The programs included solutions for root finding methods like bisection, Regula Falsi, and Newton Raphson. It also included programs for numerical integration like Simpson's rule, numerical solutions to ODEs using Euler's method and Runge-Kutta method, and solving systems of linear equations using Gauss elimination, Gauss-Seidel, and Gauss-Jordan methods. The final program performed curve fitting using the least squares method.

Uploaded by

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

NM Complete

This document contains 10 programs written in C++ to solve various numerical methods problems commonly found in chemical engineering. The programs included solutions for root finding methods like bisection, Regula Falsi, and Newton Raphson. It also included programs for numerical integration like Simpson's rule, numerical solutions to ODEs using Euler's method and Runge-Kutta method, and solving systems of linear equations using Gauss elimination, Gauss-Seidel, and Gauss-Jordan methods. The final program performed curve fitting using the least squares method.

Uploaded by

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

Deenbhandu Chhotu Ram University of Science and Technology

(Established Under Haryana Legislature Act No. 29 of 2006)


Murthal – 131039, Sonipat (Haryana)
www.dcrustm.ac.in

Basant Roll No.: 22001005904


B. Tech. Semester - IV (Chemical Engineering)
CHE210C: Numerical Methods in Chemical Engineering Lab
Programs of C++

Table of Contents
Sr. Program Language Date Page Sign.
No Used No.
1. Bisection method C++

2. Regula Falsi method C++

3. Newton’s Raphson method C++


4. Runge-Kutta method C++

5. Gauss-Elimination method C++

6. Gauss-Seidal iteration method C++

7. Gauss-Jordan method C++

8. Simpson’s rule C++

9. Euler’s method C++

10. Curve Fitting method C++

1. BISECTION METHOD
Objective: To find the roots of non-linear equation using Bisection method
Language Used: C++ Program:

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

using namespace std;

float f(float x)
{
return (x*x*x -4*x -9);
}
void bisect(float *x, float a, float b, int *itr)
{
*x=(a+b)/2; ++(*itr);
cout<<"Iteration no."<<setw(7)<<setprecision(5)<<*x<<endl; }

int main() { int


itr=0, maxitr;
float x,a,b,aerr,x1;
cout<<"Enter the values of a,b"<<"allowed errror , maximum iterations "<<endl;
cin>>a>>b>>aerr>>maxitr;
cout<<fixed; bisect(&x,
a ,b ,&itr);
do{ if(f(a)*f(x)<0) b=x;
else a=x;
bisect(&x1,a,b,&itr);
if(fabs(x1-x)<aerr)
{
cout<<"After"<<itr<<"iterations,root"<<"="<<setw(6)<<setprecision(4)<<x1<<end
l;
return 0;
} x=x1;
}while(itr<maxitr);
cout<<"Solution does not converge , "<<"iterations not sufficient "<<endl;
return 1;
}
Output:

2.REGULA FALSI METHOD

Objective: To Find Roots Of Non-Linear Equation Using Regula Falsi Method


Language Used: C++ Program:

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

using namespace std;

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); cout<<"Iteration
no."<<setw(3)<<*itr<<"x
="<<setw(7)<<setprecision(5)<<*x<<endl;
}

int main() { int itr=0,


maxitr; float
x0,x1,x2,x3,aerr;
cout<<"Enter the values for x0,x1,"<<"allowed error, maximum iterations ";"endl";
cout<<"\n";
cin>>x0>>x1>>aerr>>maxitr;
regula(&x2,x0,x1,f(x0),f(x1),&itr);
cout<<"fixed";"endl"; cout<<"\n";
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)<aerr)
{
cout<<"After"<<itr<<"Iterations,"<<"root ="<<setw(6)<<setprecision(4)<<x3;"\n";
cout<<"\n"; return 0; } x2=x3;
}while(itr<maxitr);
cout<<"Solution doesnt converge,"<<"Iterations not sufficient";"endl";
cout<<"\n"; return 1;
}

Output:
3. NEWTON RAPHSON METHOD

Objective: To find roots of non-linear equation using Newton’s method


Language Used: C++ Program:
#include<iostream>
#include<iomanip>
#include<math.h>

using namespace std;

float f(float x)
{
return x*log10(x)-1.2;
}
float df(float x)
{
return log10(x) + 0.43429;
}
int main() {
int itr,maxitr;

float h,x0,x1,aerr;
cout<<"Enter x0,allowed error,"<<"maximum iterations"<<endl ;
cin>>x0>>aerr>>maxitr; cout<<fixed;
for (itr=1;itr<=maxitr;itr++)
{ h=f(x0)/df(x0);
x1=x0-h;
cout<<"iteration no."<<setw(9)<<setprecision(6)<<x1<<endl;
if(fabs(h)<aerr)
{
cout<<"After"<<setw(3)<<itr<<"
iterations,root="<<setw(8)<<setprecision(6)<<x1<<endl; return
0;

}
x0=x1;

}
cout<<"Iterations not sufficient,"<<"Solution doesnt converge"<<endl;
return 1;
}

Output:
4.RUNGE-KUTTA METHOD

Objective: To find numerical solution of ordinary differential equations by


Runge-Kutta method
Language Used: C++
Compiler Used: Dev C++

Program:

#include<iostream>
#include<iomanip>

using namespace std;

float f(float x , float y)


{
return x+y*y;
}

int main() {
float x0,y0,h ,xn,x,y,k1,k2,k3,k4,k;
cout<<"Enter the values of x0,y0"<<"h,xn"<<endl;
cin>>x0>>y0>>h>>xn;
x=x0;y=y0; cout<<fixed;
while(1) { if(x==xn )
break; k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/20);
k4=h*(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x+=h;y+=k;
cout<<"when x="<<setprecision(4)<<setw(8)<<x<<"y="<<setw(8)<<y<<endl; }
}

Output:
5.GAUSS-ELIMINATION METHOD

Objective: To solve system of linear equations using Gauss-Elimination


method
Language Used: C++
Compiler Used: Dev C++

Program:

#include<iostream>
#include<iomanip>
#include<math.h>
#define N 4

using namespace std;

int main() {
float a [N][N+1],x[N],t,s; int
i,j,k;
cout<<"Enter the elements of the "<<"Augumented matrix rowwise"<<endl;
cout<<fixed; for(i=0;i<N;i++) for(j=0;j<N+1;j++) cin>>a[i][j];
for(j=0;j<N-1;j++) for(i=j+1;i<N;i++)
{ t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++)
a[i][k] -=a[j][k]*t;
}
cout<<"The upper triangular matrix ""is :-"<<endl; for
(i=0;i<N;i++)
cout<<setw(8)<<setprecision(4)<<a[i][j];
cout<<endl; for(i=N-1;i>=0;i--)
{ s=0; for(j=i+1;j<N;j+
+) s += a[i][j]*x[j];
x[i]= (a[i][N]-s)/a[i][i];
}
cout<<"The solution is :-"<<endl; for
(i=0;i<N;i++)
cout<<"x["<<setw(3)<<i+1<<"]="<<setw(7)<<setprecision(4)<<x[i]<<endl;
return 0;
}

Output:

6. GAUSS-SEIDAL ITERATION METHOD

Objective: To solve system of linear equations using Gauss-Seidal iteration


method
Language Used: C++
Compiler Used: Dev C++

Program:

#include<iostream>
#include<iomanip>
#include<math.h>
#define N 3

using namespace std;

int main ()
{
float a[N][N+1],x[N],aerr,maxerr,t,s,err;
int i,j,itr,maxitr; for
(i=0;i<N;i++) x[i]=0;
cout<<"Enter the elements of the "<<"Augumented matrix rowwise "<<endl;
for(i=0;i<N;i++) for (j=0;j<N+1;j++) cin>>a[i][j];
cout<<"Enter the allowed Error ,"<<"maximum iterations "<<endl;
cin>>aerr>>maxitr;
cout<<fixed;

cout<<"Iteration"<<setw(6)<<"x[1]"<<setw(11)<<"x[2]"<<setw(11)<<"x[3]"<<en
dl;
for(itr=1;itr<=maxitr;itr++)
{
maxerr=0;
for(i=0;i<N;i++)
{s=0;
for (j=0;j<N;j++) if(j!
=i) s+=a[i][j]*x[j];

t=(a[i][N]-s)/a[i][i];
err = fabs(x[i]-t);
if(err>maxerr)maxerr=err;
x[i]=t;
}
cout<<setw(5)<<itr; for(i=0;i<N;i++)
cout<<setw(11)<<setprecision(4)<<x[i];
cout<<endl; if(maxerr<aerr)
{
cout<<"Converge in"<<setw(3)<<itr<<"iterations"<<endl; for(i=0;i<N;i++)
cout<<"x["<<setw(3)<<i+1<<"]="
<<setw(7)<<setprecision(4)<<x[i]<<endl; return 0;
}
}

cout<<"Solutions does not converge "<<"iterations not sufficeient "<<endl;


return 1;
}

Output:
7. GAUSS-JORDAN METHOD

Objective: To solve system of linear equations using Gauss-Jordan method


Language Used: C++
Compiler Used: Dev C++

Program:

#include<iostream>
#include<iomanip>
#define N 4

using namespace std;

int main ()
{
float a[N][N+1],t;
int i,j,k;
cout<<"Enter the elements of the "<<"Augumented matrix rowwise "<<endl;
for (i=0;i<N;i++) for (j=0;j<N+1;j++) cin>>a[i][j];
cout<<fixed;
for (j=0;j<N;j++)
for(i=0;i<N;i++) if(i!=j)
{
t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++) a[i][k] -
=a[j][k]*t;

}
cout<<"the diagonal matrix is :-"<<endl; for(i=0;i<N;i++)

{
for (j=0;j<N+1;j++)
cout<<setw(9)<<setprecision(4)<<a[i][j];
cout<<endl;
}
cout<<"The solution is :- "<<endl;
for (i=0;i<N;i++)
cout<<"x["<<setw(3)<<i+1<<"]="<<setw(7)<<setprecision(4)<<a[i][N]/a[i]
[i]<<en dl; return 0;
}

Output:

8. SIMPSON’S RULE

Objective: To integrate numerically using Simpson’s rule


Language Used: C++
Compiler Used: Dev C++

Program:

//*simpson's rule
#include<iostream>
#include<iomanip>

using namespace std;

float y(float x)
{
return 1/(1+x*x);
}

int main()
{ float
x0,xn,h,s; int
i,n;
cout<<"Enter x0,xn,no. of subintervals"<<endl;
cin>>x0>>xn>>n; cout<<fixed;
h=(xn-x0)/n; s=y(x0)+y(xn)
+4*y(x0+h); for(i=3;i<=n-1;i+=2) s
+= 4*y(x0+i*h)+2*y(x0+(i-1)*h);
cout<<"value of integral is "<<setw(6)<<setprecision(4)<<(h/3)*s<<endl;
return 0;
}

Output:
9. EULER’S METHOD

Objective: To find numerical solution of ordinary differential equations by


Euler’s method

Program Language : C++ Program:


#include<stdio.h>
#include<conio.h>
#define f(x,y) x+y
Int main()
{ float x0, y0, xn, h, yn, slope; int I, n;
printf(“Enter Initial Condition\n”); scanf(“%f”,
&x0); scanf(“%f”, &y0); printf(“Enter calculation
point xn = “); printf(“Enter number of steps: “);
scanf(“%d”, &n)
Printf(“x0 = “);

Printf(“y0 = “);
Scanf(“%f”, &xn);
/* Calculating step size (h) */
H = (xn-x0)/n;
Printf(“\nx0\ty0\tslope\tyn\n”); printf(“-- -----------\n”);
For(i=0; I < n; i++)
Slope = f(x0, y0);
{ yn=y0+ h slope; printf(“%.4f\t%.4f\t%0.4f\t%.4f\n”,x0,y0,slope,yn);
y0 = yn; x0 = x0+h;

}
/* Displaying result */ printf(“\nValue of y at x = %0.2f is %0.3f”,xn, yn);
Getch(); return 0;
}
Output:

10. CURVE FITTING


Objective: Curve fitting by least square approximations
Program Language: C++ Program:
#include<iostream> #include<math.h>
#define S 50
Using namespace std;
Int main()
Int n, I;
Float x[S], y[S], sumX=0, sumX2-0, sumY-0, sumXY-0, a, b, A; Input/

Cout<<”How many data points?”<<endl;


Cin>> n;
For(i=1;i<=n;i++)
Cout << “x”<<<<<”]=”; cin>> x[i];

Cout << “y[“<<<<<”]=”; Cin


>> y[i];
/*Calculating Required Sum*/ for(i=1;i<=n;i++) sumXsumX+
log(x[i]); sumX2 = sumX2 + log(x[1]) log(x[i]); sumY= sumY+ log(y[i]);
sumXY = sumXY+ log(x[i])”log(y[i]);
Calculating A and b
B= (n sumXY-sumX*sumY)/(n*sumX2-sumX*sumX); A = (sumY-b sumX)/n;

/transformation of A to a “/ a = exp(A);
Displaying value of a and b “/ cout<<”Values are: a=”<<<<” and b=”<<b;
Return(0);
}
Output:

You might also like