4th Year Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

1.

(a) Program to solve nonlinear algebraic or transcendental function


with example by Bisection method

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

//define a function using the given equation


double f(double x)
{
double phi=pow(x,2)-4*x-10;
return phi;
}
int main()
{
cout.precision(7);
cout.setf(ios::fixed);
double a,b,c,EP;
int i=0;
a: cout<<"\nEnter the value of a and b\n";
cout<<"\nThe value of a is:\n";
cin>>a;
cout<<"\nThe value of b is:\n";
cin>>b;
cout<<"\nPlease enter the desired accuracy\n";
cin>>EP;

if(f(a)*f(b)>0)
{
cout<<"\nPlease enter a different pair of a and b\n";
goto a;
}
else
{
while(abs(a-b)>=EP)
{
c=(a+b)/2.0;
if(f(c)==0)
{
cout<<"\nThe root is:\n";
cout<<c;
break;
}
if(f(c)*f(a)>0)
a=c;
else
b=c;
i++;
cout<<"c="<<c<<endl;
}
cout<<"Iteration number:"<<i<<endl;
cout<<"\nThe root is:\n";
cout<<c;
}
return 0;
}

Output

Enter the value of a and b

The value of a is:


-2

The value of b is:


-1

Please enter the desired accuracy


0.001
c=-1.5000000
c=-1.7500000
c=-1.6250000
c=-1.6875000
c=-1.7187500
c=-1.7343750
c=-1.7421875
c=-1.7382812
c=-1.7402344
c=-1.7412109
Iteration number:10

The root is:


-1.7412109

Remark: Since the root obtained by the code is almost the same as the root that we found
earliest by manual calculation. So this code could be implemented to solve other algebraic or
transcendental functions to find the root using the bisection method.
1.(b) Program to solve nonlinear algebraic or transcendental function
with example by iterative method

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

double f(double x)
{
double s;
s=exp(x)/3l;
return s;
}
using namespace std;

int main()
{

double x0, x1, e,h;

cout<< setprecision(6)<< fixed;

cout<<"Enter initial guess from (0,1) interval ";


a:
cin>>x0;

if (x0<0 ||
x0>1)
{
cout<<"Please enter initial guess from (0,1) interval ";
goto a;
}

cout<<"Enter tolerable error: ";


cin>>e;
cout<< endl<<"************************"<< endl;
cout<<"Fixed Point Iteration Method"<< endl;
cout<<"************************"<< endl;
Do
{
x1 = f(x0);
h=x0;
x0= x1;
cout<<x1<<endl;

}while( fabs(x0-h)> e);

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

return(0);
}

Output

Enter initial guess from (0,1) interval 0.5


Enter tolerable error: 0.00001

************************
Fixed Point Iteration Method
************************
0.549574
0.577505
0.593862
0.603657
0.609598
0.613231
0.615462
0.616837
0.617686
0.618210
0.618535
0.618735
0.618860
0.618936
0.618984
0.619013
0.619032
0.619043
0.619050

Root is 0.619050

Remark: Since the root obtained by the code is almost the same as the root that we found
earliest by manual calculation. So this code could be implemented to solve other algebraic or
transcendental functions to find the root using the iterative method.
1.(c) Program to solve nonlinear algebraic or transcendental function
with example by Newton-Raphson method

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

double f(double x)
{
double a;
a=10*x-10-sin(x);
return a;
}

double fprime(double x)
{
double b;
b=10-cos(x);
return b;
}

int main()
{
double x0,x1,e;
int i=0;
cout.precision(9);
cout.setf(ios::fixed);
a: cout<<"Enter the initial guess of the root\n";
cin>>x1;
cout<<"\nEnter the desired degree of accuracy\n";
cin>>e;
while( fabs(x1-x0)>=e)
{
x0=x1;
x1=x0-f(x0)/fprime(x0);
i++;
cout<<"x1="<<x1<<endl;
}
cout<<"\nThe required root of thr function is:"<<x1<<endl;
cout<<"The number of times the iteration was continued is:"<<i<<endl;
int si;
cout<<"\nDo you want to rerun the code?\n";
cout<<"\n If yes,press 1 otherwise 0\n";
cin>>si;
if(si==1)
goto a;
return 0;
}

Output

Enter the initial guess of the root


1

Enter the desired degree of accuracy


0.00001
x1=1.088953264
x1=1.088597758
x1=1.088597752

The required root of thr function is:1.088597752


The number of times the iteration was continued is:3

Do you want to rerun the code?

If yes,press 1 otherwise 0

Remark: It's great to hear that your code is producing results that are close to the
manually calculated roots. The Newton-Raphson method is indeed a powerful numerical
technique for finding roots of algebraic or transcendental functions. As long as you have
a function that is continuous and differentiable in the neighborhood of the root you want
to find, you can implement the Newton-Raphson method to approximate that root.
1.(d) Program to solve a system of nonlinear equations (two variables
only) with example by Newton-Raphson method

Code

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define f(x,y) (x*x-y*y-3)
#define g(x,y) (x*x+y*y-13)
#define dfx(x,y) (2*x)
#define dfy(x,y) (-2*y)
#define dgx(x,y) (2*x)
#define dgy(x,y) (2*y)
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
double x, x0, y, y0, fx, fx0, d, h, k, ep;
cout<<"Enter the initial guesses x0 and y0 respectively:\n";
m:
cin>>x0>>y0;
cout<<"Enter the desired accuracy:\n"<<endl;
cin>>ep;
do
{
x=x0,y=y0;
d=dfx(x,y)*dgy(x,y)-dgx(x,y)*dfy(x,y);
if (d==0)
{
cout<<"The system doesn't converge for this initial guess. Please, give new guess:"<<endl;
goto m;
}
else
{
h=(g(x,y)*dfy(x,y)-f(x,y)*dgy(x,y))/d;
k=(f(x,y)*dgx(x,y)-g(x,y)*dfx(x,y))/d;
x0=x+h, y0=y+k;

}
}while(fabs(x-x0)>ep || fabs(y-y0)>ep);
cout<<"Root of the equation: x= "<<x0<<" y= "<<y0<<endl;
return 0;
}
Output

Enter the initial guesses x0 and y0 respectively:


2.54
2.54
Enter the desired accuracy:

0.00001
Root of the equation: x= 2.828427 y= 2.236068

Remark:The Newton-Raphson method is a powerful numerical technique for finding


roots of algebraic or transcendental functions. It can also be extended to two-variable
functions. To implement the Newton-Raphson method for two-variable functions.
It's great to hear that your code is producing results that are close to the manually
calculated roots. The Newton-Raphson method is indeed a powerful numerical
technique for finding roots of algebraic or transcendental functions. As long as you have
a function that is continuous and differentiable in the neighborhood of the root you want
to find, you can implement the Newton-Raphson method to approximate that root.
2.(a) Program to calculate Interpolation by Newton’s backward formula
with example

Code
//Backward difference Table code AND interpolation
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.precision(7);
cout.setf(ios::fixed);
int i=0,j=0,n,k=0;
cout<<"\nEnter the number of values to be entered.\n";
cin>>n;
double x[n], y[n][n];
cout<<"\nEnter the values of x\n";
for (i=0;i<n;i++)
cin>>x[i];
cout<<"\nEnter the values of y\n";
for (i=0;i<n;i++)
cin>>y[i][0];
for (j=1;j<n;j++)
{
k++;
for (i=k;i<n;i++)
{
y[i][j]=y[i][j-1]-y[i-1][j-1];
}
}
cout<<"\n The Backward Difference Table is as follows: \n\n";
cout<<"x"<<setw(10)<<"y"<<setw(10);
for (i=1;i<n;i++)
cout<<"y"<<i<<setw(10);
cout<<"\n-----------------------------------------------------------------------\n";
for (i=0;i<n;i++)
{
cout<<x[i]<<setw(10);
for (j=0;j<=i;j++)
{
cout<<y[i][j];
cout<<setw(10);
}
cout<<"\n";
}
double h, u,fx, p, sum, tt=1.0;
cout<<"\nEnter the value of x at which the value of y to be calculated\n";
cin>>fx;
h=x[1]-x[0];
p=(fx-x[n-1])/h;
sum=y[n-1][0];
for(j=1;j<n;j++)
{
tt=tt*(p+j-1)/j;
sum+=tt*y[n-1][j];
}
cout<<"\nThe value of y at x=38 is\n\n";
cout<<"y("<<fx<<")="<<sum;
return 0;
}

Output

Enter the number of values to be entered.


4

Enter the values of x


1
3
5
7

Enter the values of y


24
120
336
720

The Backward Difference Table is as follows:

x y y1 y2 y3
-----------------------------------------------------------------------
1.000000024.0000000
3.0000000120.000000096.0000000
5.0000000336.0000000216.0000000120.0000000
7.0000000720.0000000384.0000000168.000000048.0000000

Enter the value of x at which the value of y to be calculated


8

The value of y at x=38 is

y(8.0000000)=990.0000000

Remark: Since the obtained the required value by the code is almost the same as we found
earlier by manual calculation. So this code could be implemented to solve other algebraic or
transcendental functions to find the required value using Newton's backward formula.
2.(b) Program to calculate Interpolation by Newton’s Forward formula
with example

Code
//Forward Difference table AND forward interpolation
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.precision(4);
cout.setf(ios::fixed);
int i=0,j=0,n,k;
cout<<"\nEnter the number of values to be entered.\n";
cin>>n;
double x[n], y[n][n];
cout<<"\nEnter the values of x\n";
for (i=0;i<n;i++)
cin>>x[i];
cout<<"\nEnter the values of y\n";
for (i=0;i<n;i++)
cin>>y[i][0];
for (j=1;j<n;j++)
for (i=0;i<n-j;i++)
{
y[i][j]=y[i+1][j-1]-y[i][j-1];
}
cout<<"\n The Forward Difference Table is as follows: \n\n";
cout<<"x"<<setw(10)<<"y"<<setw(10);
for (i=1;i<n;i++)
cout<<"y"<<i<<setw(10);
cout<<"\n-----------------------------------------------------------------------\n";
k=n;
for (i=0;i<n;i++)
{
cout<<x[i]<<setw(10);
for (j=0;j<k;j++)
{
cout<<y[i][j];
cout<<setw(10);
}
cout<<"\n";
k--;
}
double h, u, fx,p, sum, tt=1.0;
cout<<"\nEnter the value of x at which the value of y to be calculated\n";
cin>>fx;
h=x[1]-x[0];
p=(fx-x[0])/h;
sum=y[0][0];
for(j=1;j<n;j++)
{
tt=tt*(p-j+1)/j;
sum+=tt*y[0][j];
}
cout<<"\nThe value of y at x=0.75 is\n\n";
cout<<"y("<<fx<<")="<<sum;
return 0;
}

Output

Enter the values of x


1
3
5
7

Enter the values of y


24
120
336
720

The Forward Difference Table is as follows:

x y y1 y2 y3
-----------------------------------------------------------------------
1.0000 24.0000 96.0000 120.0000 48.0000
3.0000 120.0000 216.0000 168.0000
5.0000 336.0000 384.0000
7.0000 720.0000

Enter the value of x at which the value of y to be calculated


8

The value of y at x=0.75 is

y(8.0000)=990.0000

Remark: Since the obtained the required value by the code is almost the same as we
found earlier by manual calculation. So this code could be implemented to solve other
algebraic or transcendental functions to find the required value using Newton's Forward
formula.
2.(c) Program to calculate Interpolation and extrapolation by
Lagrange’s formula with example

Code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(7);
cout.setf(ios::fixed);
int i, j, k, n;
cout<<"Enter the number of values to be entered\n";
cin>>n;
double x[n], y[n];
cout<<"Enter the of values of x\n";
for(i=0; i<n; i++)
cin>>x[i];
cout<<"Enter the of values of y\n";
for(i=0; i<n; i++)
cin>>y[i];
//code of interpolation starts here
double xn, sum=0, temp;
cout<<"Enter the values of x at which y to be calculated\n";
cin>>xn;
for(j=0; j<n; j++)
{
temp=1;
for(i=0; i<n; i++)
{
if(i!=j)
temp=temp*(xn-x[i])/(x[j]-x[i]);
}
sum=sum+temp*y[j];
}
cout<<"The value of y at x="<<xn<<" is: "<<sum<<endl;
double per_err;
per_err=(abs(log10(xn)-sum)/log10(xn))*100;
cout<<"Percent Error = "<<per_err<<" % "<<endl;

return 0;
}
Output

Enter the number of values to be entered


