NM Complete
NM Complete
Table of Contents
Sr. Program Language Date Page Sign.
No Used No.
1. Bisection 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>
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; }
#include <iostream>
#include<iomanip>
#include<math.h>
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;
}
Output:
3. NEWTON RAPHSON METHOD
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
Program:
#include<iostream>
#include<iomanip>
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
Program:
#include<iostream>
#include<iomanip>
#include<math.h>
#define N 4
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:
Program:
#include<iostream>
#include<iomanip>
#include<math.h>
#define N 3
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;
}
}
Output:
7. GAUSS-JORDAN METHOD
Program:
#include<iostream>
#include<iomanip>
#define N 4
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
Program:
//*simpson's rule
#include<iostream>
#include<iomanip>
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
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:
/transformation of A to a “/ a = exp(A);
Displaying value of a and b “/ cout<<”Values are: a=”<<<<” and b=”<<b;
Return(0);
}
Output: