0% found this document useful (0 votes)
3 views

NM LAB EXPERIMENTS

The document contains C++ implementations of various numerical methods including Bisection, Regula Falsi, Newton Raphson, Secant Method, Gauss Elimination, Gauss Jordan, Jacobi, Gauss Seidel, Trapezoidal, Simpson, Lagrange's Interpolation, and Newton's Divided Difference. Each method is presented with code snippets that allow users to input parameters and compute results for solving equations or performing integrations. The document serves as a practical guide for implementing these numerical techniques in programming.
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)
3 views

NM LAB EXPERIMENTS

The document contains C++ implementations of various numerical methods including Bisection, Regula Falsi, Newton Raphson, Secant Method, Gauss Elimination, Gauss Jordan, Jacobi, Gauss Seidel, Trapezoidal, Simpson, Lagrange's Interpolation, and Newton's Divided Difference. Each method is presented with code snippets that allow users to input parameters and compute results for solving equations or performing integrations. The document serves as a practical guide for implementing these numerical techniques in programming.
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/ 17

NM LAB EXPERIMENTS

1. Bisection Method

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
float f(float x)
{
return (x*log10(x)-1.2);
}
void bisect(float *x, float a, float b,int *itr)
{
*x=(a+b)/2;
++(*itr);
cout<<"Iteration Number:"<<setw(3)<<*itr<<"
x="<<setw(7)<<setprecision(5)<<*x<<endl;
}
int main()
{
int itr=0,maxitr;
float x,a,b,aerr,x1;
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Allowed Error:";
cin>>aerr;
cout<<"Maximun Iteration:";
cin>>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<<endl;
return 0;
}
x=x1;
}
while (itr<maxitr);
cout<<"Solutions does not converge,"<<"iterations not sufficient"<<endl;
return 1;
}

2. Regula Falsi

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
float f(float x)
{
return (x*log10(x)-1.2);
}
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 value of x0:";
cin>>x0;
cout<<"Enter the value of x1:";
cin>>x1;
cout<<"Correct upto Decimal:";
cin>>aerr;
cout<<"maximum Itertion:";
cin>>maxitr;
regula(&x2,x0,x1,f(x0),f(x1),&itr);
cout<<fixed;
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<<endl;
return 0;
}
x2=x3;
}
while(itr<maxitr);
cout<<"Solutions does not converge, "<<"iterations not sufficient"<<endl;
return 1;
}

3. Newton Raphson

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

#define f(x) 3*x - cos(x) -1


#define g(x) 3 + sin(x)

using namespace std;

int main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
cout<< setprecision(6)<< fixed;
/* Inputs */
cout<<"Enter initial guess: ";
cin>>x0;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;
/* Implementing Newton Raphson Method */
cout<< endl<<"*********************"<< endl;
cout<<"Newton Raphson Method"<< endl;
cout<<"*********************"<< endl;
do
{
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
{
cout<<"Mathematical Error.";
exit(0);
}

x1 = x0 - f0/g0;

cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x1<<" and f(x) = "<<


setw(10)<< f(x1)<< endl;
x0 = x1;

step = step+1;

if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}

f1 = f(x1);

}while(fabs(f1)>e);

cout<< endl<<"Root is: "<< x1;


return 0;
}
4. Secant Method
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define f(x) exp(-x)-x
using namespace std;
int main()
{
float x0,x1,x2,f0,f1,f2,aerr;
int itr=1,maxitr;
cout<<"Enter Value of X0: ";
cin>>x0;
cout<<"Enter Value of X1: ";
cin>>x1;
cout<<"Correct Upto Decimal: ";
cin>>aerr;
cout<<"Enter maximum iteration: ";
cin>>maxitr;
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
cout<<"Mathematical Error.";
exit(0);
}
x2 = x1 - (x1 - x0) * f1/(f1-f0);
f2=f(x2);
cout<<"Iteration-"<< itr <<":\t x = "<< setw(10)<<x2<<" and f(x) = "<<
setw(10)<< f(x2)<< endl;
x0=x1;
f0=f1;
x1=x2;
f1=f2;
itr++;
if(itr>maxitr)
{
cout<<"Not Convergent.";
exit(0);
}
}
while (fabs(f2)>aerr)
cout<< endl<< "Root is: "<<x2;return 0;}
5. Gauss elimination
/* Gauss elimination method */
#include <iostream>
#include <iomanip>
#include <math.h>
#define N 3
using namespace std;
int main()
{
float a[N][N+1],x[N],t,s;
int i,j,k;
cout << "Enter the elements of the augmented matrix row wise" << 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;
}
/* now printing the
upper triangular matrix */
cout << "The upper triangular matrix is:-" << endl;
for (i=0;i<N;i++)
{
for (j=0;j<N+1;j++)
cout << setw(8) << setprecision(4) << a[i][j];
cout << endl;
}
/* now performing back substitution */
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];
}
/* now printing the results */
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;}
6. Gauss Jordan

/* Gauss jordan method */


#include <iostream>
#include <iomanip>
#define N 3
using namespace std;
int main()
{
float a[N][N+1],t;
int i,j,k;
cout << "Enter the elements of the"<< " augmented matrix row wise" << endl;
for (i=0;i<N;i++)
for (j=0;j<N+1;j++)
cin >> a[i][j];
/* now calculating the values
of x1,x2,....,xN */
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;
}
/* now printing the diagonal matrix */
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;
}
/* now printing the results */
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] << endl;
return 0;
}
7. Jacobi

#include<iostream>
#include<iomanip>
#include<math.h>
/* In this example we are solving
8x - 3y + 2z = 20
4x + 11y - z = 33
6x + 3y - 12z = 35
*/
/* Arranging given system of linear
equations in diagonally dominant
form:
8x - 3y + 2z = 20
4x + 11y - z = 33
6x + 3y - 12z = 35
*/
/* Equations:
x = (20+3y-2z)/8
y = (33-4x+z)/11
z = (35-6x-3y)/12
*/
/* Defining function */
#define f1(x,y,z) (20+3*y-2*z)/8
#define f2(x,y,z) (33-4*x+z)/11
#define f3(x,y,z) (35-6*x-3*y)/12
using namespace std;
int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int step=1;
cout<< setprecision(4)<< fixed;
cout<<"Correct Up To Decimal: ";
cin>>e;
cout<< endl<<"Count\tx\ty\tz"<< endl;
do
{
x1 = f1(x0,y0,z0);
y1 = f2(x0,y0,z0);
z1 = f3(x0,y0,z0);
cout<< step<<"\t"<< x1<<"\t"<< y1<<"\t"<< z1<< endl;
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
step++;
x0 = x1;
y0 = y1;
z0 = z1;
}
while(e1>e && e2>e && e3>e);
cout<< endl<<"Solution: x = "<< x1<<", y = "<< y1<<" and z = "<< z1;
return 0;
}

8. Gauss Seidel
/* Gauss Seidal method */
#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;
/* first initializing the array x */
for (i=0;i<N;i++) x[i]=0;
cout << "Enter the elements of the"<< " augmented matrix row wise" << endl;
for (i=0;i<N;i++)
for (j=0;j<N+1;j++)
cin >> a[i][j];
cout<<"Correct Up To Decimal: ";
cin >> aerr;
cout<< "Maximum iterations: ";
cin>> maxitr;
cout << fixed;
cout << "Iteration" << setw(6) << "x[1]"<< setw(11) << "x[2]"<< setw(11) << "x[3]"
<< endl;
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 << "Converges 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 << "Solution does not converge,"<< "iterations not sufficient" << endl;
return 1;
}

9. Trapezoidal

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

/* Define function here */


#define f(x) 1/(1+(x*x))

using namespace std;


int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;

/* Input */
cout<<"Enter lower limit of integration: ";
cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;

/* Calculation */

/* Finding step size */


stepSize = (upper - lower)/subInterval;

/* Finding Integration Value */


integration = f(lower) + f(upper);

for(i=1; i<= subInterval-1; i++)


{
k = lower + i*stepSize;
integration = integration + 2 * (f(k));
}

integration = integration * stepSize/2;

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

return 0;
}

10. Simpson

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

/* Define function here */


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

using namespace std;


int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;

/* Input */
cout<<"Enter lower limit of integration: ";
cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;

/* Calculation */

/* Finding step size */


stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);

for(i=1; i<= subInterval-1; i++)


{
k = lower + i*stepSize;

if(i%2==0)
{
integration = integration + 2 * (f(k));
}
else
{
integration = integration + 4 * (f(k));
}

integration = integration * stepSize/3;

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

return 0;
}

11. Lagrange’s Interpolation

#include<iostream>
using namespace std;
int main()
{
float x[100],y[100],xp,yp=0,p;
int i,j,n;
cout<<"Enter number of data: ";
cin>>n;
cout<<"Enter Data="<<endl;
for(i=1;i<=n;i++)
{
cout<<"x["<<i<<"]=";
cin>>x[i];
cout<<"y["<<i<<"]=";
cin>>y[i];
}
cout<<"Enter Interpolation point:";
cin>>xp;
for(i=1;i<=n;i++)
{
p=1;
for(j=1;j<=n;j++)
{
if(i!=j)
{
p*=(xp-x[j])/(x[i]-x[j]);
}
}
yp=yp+p*y[i];
}
cout<<endl<<"Interpolated value at "<<xp<<" is "<<yp;
return 0;
}

12. Newton’s Divided Diff.

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
float x[10],y[10],p[10];
float k,f,n,f1=1,f2=0;
int i,j=1;

cout<<"Enter the no. of observations\n";


cin>>n;

cout<<"Enter the different values of x\n";

for(i=1;i<=n;i++)
{
cin>>x[i];
}

cout<<"Enter the corresponding values of y\n";

for(i=1;i<=n;i++)
{
cin>>y[i];
}

f=y[1];

cout<<"Enter the value of 'k' for f(k) evaluation\n";


cin>>k;

do
{
for(i=1;i<=n-1;i++)
{
p[i]=((y[i+1]-y[i])/(x[i+j]-x[i]));
y[i]=p[i];
}
for(i=1;i<=j;i++)
{
f1 *= (k-x[i]);
}

f2 += (y[1]*f1);
f1=1;
n--;
j++;
}while(n!=1);

f += f2;
cout<<"f("<<k<<")="<<f;

return 0;
}

13. Newton’s forward

#include <iostream>
#include<iomanip>
using namespace std;

// calculating u mentioned in the formula


float u_cal(float u, int n)
{
float temp = u;
for (int i = 1; i < n; i++)
temp = temp * (u - i);
return temp;
}

// calculating factorial of given number n


int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}

int main()
{
cout<<"Enter the number of values given: ";
// Number of values given
int n;
cin>>n;
float x[n];
// y[][] is used for difference table
// with y[][0] used for input
float y[n][n];
for(int i=0;i<n;i++)
{
cout<<"X["<<(i+1)<<"]: ";
cin>>x[i];
cout<<"Y["<<(i+1)<<"]: ";
cin>>y[i][0];
}
// Calculating the forward difference table
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++)
y[j][i] = y[j + 1][i - 1] - y[j][i - 1];
}

// Displaying the forward difference table


for (int i = 0; i < n; i++) {
cout << setw(4) << x[i]
<< "\t";
for (int j = 0; j < n - i; j++)
cout << setw(4) << y[i][j]
<< "\t";
cout << endl;
}
// Value to interpolate at
cout<<"Enter value to interpolate at: ";
float value;14
cin>>value;

// initializing u and sum


float sum = y[0][0];
float u = (value - x[0]) / (x[1] - x[0]);
for (int i = 1; i < n; i++) {
sum = sum + (u_cal(u, i) * y[0][i]) /
fact(i);
}

cout << "\n Value at " << value << " is "
<< sum << endl;
return 0;
}

14. Newton’s Backward

#include<iostream>
#include<iomanip>
using namespace std;
#define MAX 100
#define ORDER 4
int main()
{
float ax[MAX+1],ay[MAX+1],diff[MAX+1][ORDER+1];
float nr=1,dr=1,x,p,h,yp;
int n,i,j,k;
cout<<"Enter the value of n:";
cin>>n;
cout<<"Enter the value in form x y:-"<<endl;
for(i=1;i<=n;i++)
{
cin>>ax[i]>>ay[i];
}
cout<<"Enter the value of 'x' for which 'y' is to be determined=";
cin>>x;
h=ax[2]-ax[1];
for(i=n;i>=1;i--)
{
diff[i][1]=ay[i]-ay[i-1];
}
for(j=2;j<=ORDER;j++)
{
for(i=n;i>j;i--)
{
diff[i][j]=diff[i][j-1]-diff[i-1][j-1];
}
}
i=n;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=ORDER;k++)
{
nr*=p+k-1;
dr*=k;
yp+=(nr/dr)*diff[i][k];
}
cout<<"When x = "<<setprecision(3)<<x<<" y = "<<setprecision(2)<<yp<<endl;
return 0;}

You might also like