4
Enter the of values of x
300
304
305
307
Enter the of values of y
2.4771
2.4829
2.4843
2.4871
Enter the values of x at which y to be calculated
301
The value of y at x=301.0000000 is: 2.4785971
Percent Error =0.0012365 %

Remark: Since the obtained the required value by the code is almost the same as we
found earlier by manual calculation. So this code could be implemented to solve other
algebraic or transcendental functions to find the required value using Interpolation and
extrapolation by Lagrange’s formula
2.(d) Program to calculate Interpolation and extrapolation by Newton’s
divided difference formula with example

Code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i, j, k, n;
cout<<"Enter the number of values to be entered\n";
cin>>n;
double x[n], y[n][n];
cout<<"Enter the of values of x\n";
for(i=0; i<n; i++)
cin>>x[i];
cout<<"Enter the of values of y\n";
for(i=0; i<n; i++)
cin>>y[i][0];
//Calculate difference table
for(j=1; j<n; j++)
{
for(i=0; i<n-j; i++)
{
y[i][j]=(y[i+1][j-1]-y[i][j-1])/(x[i+j]-x[i]);
}
}
//Print difference table
cout<<"\nThe Divided difference table is as follows:\n\n";
cout<<"x"<<setw(20)<<"y"<<setw(20);
for(i=1; i<n; i++)
cout<<"d"<<i<<"y"<<setw(20);
cout<<"\n-------------------------------------------------------\n";
k=n;
for(i=0; i<n; i++)
{
cout<<x[i]<<setw(20);
for(j=0; j<k; j++)
{
cout<<y[i][j]<<setw(20);
}
cout<<"\n";
k--;
}
//Code of interpolation
double xn, sum=y[0][0], temp=1.0;
cout<<"Enter the values of x at which y to be calculated\n";
cin>>xn;
for(j=1; j<n; j++)
{
temp=temp*(xn-x[j-1]);
sum=sum+temp*y[0][j];
}
cout<<"The value of y at x="<<xn<<" is: "<<sum<<endl;
double per_err;
per_err=(abs(log10(xn)-sum)/log10(xn))*100;
cout<<"Percent Error = "<<per_err<<" % "<<endl;
return 0;
}

Output

Enter the number of values to be entered


4
Enter the of values of x
300
304
305
307
Enter the of values of y
2.4771
2.4829
2.4843
2.4871
Enter the values of x at which y to be calculated
301
The value of y at x=301.0000000 is: 2.4785971
Percent Error =0.0012365 %

Remark: Since the obtained the required value by the code is almost the same as we
found earlier by manual calculation. So this code could be implemented to solve other
algebraic or transcendental functions to find the required value using Newton’s divided
difference formula
3.(a) Program to solve a system of linear equations (3 variables) with
example Gauss elimination method

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

int main()
{
int n,i,j,k;
cout.precision(4);
cout.setf(ios::fixed);
a:
cout<<"Enter the size of system"<<endl;
cin>>n;
float a[n][n+1],x[n];
cout<<"Enter the elements of system co efficients row wise"<<endl;
for(i=0;i<n;i++)
for(j=0;j<=n;j++)
cin>>a[i][j];
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(abs(a[i][i])<abs(a[k][i]))
{
for(j=0;j<=n;j++)
{
double temp =a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
}

cout<<"The matrix after pivotaisation"<<endl;


for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
cout<<a[i][j]<<" ";

cout<<endl;
}
//Gaussian elimination
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
double t=a[k][i]/a[i][i];

for(j=0;j<=n;j++)
a[k][j]=a[k][j]-t*a[i][j];
}
}

cout<<"The new matrix after gauss elimination"<<endl;

for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<"\n";
}

for(i=n-1;i>=0;i--)
if(a[i][i]==0)
{
cout<<"Inconsistency detected, calculation another system"<<endl;
goto a;
}
//back substitution
for(i=n-1;i>=0;i--)
{

x[i]=a[i][n];

for(j=i+1;j<n;j++)
{
if(j!=i)
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
}

cout<<"Values"<<endl;
for(i=0;i<n;i++)
cout<<x[i]<<endl;

}
Output

Enter the size of system


3
Enter the elements of system co efficients row wise
2 1 1 10
3 2 3 18
1 4 9 16
The matrix after pivotaisation
3.0000 2.0000 3.0000 18.0000
1.0000 4.0000 9.0000 16.0000
2.0000 1.0000 1.0000 10.0000
The new matrix after gauss elimination
3.0000 2.0000 3.0000 18.0000
-0.0000 3.3333 8.0000 10.0000
-0.0000 0.0000 -0.2000 -1.0000
Values
7.0000
-9.0000
5.0000

Remark: Since the value of the variable x,y ,z is obtained by the code is almost
the same as the value that we found earlier by manual calculation. So this code
could be implemented to solve other systems of linear equations using Gauss
elimination method.
3.(b) Program to solve a system of linear equations (3 variables) with
example Jacobi iterative method

Code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(4);
cout.setf(ios::fixed);
int k,i,j,n;
cout<<"\nEnter the no. of equations\n";
cin>>n; //Input no. of equations
double a[n][n+1]; //declare a 2d array for storing the elements of the augmented matrix
double x[n];
double x1[n]; //declare an array to store the values of variables
double r[n];
double eps,temp;
cout<<"\n Enter the elements of the augmented matrix row-wise:\n";
for (i=0;i<n;i++)
for (j=0;j<=n;j++)
cin>>a[i][j];
cout<<"\n The matrix you have entered is\n";
for (i=0;i<n;i++)
{
for (j=0;j<=n;j++)
cout<<a[i][j]<< setw(10);
cout<<"\n";
}
cout<<"\n";
cout<<"\n Enter the initial values of the variables:\n";
for (i=0;i<n;i++)
cin>>x[i];
cout<<"\n Enter the accuracy upto which you want the solution:\n";
cin>>eps;
for (i=0;i<n-1;i++) //Pivotisation(partial) to make the equations diagonally dominant
for (k=i+1;k<n;k++)
if (abs(a[i][i])<abs(a[k][i]))
for (j=0;j<=n;j++)
{
temp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
cout<<"\n The matrix after pivoting\n";
cout<<"\n----------------------------------------------------------------------\n";
for (i=0;i<n;i++)
{
for(j=0;j<=n;j++)
{
cout<<a[i][j]<<setw(10);
}
cout<<"\n";
}
cout<<"\n";

cout<<"\n----------------------------------------------------------------------";
do //Perform iterations to calculate x1,x2,...xn
{
for (i=0;i<n;i++) //Loop that calculates x1,x2,...xn
{
x1[i]=x[i];
r[i]=(a[i][n]/a[i][i]);
for (j=0;j<n;j++)
{
if (j!=i)
// continue;
r[i]=r[i]-(a[i][j]/a[i][i]*x[j]);
}
}
for (i=0;i<n;i++)
{
x[i]=r[i];
}
} while (fabs(x1[0]-x[0])>eps||fabs(x1[1]-x[1])>eps||fabs(x1[2]-x[2])>eps);

cout<<"\n The solution is as follows:\n";


for (i=0;i<n;i++)
cout<<"\n x["<<i<<"]="<<x[i]; //Print the contents of x[]
return 0;
}
Output

