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

CBNST Lab

The document contains 5 practical assignments from a Computer Science term work submission on the topic of CBNST LAB (PMA-502). The practical assignments cover topics like error analysis in polynomial equations, root finding of algebraic and transcendental equations using bisection method, Newton's forward and backward interpolation formula, Gauss forward and backward interpolation formula, and implementing the regular falsi method to find roots of polynomial or transcendental equations. The term work is submitted by a student to fulfill the requirement for their 5th semester B.Tech degree.
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)
49 views

CBNST Lab

The document contains 5 practical assignments from a Computer Science term work submission on the topic of CBNST LAB (PMA-502). The practical assignments cover topics like error analysis in polynomial equations, root finding of algebraic and transcendental equations using bisection method, Newton's forward and backward interpolation formula, Gauss forward and backward interpolation formula, and implementing the regular falsi method to find roots of polynomial or transcendental equations. The term work is submitted by a student to fulfill the requirement for their 5th semester B.Tech degree.
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/ 16

A

Term-Work
On

CBNST LAB (PMA-502)

Submitted in partial fulfillment of the requirement for the V semester


B.Tech

By
Shyam Yadav
20011180
Faculty-in-Charge
Shashi Kumar Sharma
Assistant Professor

DEPARTMENT OF COMPUTER SCIENCE &ENGINEERING

GRAPHIC ERA HILL UNIVERSITY, BHIMTAL CAMPUS


2022-22
STUDENT’S DECLARATION

I, …………….., hereby declare the work, which is being presented in the term-work, entitled

“CBNST LAB “ in partial fulfillment of the requirement for the award of the degree B,Tech in

the session 2022-2022, is an authentic record of my own work carried out under the supervision

of Mr. Shashi Kumar Sharma.

The matter embodied in this term-work has not been submitted by me for the award of any other

degree.

Date: ………… ……………….

(Full signature of student)


CERTIFICATE

The term-work entitled “……………….” being submitted by…………………………….

S/o………….enrollment no………… Roll no……….to Graphic Era Hill University Bhimtal

Campus for the award of Bonafede work carried out by his/ her. He/She has worked under

my guidance and supervision and fulfilled the requirement for the submission of reports.

(…………………) (……………………)

Faculty-in-Charge (HOD, CSE Dept.)


PRACTICAL- 1

Objective- Write a program in to deduce error involved in


polynomial equation.

#include<iostream>
using namespace std;
#include<math.h>
int main()
{
double abs_err, rel_err, p_rel_err, t_val, a_val;
cout<<"\n INPUT TRUE VALUE: ";
cin>>t_val;
cout<<"\n INPUT APPROXIMATE VALUE: ";
cin>>a_val;
abs_err = fabs(t_val-a_val);
rel_err = (abs_err / t_val);
p_rel_err = (rel_err * 100);
cout<<"\nABSOLUTE ERROR= "<< abs_err;
cout<<"\nRELATIVE ERROR= "<<rel_err;
cout<<"\nPERCENTAGE RELATIVE ERROR= "<< p_rel_err;
return 0;
}
PRACTICAL- 2

Objective- Write a program to find out the root of the Algebric and
Transcendental equations using Bisection Method.
#include<bits/stdc++.h>
using namespace std;
#define EPSILON 0.01

double func(double x)
{
return x*x*x - x*x + 2;
}

void bisection(double a, double b)


{
if (func(a) * func(b) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}

double c = a;
while ((b-a) >= EPSILON)
{
c = (a+b)/2;

if (func(c) == 0.0)
break;
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
cout << "The value of root is : " << c;
}
int main()
{

double a, b;
cout<<"Enter the values: ";
cin>>a>>b;
bisection(a, b);
return 0;
}

PRACTICAL- 3
Objective- Write a program in to implement Newton’s Forward and
Backward Interpolation formula.
//forward
#include <bits/stdc++.h>
using namespace std;
float u_cal(float u, int n)
{
float temp = u;
for (int i = 1; i < n; i++)
temp = temp * (u - i);
return temp;
}

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

int main()
{
int n = 4;
float x[] = { 45, 50, 55, 60 };

float y[n][n];
y[0][0] = 0.7071;
y[1][0] = 0.7660;
y[2][0] = 0.8192;
y[3][0] = 0.8660;
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];
}

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;
}

float value = 52;


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;
}

//Backward
#include <bits/stdc++.h>
using namespace std;
float u_cal(float u, int n)
{
float temp = u;
for (int i = 1; i < n; i++)
temp = temp * (u + i);
return temp;
}

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

int main()
{
int n = 5;
float x[] = { 1891, 1901, 1911,1921, 1931 };

float y[n][n];
y[0][0] = 46;
y[1][0] = 66;
y[2][0] = 81;
y[3][0] = 93;
y[4][0] = 101;

for (int i = 1; i < n; i++) {


for (int j = n - 1; j >= i; j--)
y[j][i] = y[j][i - 1] - y[j - 1][i - 1];
}

for (int i = 0; i < n; i++) {


for (int j = 0; j <= i; j++)
cout << setw(4) << y[i][j]
<< "\t";
cout << endl;
}

float value = 1925;

float sum = y[n - 1][0];


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

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

PRACTICAL- 4
Objective- Write a program in to implement Gauss Forward and
Backward interpolation formula.

//Forward

#include <iostream>
using namespace std;
#include <math.h>
int main()
{
int n;
int i,j;
float ax[10];
float ay[10];
float x;
float nr,dr;
float y=0; float h;
float p;
float diff[20][20];
float y1,y2,y3,y4;

cout<<"Enter the number of terms - ";


cin>>n;
cout<<"Enter the value of x - ";
for (i=0;i<n;i++)
{
cin>>ax[i];
}
cout<<" \nEnter the value of y - ";
for(i=0;i<n;i++)
{
cin>>ay[i];
}
cout<<"\nEnter the value of x for which you want the value of y - ";

cin>>x;
h=ax[1]-ax[0];

for(i=0;i<n-1;i++)
{
diff[i][1]=ay[i+1]-ay[i];
}
for(j=2;j<=4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do
{
i++;
}
while(ax[i]<x);
i--;
p=(x-ax[i])/h;
y1=p*diff[i][1];
y2=p*(p-1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+1)*p*(p-1)*(p-2)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
cout<<"\nwhen x= "<<x<<"y="<<y;
return 0;
}

//Backward
#include <iostream>
using namespace std;
#include <math.h>
int main()
{
int n;
int i,j; float ax[10];
float ay[10];
float x;
float y=0;
float h;
float p;
float diff[20][20];
float y1,y2,y3,y4;

cout<<"\n Enter the number of terms - ";


scanf("%d",&n);
cout<<"\n Enter the value of x - ";
for (i=0;i<n;i++)
{
cin>>ax[i];
}
cout<<"\n\n Enter the value of y - ";
for(i=0;i<n;i++)
{
cin>>ay[i];
}
cout<<"\nEnter the value of x for which you want the value of y - ";
cin>>x;
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
{
diff[i][1]=ay[i+1]-ay[i];
}
for(j=2;j<=4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do
{
i++;
}
while (ax[i]<x);
i--;
p=(x-ax[i])/h;
y1=p*diff[i-1][1];
y2=p*(p+1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+2)*(p+1)*p*(p-1)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
cout<<"\nwhen x= "<<x<<" y= "<<y;
return 0;
}
PRACTICAL- 5
Objective- Write a program to implement of regular falsi method to
find the root of a polynomial or transdental equation.

// Regular Falsi

#include <bits/stdc++.h>
using namespace std;
#define f(x) (x*x-2*x-5)

const double eps = 0.1;

int main(){
cout << fixed << setprecision(5);
double a = 0, b = 4;
double y1, y2, x, y;
y1 = f(a);
y2 = f(b);
x = (a*y2-b*y1)/(y2-y1);
y = f(x);
int n=20;
while(n--){
if(y < 0){
a=x; y1=y;
}else if(y > eps){
b=x; y2=y;
}
// cout << a << " " << b << " " << y1 << " " << y2 << endl;
x = (a*y2-b*y1)/(y2-y1);
y = f(x);
}
cout << x << endl;
}

You might also like