Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views17 pages

C++progamm Sem

The document contains multiple C++ programs that implement various numerical methods including Gauss Elimination, Jacobi Iterative, Bisection, Gauss Jordan, Simple Iterative, Newton Raphson, and curve fitting techniques. Each method is designed to solve mathematical problems such as systems of equations, root finding, and curve fitting using different algorithms. Sample outputs are provided for each method to demonstrate their functionality and results.

Uploaded by

kirubajea.8
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)
9 views17 pages

C++progamm Sem

The document contains multiple C++ programs that implement various numerical methods including Gauss Elimination, Jacobi Iterative, Bisection, Gauss Jordan, Simple Iterative, Newton Raphson, and curve fitting techniques. Each method is designed to solve mathematical problems such as systems of equations, root finding, and curve fitting using different algorithms. Sample outputs are provided for each method to demonstrate their functionality and results.

Uploaded by

kirubajea.8
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/ 17

#include<iostream.

h> Gauss Elimination Method


#include<conio.h>
#include<math.h>
int main()
{
long int i,j,k,n;
clrscr();
double x[10],a[10][10],c[10];
cout<< “Enter the value of n:”;
cin>>n;
cout<< “Enter the right hand side constants:”<<endl;
for(i=0;i<n;i++)
cin>>c[i];
cout<<”Enter the coefficient row wise:”<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
for(k=0;k<n-1;k++)
{
for(i=k+1;i<n;i++)
{
for(j=k+1;j<n;j++)
a[i][j]=a[i][j]-a[i][k]/a[k][k]*a[k][j];
c[i]=c[i]-(a[i][k]/a[k][k])*c[k];
}
}
x[n-1]=c[n-1]/a[n-1][n-1];
cout<<”the solution is :”<<endl;
cout<< “x[“<<n-1<<”]=”<<x[n-1]<<endl;
for(k=0;k<n-1;k++)
{
i=n-k-2;
for(j=i+1;j<n;j++)
c[i]=c[i]-(a[i][j]*x[j]);
x[i]=c[i]/a[i][j];
cout<<”x[“<<i<<”]=”<<x[i]<<endl;
}
return 0;
}

Output:

Enter the value of n:


2
Enter the RHS constants:
4 -8
Enter the coefficients row wise:
3 -2
6 4

The solution is:


X[0]=0
X[1]=-2
#include<iostream.h> Jacobi Iterative Method
#include<conio.h>
#include<iomanip.h>
#define f1(x,y,z) (58-2*y-3*z)/45
#define f2(x,y,z) (47+3*x-2*z)/22
#define f3(x,y,z) (67-5*x-1*y)/20
int main()
{
clrscr();
float x0=0,y0=0,z0=0,x1,y1,z1,e1,e2,e3,e;
int step=1;
cout<<setprecision(6)<<setiosflags(ios::showpoint|
ios::fixed);
cout<< “enter tolerable error:”;
cin>>e;
cout<<endl<< “count \tx\t\ty\t\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;
}
output:

enter tolerable error: 0.0001

count x y z
1 1.288889 2.136364 3.350000
2 0.970606 2.007576 2.920959
3 1.004933 2.003177 3.006970
4 0.999394 2.000039 2.998608
5 1.000091 2.000044 3.000149

solution: x = 1.000091, y=2.000044 and z=3.000149


#include<iostream.h> Bisection Method
#include<conio.h>
#include<iomanip.h>
#include<math.h>
#define f(x) x*x*x-2*x-5
int main()
{
float x0,x1,x,f0,f1,f,e;
int step=1;
cout<<setprecision(6)<<setiosflags(ios::showpoint)|(ios::fixed);
up:
cout<< “enter the first guess:”;
cin>>x0;
cout<< “ enter the second guess:”;
cin>>x1;
cout<<”enter the tolerable errors:”;
cin>>e;
f0=f(x0);
f1=f(x1);
if(f0*f1>0.0)
{
cout<< “incorrect initial guesses”<<endl;
goto up;
}
cout<<endl<< “**************”<<endl;
cout<< “bisection method”<<endl;
cout<< “**************”<<endl;
do
{
x=(x0+x1)/2;
f=f(x);
cout<< “iteration –“<<step<< “:\t x=”<<setw(10)<<x
<<” and f(x)=”<<setw(10)<<f(x)<<endl;
if(f0*f<0)
{
x1=x;
}
else
{
x0=x;
}
step=step+1;
}
while(fabs(f)>e);
cout<<end<< “root is: “<<x<<endl;
return 0;
}

output:
enter the first guess: 2
enter the second guess: 3
enter the tolerable error: 0.0001
bisection method
iteration -1 x=2.500000 and f(x)=5.625000
iteration -2 x=2.250000 and f(x)=1.890625
iteration -3 x=2.125000 and f(x)=0.345703
iteration -4 x=2.062500 and f(x)=-0.351318
iteration -5 x=2.093750 and f(x)=-0.008942
iteration -6 x=2.109375 and f(x)= 0.166936
iteration -7 x=2.101563 and f(x)= 0.078562
iteration -8 x=2.097656 and f(x)= 0.034714
iteration -9 x=2.095703 and f(x)= 0.012862
iteration -10 x=2.094727 and f(x)= 0.001954
iteration -11 x=2.094238 and f(x)=-0.003495
iteration -12 x=2.094482 and f(x)= -0.000771
iteration -13 x=2.094604 and f(x)= 0.000592
iteration -14 x=2.094543 and f(x)=-0.000090

roots is : 2.094543
#include<iostream.h> Gauss Jordan Method
#include<conio.h>
#include<math.h>
int main()
{
float i,j,k,n;
float x[10], a[10][10];
cout<<”Enter the value of n:”<<endl;
cin>>n;
cout<<”Enter the RHS constants:”<<endl;
for(i=0;i<n;i++)
cin>>a[i][n];
cout<<”Enter the coefficients row wise:”<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(i!=k)
{
for(j=k+1;j<n+1;j++)
a[i][j]=a[i][j]-(a[i][k]/a[k][k])*a[k][j];
}
}
cout<<”the solution is :”<<endl;
for(i=0;i<n;i++)
{
x[i]=(a[i][n]/a[i][i]);
cout<<”x[“<<i<<”]=”<<x[i]<<endl;
}
}
return 0;
}

Output:
Enter the value of n:
2
Enter the RHS constants:
4 -8
Enter the coefficients row wise:
3 -2
6 4

The Solution is
X[0]= 0
X[1]=-2
#include<iostream.h> Simple Iterative Method
#include<conio.h>
#include<math.h>
#include<iomanip.h>
#include<stdio.h>
#define f(x) (x*x*x)+(x*x)-1
#define g(x) 1/sqrt(x+1)
int main()
{
clrscr();
int step=1,N;
float x0,x1,e;
cout<<setprecision(6)
<<setiosflags(ios::showpoint|ios::fixed);
cout<<”Enter the initial guess:”;
cin>>x0;
cout<<”Enter the tolerable error:”;
cin>>e;
cout<<”Enter the maximum iteration:”;
cin>>N;
cout<<endl<<”***********************”;
cout<<endl<<”Fixed point or Simple iteration method”;
cout<<endl;
cout<<”**********************”<<endl;
do
{
x1=g(x0);
cout<<”iteration-“<<step<<”:\tx1=”<<setw(10)<<x1
<<”and f(x1)=”<<setw(10)<<f(x1)<<endl;
step=step+1;
if(step>N)
{
cout<<”not convergent”;
return 0;
}
x0=x1;
}
while(fabs(f(x1))>e);
cout<<endl<<” Roots is:”<<x1;
getch();
return 0;
}

Output:

Enter the initial guess : 2


Enter the tolerable error: 0.00001
Enter the minimum iteration: 10
Fixed point or Simple iteration method
Iteration-1: x1=0.577350 and f(x1)=-0.474217
Iteration-2: x1=0.796225 and f(x1)=0.138761
Iteration-3: x1=0.746139 and f(x1)=-0.027884
Iteration-1: x1=0.756764 and f(x1)=0.006085
Iteration-2: x1=0.754472 and f(x1)=-0.001305
Iteration-3: x1=0.754965 and f(x1)=0.000281
Iteration-1: x1=0.754859 and f(x1)=-0.000060
Iteration-2: x1=0.754882 and f(x1)=0.000013
Iteration-3: x1=0.754877 and f(x1)=-0.000003

Roots is 0.754877
#include<iostream.h> Newton Raphson Method
#include<iomanip.h>
#include<math.h>
#include<stdlib.h>
#define f(x) 3*x-cos(x)-1
#define g(x) 3+sin(x)
int main()
{
float x0,x1,f0,f1,g0,e;
int step=1,N;
cout<<setprecision(6)
<<setiosflags(ios::showpoint|ios::fixed);
cout<<"Enter initial guess:";
cin>>x0;
cout<<"Enter tolerable error:";
cin>>e;
cout<<"Enter maximum iteration:";
cin>>N;
cout<<endl<<"*******************"<<endl;
cout<<"Newton Raphson Method"<<endl;
cout<<"*******************"<endl;
do
{
g0=g(x0);
f0=f(x0);
if(g0==0.0)
{
cout<<"Mathematical Error";
return 0;
}
x1=x0-f0/g0;
cout<<"Iteration-"<<step<<":\tx="<<setw(10)<<x1
<<" and f(x)="<<setw(10)<<f(x1)<<endl;
x0=x1;
step=step+1;
if(step>N)
{
cout<<"Not Convergent";
return 0;
}
f1=f(x1);
}
while(fabs(f1)>e);
cout<<endl<<"Root is "<<x1;
return 0;
}

Output:
Enter initial guess: 3
Enter the tolerable error: 0.0001
Enter the maximum iteration: 10
Newton Raphson Method
Iteration-1: x=0.137966 and f(x)=-1.576600
Iteration-2: x=0.640463 and f(x)= 0.119571
Iteration-3: x=0.607227 and f(x)= 0.000446
Iteration-4: x=0.607102 and f(x)= 0.000000

Root is 0.607102
#include<iostream.h> Curve Fitting a Parabola
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int i,j,k,n;
float x[10],y[10],p,q,h,a[10][10],c[10];
clrscr();
cout<<"\n Enter the Value of n:";
cin>>n;
cout<<"\n Enter the initial value of x:";
cin>>x[0];
cout<<"\nEnter the Step size h for x:";
cin>>h;
for(i=1;i<n;i++)
{
x[i]=x[i-1]+h;
}
cout<<"\nEnter the value of Y:\n";
for(i=0;i<n;i++)
{
cin>>y[i];
}
p=x[0]-(h/2);
q=x[n-1]+(h/2);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=(pow(q,(i+j+1))-pow(p,(i+j+1)))/(i+j+1);
}
}
for(i=0;i<3;i++)
{
a[i][3]=0;
for(j=0;j<n;j++)
{
if(i==0)
{
a[i][3]=a[i][3]+h*y[i];
}
else
{
a[i][3]=a[i][3]+h*pow(x[j],i)*y[j];
}
}
}
for(k=0;k<3;k++)
{
for(i=0;i<3;i++)
{
if(i!=k)
{
for(j=k+1;j<4;j++)
{f
a[i][j]=a[i][j]-((a[i][k]*a[k][j])/a[k][k]);
}
}
}
}
for(i=0;i<3;i++)
{
c[i]=a[i][3]/a[i][i];
cout<<"The value of c["<<i<<"]is:"<<c[i]<<"\n";
}
cout<<"\nThe required second degree parabola is:\n";
if (c[1]>0&&c[2]>0)
{
cout<<"y="<<c[0]<<"+"<<c[1]<<"+"<<c[2]<<"\n";
}

else if (c[1]<0&&c[2]<0)
{
cout<<"y="<<c[0]<<c[1]<<c[2]<<"\n";
}
else if(c[1]<0)
{
cout<<"y="<<c[0]<<c[1]<<"+"<<c[2]<<"\n";
}
else
{
cout<<"y="<<c[0]<<"+"<<c[1]<<c[2]<<"\n";
}
getch();
return 0;
}
#include<iostream.h> Curve Fitting a Straight Line
#include<conio.h>
#include<iomanip.h>
#include<stdio.h>
#define s 50
int main()
{
clrscr();
int n,i;
float x[s],y[s],sumX=0,sumX2=0,sumY=0,sumXY=0,a,b;
cout<<"Enter the number of data to be entered:"<<endl;
cin>>n;
cout<<"Enter the value of x:"<<endl;
for(i=1;i<=n;i++)
cin>>x[i];
cout<<"Enter the values of y:"<<endl;
for(i=1;i<=n;i++)
cin>>y[i];
for(i=1;i<=n;i++)
{
sumX=sumX+x[i];
sumX2=sumX2+x[i]*x[i];
sumY=sumY+y[i];
sumXY=sumXY+x[i]*y[i];
}
b=(n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
a=(sumY-b*sumX)/n;
cout<<"The value of a is"<<a<<"and b is"<<b<<endl;
cout<<"Eqution of best fit is y="<<a<<"+"<<b<<"x"<<endl;
getch();
return 0;
}
Output:

Enter the number of data to be entered: 5


Enter the values of x:
5 10 15 20 25
Enter the values of y:
16 19 23 26 30

The Value of a is 12.3 and b is 0.7


Equation of best fit is y=12.3+0.7x

You might also like