The matrix you have entered is


28.0000 4.0000 -1.0000 32.0000
1.0000 3.0000 10.0000 24.0000
2.0000 17.0000 4.0000 35.0000

Enter the initial values of the variables:


000

Enter the accuracy upto which you want the solution:


0.0001

The matrix after pivoting

----------------------------------------------------------------------
28.0000 4.0000 -1.0000 32.0000
2.0000 17.0000 4.0000 35.0000
1.0000 3.0000 10.0000 24.0000

----------------------------------------------------------------------
The solution is as follows:

x[0]=0.9936
x[1]=1.5070
x[2]=1.8485

Remark: Since the value of the variable x,y ,z is obtained by the code is almost
the same as the value that we found earlier by manual calculation. So this code
could be implemented to solve other systems of linear equations using the Jacobi
iterative method.
3.(c) Program to solve a system of linear equations (3 variables) with
example Gauss Seidal iterative method

Code
//Gaus-seidel (Written by: Manas Sharma - University of Delhi)
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(7);
cout.setf(ios::fixed);
int n,i,j,k,flag=0,count=0;
cout<<"\nEnter the no. of equations\n";
cin>>n; //Input no. of equations
double a[n][n+1]; //declare a 2d array for storing the elements of the augmented matrix
double x[n]; //declare an array to store the values of variables
double eps,y;
cout<<"\nEnter the elements of the augmented matrix row-wise:\n";
for (i=0;i<n;i++)
for (j=0;j<=n;j++)
cin>>a[i][j];
cout<<"\nEnter the initial values of the variables:\n";
for (i=0;i<n;i++)
cin>>x[i];
cout<<"\nEnter the accuracy upto which you want the solution:\n";
cin>>eps;
for (i=0;i<n;i++) //Pivotisation(partial) to make the equations diagonally dominant
for (k=i+1;k<n;k++)
if (abs(a[i][i])<abs(a[k][i]))
for (j=0;j<=n;j++)
{
double temp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
cout<<"Iter"<<setw(15);
for(i=0;i<n;i++)
cout<<"x"<<i<<setw(15);
cout<<"\n----------------------------------------------------------------------";
do //Perform iterations to calculate x1,x2,...xn
{
cout<<"\n"<<count+1<<"."<<setw(15);
for (i=0;i<n;i++) //Loop that calculates x1,x2,...xn
{
y=x[i];
x[i]=a[i][n];
for (j=0;j<n;j++)
{
if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
if (abs(x[i]-y)<=eps) //Compare the ne value with the last value
flag++;
cout<<x[i]<<setw(15);
}
cout<<"\n";
count++;
}while(flag<n); //If the values of all the variables don't differ from their previious
values with error more than eps then flag must be n and hence stop the loop

cout<<"\n The solution is as follows:\n";


for (i=0;i<n;i++)
cout<<"x"<<i<<" = "<<x[i]<<endl; //Print the contents of x[]
return 0;
}
Output

Enter the no. of equations


3

Enter the elements of the augmented matrix row-wise:


28 4 -1 32
1 3 10 24
2 17 4 35

Enter the initial values of the variables:


000

Enter the accuracy upto which you want the solution:


0.0001
Iter x0 x1 x2
----------------------------------------------------------------------
1. 1.1428571 1.9243697 1.7084034

2. 0.9289616 1.5475567 1.8428368

3. 0.9875932 1.5090274 1.8485325

4. 0.9933008 1.5070158 1.8485652

5. 0.9935894 1.5069742 1.8485488

The solution is as follows:


x0 = 0.9935894
x1 = 1.5069742
x2 = 1.8485488

Remark: Since the value of the variable x,y ,z is obtained by the code is almost
the same as the value that we found earlier by manual calculation. So this code
could be implemented to solve other systems of linear equations using the Jacobi
iterative method.
4.(b) Numerical integration by using trapezoidal formula

Code
//Use of Trapezoidal Method for numerical Integration
#include<iostream>
#include<cmath>
using namespace std;
double f(double x) //Function f(x)
{
double a=1/(1+x);
return a;
}
int main()
{
int n,i;
double a,b,h,sum=0,integral;
cout<<"Enter the limits of integration,\nInitial limit,a=";
cin>>a;
cout<<"Final limit, b=";
cin>>b;
cout<<"Enter the no. of subintervals, n=";
cin>>n;
double x[n+1],y[n+1];
h=(b-a)/n;
for (i=0;i<=n;i++)
{
x[i]=a+i*h;
y[i]=f(x[i]);
}
for (i=1;i<n;i++)
{
sum=sum+y[i];
}
integral=h/2.0*((y[0]+y[n])+2.0*sum);
cout<<"The definite integral is "<<integral<<endl;
return 0;
}
Output

Enter the limits of integration,


Initial limit,a=0
Final limit, b=1
Enter the no. of subintervals, n=4
The definite integral is 0.697024

Remark: The code successfully calculates the value of an integration using trapezoidal
formula and matches the result obtained from manual calculations. This indicates that
the code can be applied to solve other integration problems as well. The key takeaway
is that the trapezoidal formula implementation in the code appears to be accurate and
can be reused for a variety of integration tasks.
4.(c) Numerical integration by using Simson’s 1/3 formula

Code
//Simpson's 1/3rd Rule for Evaluation of Definite Integrals
#include<iostream>
#include<cmath>
using namespace std;
double f(double x)
{
double a=1/(1+x);
return a;
}
int main()
{ cout.precision(5);
cout.setf(ios::fixed);
int n,i;
double a,b,c,h,sum=0,integral;
cout<<"\nEnter the interval of integration,\n\nLower limit a= ";
cin>>a;
cout<<"\nUpper limit, b="; //get the limits of integration
cin>>b;
cout<<"\nEnter the no. of subintervals\n\n";
cout<<"\nPlease enter a number that is multiple of 2";
cin>>n;
double x[n+1],y[n+1];
h=(b-a)/n;
for (i=0;i<=n;i++)
{
x[i]=a+i*h;
y[i]=f(x[i]);
}
for (i=1;i<n;i+=2)
{
sum=sum+4.0*y[i];
}
for (i=2;i<n;i+=2)
{
sum=sum+2.0*y[i];
}
integral=h/3.0*(y[0]+y[n]+sum);
cout<<"\nThe definite integral is "<<integral<<"\n"<<endl;
return 0;
}
Output

Enter the interval of integration,

Lower limit a= 0

Upper limit, b=1

Enter the no. of subintervals

Please enter a number that is multiple of 24

The definite integral is 0.69325

Remark: The code successfully calculates the value of an integration using


Simpson's 1/3 rule and matches the result obtained from manual calculations.
This indicates that the code can be applied to solve other integration problems as
well. The key takeaway is that the Simpson's 1/3 rule implementation in the code
appears to be accurate and can be reused for a variety of integration tasks.
4.(d) Numerical integration by using Simson’s 3/8 formula

Code
//Simpson's 3/8th Rule for Evaluation of Definite Integrals
#include<iostream>
#include<cmath>
using namespace std;
double f(double x) {
double a=1/(1+x*x);
return a;
}
int main() {
cout.precision(6);
cout.setf(ios::fixed);
int n,i;
double a,b,c,h,sum=0,integral;
cout<<"\nEnter the interval of integration,\n\nLower limit,a= ";
cin>>a;
cout<<"\nUpper limit, b="; //get the limits of integration
cin>>b;
cout<<"\nEnter the no. of subintervals\n\n";
cout<<"\nPlease enter a number that is multiple of 3\n\n";
cin>>n;
double x[n+1],y[n+1];
h=(b-a)/n;
for (i=0;i<=n;i++){
x[i]=a+i*h;
y[i]=f(x[i]);
}
for (i=1;i<n;i++)
{
if (i%3==0)
sum=sum+2*y[i];
else
sum=sum+3*y[i];
}
integral=3*h/8*(y[0]+y[n]+sum);
cout<<"\nThe definite integral is "<<integral<<"\n"<<endl;
return 0;
}
Output

Enter the interval of integration,

Lower limit,a= 0

Upper limit, b=1

Enter the no. of subintervals

Please enter a number that is multiple of 3 4

The definite integral is 0.750331

Remark: The code successfully calculates the value of an integration using


Simpson's 3/8 rule and matches the result obtained from manual calculations.
This indicates that the code can be applied to solve other integration problems as
well. The key takeaway is that the Simpson's 3/8 rule implementation in the code
appears to be accurate and can be reused for a variety of integration tasks.
5.(a) Numerical solution of first order DE of the type by Euler formula
at a point and for particular range with example.

Code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double df(double x, double y)
{
double a=(1-y);
return a;
}
int main()
{
int n,i;
double x0,y0,h,X;
cout.precision(5);
cout.setf(ios::fixed);
cout<<"\nEnter the initial condition\n";
cout<<"\nEnter the initial value of x\n";
cin>>x0;
cout<<"\nEnter the initial value of y\n";
cin>>y0;
cout<<"\nFor what value of x do you want to find the value of y\n";
cin>>X;
cout<<"\nEnter the number of subdivisions\n";
cout<<"\nPlease enter a large value of \n";
cin>>n;
h=(X-x0)/n;
double x[n],y[n];
x[0]=x0; y[0]=y0;
for(i=0;i<n;i++) {
y[i+1]=y[i]+h*df(x[i],y[i]);
x[i+1]=x[i]+h;
}
cout<<"\n\nThe approximate value of y at x="<<x[n]<<" is "<<y[n]<<endl;
double per_err;
per_err=(abs((1-exp(-X))-y[n]))/(1-exp(-X))*100;
cout<<"Percent Error = "<<per_err<<" % "<<endl;
return 0;
}
Output

Enter the initial condition

Enter the initial value of x


0

Enter the initial value of y


0

For what value of x do you want to find the value of y


0.1

Enter the number of subdivisions

Please enter a large value of


10

The approximate value of y at x=0.10000 is 0.09562


Percent Error = 0.47849 %

Remark: Since the value y at x = 0.10 obtained by the code is same as the
value that we found earlier by manual calculation. And the Percent Error is just
0.47849 % . It is very small .So this code could be implemented to solve other 1st
order differential equations by Euler's method.
5.(b) Numerical solution of first order DE of the type by Modified Euler
formula at a point and for particular range with example

Code
//Modified Euler's Method for differential equations
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double df(double x, double y)
{
double a=x+y;
return a;
}
int main()
{
int n,i;
double x0,y0,h,X;
cout.precision(7);
cout.setf(ios::fixed);
cout<<"\nEnter the initial condition\n";
cout<<"\nEnter the initial value of x\n";
cin>>x0;
cout<<"\nEnter the initial value of y\n";
cin>>y0;
cout<<"\nFor what value of x do you want to find the value of y\n";
cin>>X;
cout<<"\nEnter the number of subdivisions\n";
cout<<"\nPlease enter a large value of \n";
cin>>n;
h=(X-x0)/n;
double x[n],y1[n],y[n];
x[0]=x0; y[0]=y0,y1[0]=0;
for(i=0;i<n;i++)
{
y[i+1]=y[i]+h*df(x[i],y[i]);
x[i+1]=x[i]+h;
y1[i+1]=y[i]+h/2.0*(df(x[i],y[i])+df(x[i+1],y[i+1]));
}
cout<<"\n\nThe approximate value of y at x="<<x[n]<<" is="<<y1[n]<<endl;
#define f(x) (-x*x-2*(x+1)+3*exp(x))
double per_err;
per_err=(abs(f(X)-y[n]))/(1-exp(-X))*100;
cout<<"Percent Error = "<<per_err<<" % "<<endl;
return 0;
}
Output

Enter the initial condition

Enter the initial value of x


0

Enter the initial value of y


1

For what value of x do you want to find the value of y


0.02

Enter the number of subdivisions

Please enter a large value of


10

The approximate value of y at x=0.0200000 is=1.0203660


Percent Error = 0.7974549 %

Remark: Since the value y at x = 0.10 obtained by the code is same as the
value that we found earlier by manual calculation. And the Percent Error is just
0.7974549 % . It is very small .So this code could be implemented to solve other
1st order differential equations by Euler's Modified method.
5.(c) Numerical solution of first order DE of the type by Runge-Kutta
4th order formula at a point and for particular range with example.

Code
//RUNGE-KUTTA 4th Order Method for solving ODE
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double df(double x, double y)
{
double a=x*y+y*y;
return a;
}
int main()
{
int n,i;
double x0,y0,h,X,k1,k2,k3,k4;
cout.precision(7);
cout.setf(ios::fixed);
cout<<"\nEnter the initial condition\n";
cout<<"\nEnter the initial value of x\n";
cin>>x0;
cout<<"\nEnter the initial value of y\n";
cin>>y0;
cout<<"\nFor what value of x do you want to find the value of y\n";
cin>>X;
cout<<"\nEnter the number of subdivisions\n";
//cout<<"\nPlease enter a large value of \n";
cin>>n;
h=(X-x0)/n;
double x[n],y[n];
x[0]=x0; y[0]=y0;
for(i=0;i<n;i++)
{
k1=h*df(x[i],y[i]);
k2=h*df(x[i]+h/2.0, y[i]+k1/2.0);
k3=h*df(x[i]+h/2.0, y[i]+k2/2.0);
k4=h*df(x[i]+h, y[i]+k3);
y[i+1]=y[i]+1/6.0*(k1+2*k2+2*k3+k4);
x[i+1]=x[i]+h;

}
cout<<"\n\nThe approximate value of y at x="<<x[n]<<" is="<<y[n]<<endl;
return 0;
}
Output

Enter the initial condition

Enter the initial value of x


0

Enter the initial value of y


1

For what value of x do you want to find the value of y


0.2

Enter the number of subdivisions


10

The approximate value of y at x=0.2000000 is=1.2773935

Remark: Since the value y a. x = 0.2 obtained by the code is same as the
value that we found earlier by manual calculation.So this code could be
implement to solve other 1st order differential equation by Range-kutta 4th
order method.
5.(d) Numerical solution of system of 1st order DE by Runge-Kutta 4th
order formula at a point and for particular range with example

Code
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
#define dy_dx(x,y,z) (z)
#define dz_dx(x,y,z) (-4*z-4*y)
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,n;
double x0,y0,z0,h,xn;
cout<<"Enter the initial value of x:\n";
cin>>x0;
cout<<"Enter the initial value of y corresponding to x:\n";
cin>>y0;
cout<<"Enter the initial value of z corresponding to x:\n";
cin>>z0;
cout<<"Enter the value of x up to which you want to find the value of y & z:\n";
cin>>xn;
cout<<"Enter the value of h\n";
cin>>h;
n=(xn-x0)/h;
double x[n+1],y[n+1],z[n+1],k1,k2,k3,k4,l1,l2,l3,l4;
x[0]=x0, y[0]=y0, z[0]=z0;
for(i=0;i<n;i++)
{
k1=h*dy_dx(x[i],y[i],z[i]);
l1=h*dz_dx(x[i],y[i],z[i]);
k2=h*dy_dx((x[i]+h/2.0),(y[i]+k1/2.0),(z[i]+l1/2.0));
l2=h*dz_dx((x[i]+h/2.0),(y[i]+k1/2.0),(z[i]+l1/2.0));
k3=h*dy_dx((x[i]+h/2.0),(y[i]+k2/2.0),(z[i]+l2/2.0));
l3=h*dz_dx((x[i]+h/2.0),(y[i]+k2/2.0),(z[i]+l2/2.0));
k4=h*dy_dx((x[i]+h),(y[i]+k3),(z[i]+l3));
l4=h*dz_dx((x[i]+h),(y[i]+k3),(z[i]+l3));
y[i+1]=y[i]+(1/6.0)*(k1+2*k2+2*k3+k4);
z[i+1]=z[i]+(1/6.0)*(l1+2*l2+2*l3+l4);
x[i+1]=x[i]+h;
}
for(i=0; i<=n; i++)
{
cout<<"y("<<x[i]<<"): "<<y[i]<<endl;
cout<<"z("<<x[i]<<"): "<<z[i]<<endl;
}
return 0;
}

