NM LAB EXPERIMENTS
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>
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;
step = step+1;
if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
f1 = f(x1);
}while(fabs(f1)>e);
#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>
/* 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 */
return 0;
}
10. Simpson
#include<iostream>
#include<math.h>
/* 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 */
if(i%2==0)
{
integration = integration + 2 * (f(k));
}
else
{
integration = integration + 4 * (f(k));
}
return 0;
}
#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;
}
#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;
for(i=1;i<=n;i++)
{
cin>>x[i];
}
for(i=1;i<=n;i++)
{
cin>>y[i];
}
f=y[1];
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;
}
#include <iostream>
#include<iomanip>
using namespace std;
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];
}
cout << "\n Value at " << value << " is "
<< sum << endl;
return 0;
}
#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;}