Output

Enter the initial value of x:


0
Enter the initial value of y corresponding to x:
0
Enter the initial value of z corresponding to x:
1
Enter the value of x up to which you want to find the value of y & z:
0.1
Enter the value of h
0.1
y(0.000000): 0.000000
z(0.000000): 1.000000
y(0.100000): 0.081867
z(0.100000): 0.655000

Remark: Since the value y a. x = 0.1 obtained by the code is same as the
value that we found earlier by manual calculation.So this code could be
implement to solve other 2nd order differential equation by Range-kutta 4th
order method.
6.(a) Numerical solution of parabolic type PDE by finite difference
formula at a point and for particular range with example.

Code
//Numerical solution to parabolic equation du/dt = c^2 *
d^2u/dx^2
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define u_x_0(x) (sin(3.1416*x)) //f(x)=(sin(3.1416*x))
assigned as initial condition
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,j,m,n, option;
double c,alpha,h,k,x,t,x_i,x_f,t_i,t_f,u_i,u_f;
cout<<"Numerical solution of heat equation using finite
difference formula"<<endl;
cout<<"Enter value of c"<<endl;
cin>>c;
cout<<"Enter initial x and corresponding u (1st boundary
condition)"<<endl;
cin>>x_i>>u_i; //1st boundary condition u(x_i,t)=u_i
cout<<"Enter final x and corresponding u (2nd boundary
condition)"<<endl;
cin>>x_f>>u_f; //2nd boundary condition u(x_f,t)=u_f
t_i=0; //initial simulation time assigned
cout<<"Enter final simulation time"<<endl;
cin>>t_f;
cout<<"Enter value of h and k"<<endl;
z:
cin>>h>>k;
alpha = (c*c*k)/(h*h); //calculated alpha
//check alpha limitation
if(alpha<=0 || alpha>0.5)
{
cout<<"please enter new value of h & k such that 0 <=
alpha <=0.5 where, alpha = (c*c*k)/(h*h)"<<endl;
goto z;
}
//calculated division of x & t
m=(x_f-x_i)/h;
n=(t_f-t_i)/k;
//u defined as [(n+1) by (m+1)] dimensional array
double u[n+1][m+1];
//calculated 1st & last column
for(i=0; i<=n; i++)
{
u[i][0]=u_i; //1st column
u[i][m]=u_f; //last column
}
//calculated 1st row
for( j=1; j<=m-1; j++)
{
x=x_i+j*h; //transforming j into x
u[0][j]= u_x_0(x);
}
//row wise calculated 2nd to last row
for(i=0; i<=n-1; i++)
{
for(j=1; j<=m-1; j++)
{
u[i+1][j]=alpha*(u[i][j-1]+u[i][j+1])+(1-
2*alpha)*u[i][j];
}
}
//printing value of u(x,t)
while(1)
{
cout<<"\nChoose option how you want result:"<<endl;
cout<<"1. x, t both vary"<<endl;
cout<<"2. x vary, t constant"<<endl;
cout<<"3. x constant, t vary"<<endl;
cout<<"4. x, t both constant"<<endl;
cin>>option;
if(option==1)
{
for( i=0; i<=n; i++)
{
t=t_i+i*k; //transforming i into t
for( j=0; j<=m; j++)
{
x=x_i+j*h; //transforming j into x
cout<<"u("<<x<<","<<t<<")=
"<<u[i][j]<<endl;
}
}
}
else if(option==2)
{
cout<<"Enter value of t where you want to find the
value of u:"<<endl;
cin>>t;
i=(t-t_i)/k; //transforming t into i
for( j=0; j<=m; j++)
{
x=x_i+j*h; //transforming j into x
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
else if(option==3)
{
cout<<"Enter value of x where you want to find the
value of u:"<<endl;
cin>>x;
j=(x-x_i)/h; //transforming x into j
for( i=0; i<=n; i++)
{
t=t_i+i*k; //transforming i into t
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
else if(option==4)
{
cout<<"Enter value of x where you want to find the
value of u:"<<endl;
cin>>x;
cout<<"Enter value of t where you want to find the
value of u:"<<endl;
cin>>t;
i=(t-t_i)/k; //transforming t into i
j=(x-x_i)/h; //transforming x into j
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
return 0;
}
Output

Numerical solution of heat equation using finite difference formula


Enter value of c
1
Enter initial x and corresponding u (1st boundary condition)
0
0
Enter final x and corresponding u (2nd boundary condition)
0
0
Enter final simulation time
1
Enter value of h and k
0.5
0.125

Choose option how you want result:


1. x, t both vary
2. x vary, t constant
3. x constant, t vary
4. x, t both constant
5

Remark: The code successfully calculates the value using least square method and
matches the result obtained from manual calculations. This indicates that the code can
be applied to solve other problems as well.
6.(b) Numerical solution of hyperbolic type PDE by finite difference
formula at a point and for particular range with example.

Code
//Numerical solution to hyperbolic equation d^2u/dt^2 = c^2 *
d^2u/dx^2
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define u_x_0(x) (4*x-x*x) //f(x)=(4*x-x*x) assigned as 1st
initial condition
#define du_dt(x) (0) //g(x)=0 assigned as 2nd initial
condition
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,j,m,n,option;
double c,alpha,h,k,x,t,x_i,x_f,t_i,t_f,u_i,u_f;
cout<<"Numerical solution of wave equation using finite
difference formula"<<endl;
cout<<"Enter value of c"<<endl;
cin>>c;
cout<<"Enter initial x and corresponding u (1st boundary
condition)"<<endl;
cin>>x_i>>u_i; //1st boundary condition u(x_i,t)=u_i
cout<<"Enter final x and corresponding u (2nd boundary
condition)"<<endl;
cin>>x_f>>u_f; //2nd boundary condition u(x_f,t)=u_f
t_i=0; //initial simulation time assigned
cout<<"Enter final simulation time"<<endl;
cin>>t_f;
cout<<"Enter value of h and k"<<endl;
z:
cin>>h>>k;
alpha = c*(k/h); //calculated alpha
//check alpha limitation
if(alpha>1)
{
cout<<"please enter new value of h & k such that
alpha<1 where, alpha=c*(k/h)"<<endl;
goto z;
}
//calculated division of x & t
m=(x_f-x_i)/h;
n=(t_f-t_i)/k;
//u defined as [(n+1) by (m+1)] dimensional array
double u[n+1][m+1];
//calculated 1st & last column
for(i=0; i<=n; i++)
{
u[i][0]=u_i; //1st column
u[i][m]=u_f; //last column
}
//calculated 1st row
for(j=1; j<=m-1; j++)
{
x=x_i+j*h; //transforming j into x
u[0][j]= u_x_0(x);
}
//calculated 2nd row
for(j=1; j<=m-1; j++)
{
x=x_i+j*h; //transforming j into x
u[1][j]=((pow(alpha,2))*(u[0][j+1]+u[0][j-1]))/2.0+(1-
pow(alpha,2))*u[0][j]+k*du_dt(x);
}
//row wise calculated 3rd to last row
for(i=1; i<=n-1; i++)
{
for(j=1; j<=m-1; j++)
{
x=x_i+j*h; //transforming j into x
u[i+1][j]=(pow(alpha,2))*(u[i][j1]+u[i][j+1])+2*(1-pow(alpha,2))*u[i][j]-u[i-1][j];
}
}
//printing value of u(x,t)
while(1)
{
cout<<"\nChoose option how you want result:"<<endl;
cout<<"1. x, t both vary"<<endl;
cout<<"2. x vary, t constant"<<endl;
cout<<"3. x constant, t vary"<<endl;
cout<<"4. x, t both constant"<<endl;
cin>>option;
if(option==1)
{
for( i=0; i<=n; i++)
{
t=t_i+i*k; //transforming i into t
for( j=0; j<=m; j++)
{
x=x_i+j*h; //transforming j into x
cout<<"u("<<x<<","<<t<<")=
"<<u[i][j]<<endl;
}
}
}
else if(option==2)
{
cout<<"Enter value of t where you want to find the
value of u:"<<endl;
cin>>t;
i=(t-t_i)/k; //transforming t into i
for( j=0; j<=m; j++)
{
x=x_i+j*h; //transforming j into x
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
else if(option==3)
{
cout<<"Enter value of x where you want to find the
value of u:"<<endl;
cin>>x;
j=(x-x_i)/h; //transforming x into j
for( i=0; i<=n; i++)
{
t=t_i+i*k; //transforming i into t
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
else if(option==4)
{
cout<<"Enter value of x where you want to find the
value of u:"<<endl;
cin>>x;
cout<<"Enter value of t where you want to find the
value of u:"<<endl;
cin>>t;
i=(t-t_i)/k; //transforming t into i
j=(x-x_i)/h; //transforming x into j
cout<<"u("<<x<<","<<t<<")= "<<u[i][j]<<endl;
}
}
return 0;
}
Output

Numerical solution of wave equation using finite difference formula


Enter value of c
1
Enter initial x and corresponding u (1st boundary condition)
0
0
Enter final x and corresponding u (2nd boundary condition)

0
0
Enter final simulation time
1
Enter value of h and k
1
2
please enter new value of h & k such that alpha<1 where, alpha=c*(k/h)
2
1

Choose option how you want result:


1. x, t both vary
2. x vary, t constant
3. x constant, t vary
4. x, t both constant

Remark: The code successfully calculates the value using least square method and
matches the result obtained from manual calculations. This indicates that the code can
be applied to solve other problems as well.
7.(a) Curve fitting of the form y=a+bx+cx^2

Code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,j,k,n,m;
cout<<"\nEnter the number of points to be entered\n";
cin>>m;
double p[m], q[m], temp, t;
cout<<"\nEnter the x values\n";
for(i=0; i<m; i++)
{
cin>>p[i];
}
cout<<"\nEnter the y values\n";
for(i=0; i<m; i++)
{
cin>>q[i];
}
n=2; //Set degree of polynomial
double a[n+1][n+2],x[n+1];
for(i=0; i<=n; i++)
{
//Calculate different element of the augmented matrix in a row
for(j=0; j<=n; j++)
{
a[i][j]=0;
for(k=0;k<m;k++)
{
a[i][j]=a[i][j]+pow(p[k],(i+j));
}
}
//Calculate last element of the augmented matrix in a row
a[i][n+1]=0;
for(k=0;k<m;k++)
{
a[i][n+1]=a[i][n+1]+pow(p[k],i)*q[k];
}
}
n=n+1;
//Do pivoting
for(k=0; k<n-1; k++)
{
for(i=k+1; i<n; i++)
{
if(fabs(a[k][k])<fabs(a[i][k]))
{
for(j=0; j<=n; j++)
{
temp=a[k][j];
a[k][j]= a[i][j];
a[i][j]=temp;
}
}
}
}
//Do the elementary row operation
for(k=0; k<n-1; k++)
{
for(i=k+1; i<n; i++)
{
t=a[i][k]/a[k][k];
for(j=0; j<=n; j++)
{
a[i][j]=a[i][j]-t*a[k][j];
}
}
}
//Back substitution
for(i=n-1; i>=0; i--)
{
x[i]=a[i][n];
for(j=i+1; j<n; j++)
{
if(j!=i)
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
}
cout<<"\nThe values of the coefficients are\n";
for(i=0; i<n; i++)
{
cout<<x[i]<<endl;
}
cout<<"\nThe required polynomial is\n";
cout<<"y="<<x[0]<<"+"<<x[1]<<"x+"<<x[2]<<"x^2"<<endl;
return 0;
}
Output

Enter the number of points to be entered


5

Enter the x values


0
1
2
3
4

Enter the y values


1
1.5
1.9
2.2
7

The values of the coefficients are


1.394286
-1.158571
0.607143

The required polynomial is


y=1.394286+-1.158571x+0.607143x^2

Remark: Since the fitted polynomial obtained by the code is almost same as the
fitted polynomial that we found earlier by manual calculation. So this code
could be implement to fitted other polynomial of degree n for a given set of
points.
7.(b) Curve fitting of the form y = a+bx+cx^2+dx^3

Code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(6);
cout.setf(ios::fixed);
int i,j,k,n,m;
cout<<"\nEnter the number of points to be entered\n";
cin>>m;
double p[m], q[m], temp, t;
cout<<"\nEnter the x values\n";
for(i=0; i<m; i++)
{
cin>>p[i];
}
cout<<"\nEnter the y values\n";
for(i=0; i<m; i++)
{
cin>>q[i];
}
n=3; //Set degree of polynomial
double a[n+1][n+2],x[n+1];
for(i=0; i<=n; i++)
{
//Calculate different element of the augmented matrix in a row
for(j=0; j<=n; j++)
{
a[i][j]=0;
for(k=0;k<m;k++)
{
a[i][j]=a[i][j]+pow(p[k],(i+j));
}
}
//Calculate last element of the augmented matrix in a row
a[i][n+1]=0;
for(k=0;k<m;k++)
{
a[i][n+1]=a[i][n+1]+pow(p[k],i)*q[k];
}
}
n=n+1;
//Do pivoting
for(k=0; k<n-1; k++)
{
for(i=k+1; i<n; i++)
{
if(fabs(a[k][k])<fabs(a[i][k]))
{
for(j=0; j<=n; j++)
{
temp=a[k][j];
a[k][j]= a[i][j];
a[i][j]=temp;
}
}
}
}
//Do the elementary row operation
for(k=0; k<n-1; k++)
{
for(i=k+1; i<n; i++)
{
t=a[i][k]/a[k][k];
for(j=0; j<=n; j++)
{
a[i][j]=a[i][j]-t*a[k][j];
}
}
}
//Back substitution
for(i=n-1; i>=0; i--)
{
x[i]=a[i][n];
for(j=i+1; j<n; j++)
{
if(j!=i)
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
}
cout<<"\nThe values of the coefficients are\n";
for(i=0; i<n; i++)
{
cout<<x[i]<<endl;
}
cout<<"\nThe required polynomial is\n";

cout<<"y="<<x[0]<<"+"<<x[1]<<"x+"<<x[2]<<"x^2+"<<x[3]<<"x^3"<<endl;
return 0;
}
Output

Enter the number of points to be entered


5

Enter the x values


0
1
2
3
4

Enter the y values


1
1.5
1.1
3.7
7.2

The values of the coefficients are


1.085714
0.178571
-0.257143
0.150000

The required polynomial is


y=1.085714+0.178571x+-0.257143x^2+0.150000x^3

Remark: Since the fitted polynomial obtained by the code is amost same as the
fitted polynomial that we found earlier by manual calculation. So this code
could be implement to fitted other polynomial of degree n for a given set of
points.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy