0% found this document useful (0 votes)
25 views22 pages

Numerical Lab-Report

Uploaded by

dmaa357
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)
25 views22 pages

Numerical Lab-Report

Uploaded by

dmaa357
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/ 22

1

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Lab Report

Course Title: Numerical Method Lab


Course Code: CSE-312
Submitted by Submitted to
Name: Din Mohammad Al Amin Name: Mst. Jannatul Ferdous
Id: 201311044 Assistant Professor, Department of CSE,
Batch No:23th Varendra university
Semester: 7th
Section: A Name: Monika Kabir
Lecturer, Department of CSE, Varendra
Phone Num:01755182352 university

Date:10/04/2022
2

Index
Contents Page

1. Error Calculation …………………………………………….. 3-6

2. Bisection Method ……………………………………………. 7 - 11

3. False Position Method ………………………………………. 12-15

4. Iteration Method…………………………………………….... 16 - 20

5. Newton Raphson Method…………………………………….. 21 - 22

6. Newton Interpolation………………………………………….

 Newton Forward………………………………………. 22 - 23

 Newton Backward…………………………………….. 24 – 25

7. Linear Curve Fitting …………………………………………. 25 - 27


3

Experiment Name: Error Calculation

Question:

Code:

For Question-01
//Din Mohammad Al Amin Id-201311044

#include<bits/stdc++.h>
using namespace std;
int main(){

double Tm=146.2; // measured value


double T=145.9; // True value

double absolute_e =(Tm-T);


double relative_e =(absolute_e/T);

cout<<"Absolute error is : "<<absolute_e<<endl;


cout<<"Relative error is : "<<relative_e<<endl;
return 0;
4

Output:

For Question-02
//Din Mohammad Al Amin Id-201311044

#include<bits/stdc++.h>
using namespace std;
int main(){
double num=8.6; //so n=1;
int n=1;

double x = (0.5 * pow(10,-n));

double ans =(x/num);

cout<<"The relative error is: "<< ans<<endl;

return 0;
}

Output:
5

For Question-03
//Din Mohammad Al Amin Id-201311044

#include<bits/stdc++.h>
using namespace std;
int main(){
float x;
cout<<"Sample input : ";
cin>>x;
stringstream ss;
ss << abs(x-(int)x);
string s;
ss >> s;

cout<<"Sample output = "<<s.length()-2<<endl;


return 0;
}

Output:

For Question-04

//Din Mohammad Al Amin Id-201311044


#include<bits/stdc++.h>
using namespace std;
int main(){
double x=1.5,ans;
ans = pow(x,3) + (5*x) -1;
cout<<"a.f(x)="<<ans << endl;

// b
int x1 = 1;
6

double result = exp(x1)-1;


cout<<"b.f(x)="<<result << endl;

return 0;
}

Output:

Experiment Name: Bisection Method


Question:

Code:
For question-01

#include<iostream>
#include<iomanip>
#include<math.h>
7

//define a function
#define f(x) x*x*x - 3*x -5
using namespace std;

int main(){
//declaring required variable
float x0, x1, x, f0, f1, f, e;
int step = 1;

cout<<setprecision(6)<<fixed;

//Inputs
up:
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>> e;

//calculating functional value


f0 = f (x0);
f1 = f (x1);

if(f0 * f1 > 0.0){


cout<<"Incorrect initial guesses." << endl ;
goto up;

//Implementing bisection bethod


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

}
while(fabs(f) > e);
cout<<endl<<"Root is: "<< x << endl;

return 0;

Output:

For question-02

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

//define a function
#define f(x) x*exp(x) - 1
using namespace std;
int main(){
//declaring required variable
float x0, x1, x, f0, f1, f, e;
9

int step = 1;

cout<<setprecision(6)<<fixed;

//Inputs
up:
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>> e;

//calculating functional value


f0 = f (x0);
f1 = f (x1);

if(f0 * f1 > 0.0){


cout<<"Incorrect initial guesses." << endl ;
goto up;

//Implementing bisection bethod


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<<endl<<"Root is: "<< x << endl;

return 0;

}
10

Output:

Experiment Name: False Position Method

Question:

Code:

For Question-02
11

#include<stdio.h>
#include<math.h>

double F( double x)
{
return (x*x*x - 2*x -5);
}

int main()
{
double x0,x1;

printf("Enter the 1st root : ");


scanf("%lf",&x0);

printf("\nEnter the 2nd root : ");


scanf("%lf",&x1);

int iter;
printf("\n\nEnter the number of iteration : ");
scanf("%d",&iter);

int c=1;
double l1=x0;
double l2=x1;
double r,f1,f2,f3;

if(F(l1)==0)
r=l1;
else if(F(l2)==0)
r=l2;
else
{
while(c<=iter)
{

f1=F(l1);
r=((l1*F(l2))-(l2*F(l1)))/(F(l2)-F(l1));
f2=F(r);
f3=F(l2);

if(f2==0)
{
r=f2;
12

break;
}
printf("The root after %d iteration is %lf \n\n",c,r);

if(f1*f2<0)
l2=r;
else if(f2*f3<0)
l1=r;
c++;
}
}

printf("\n\n\nSo, the approximation to the root is %lf\n",r);

return 0;
}

Output:
13

Experiment Name: Iteration Method

Code:

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

//Define function f(x)

#define f(x) sin(x)-10*x+10

//Formeting x= g(x)
#define g(x) (10+sin(x))/10

using namespace std;

int main()
{
int step=1, N;
float x0, x1, e;

// Setting precision and writing floating point values in fixed-point


notation.
cout<< setprecision(6)<< fixed;
/* Inputs */
cout<<"Enter initial guess: ";
cin>>x0;
14

cout<<"Enter tolerable error: ";


cin>>e;

cout<<"Enter maximum iteration: ";


cin>>N;

//Implementing Fixed Point Iteration


cout<<endl;

do
{
x1 = g(x0);
cout<<"Iteration-"<< step<<":\t x1 = "<< setw(10)<< x1<<" and
f(x1) = "<< setw(10)<< f(x1)<< endl;

step = step + 1;

if(step>N)
{
cout<<"Not Convergent.";
exit(0);
}

x0 = x1;

}while( fabs(f(x1)) > e);

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

return(0);
Output:

Experiment Name: Newton Raphson


15

Code:

#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define f(x) 3*x - cos(x) -1
#define g(x) 3 + sin(x)
using namespace std;
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;
cout<< endl<<"*********************"<< endl;
cout<<"Newton Raphson Method"<< endl;
cout<<"*********************"<< endl;
do
{
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
16

{
cout<<"Mathematical Error.";
exit(0);
}
x1 = x0 - f0/g0;
cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x1<<" and f(x) = "<< setw(10)<< f(x1)<<
endl;
x0 = x1;
step = step+1;
if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}

f1 = f(x1);

}while(fabs(f1)>e);

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


return 0;
}

Output:
17

Experiment Name: Newton Interpolation

Newton Forward Interpolation:

Code:

// newton forward interpolation


#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;
}
// calculating factorial of given number n
int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}

int main()
{
// Number of values given
int n = 4;
float x[] = { 1, 3, 5, 7 };

// y[][] is used for difference table


// with y[][0] used for input
float y[n][n];
y[0][0] = 24;
y[1][0] = 120;
y[2][0] = 336;
y[3][0] = 720;

// Calculating the forward difference


// table
18

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

// Displaying the forward difference table


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

// Value to interpolate at
float value = 8;

// initializing u and sum


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

Output:
19

Experiment Name: Newton Backward interpolation:

Code:
// newton backward interpolation
#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()
{
// number of values given
int n = 4;
float x[] = { 1, 3, 5, 7 };

// table and y[][0] used for input


float y[n][n];
y[0][0] = 24;
y[1][0] = 120;
y[2][0] = 336;
y[3][0] = 720;

// Calculating the backward difference table


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];
20

// Displaying the backward difference table


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

// Value to interpolate at
float value = 150;

// Initializing u and sum


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

Output:
21

Experiment Name: Linear Curve Fitting

Question: Certain experimental values of x and y are given below:


(0,-1), (2,5), (5,12), (7,20)
if the straight-line Y=a0 +a1x is fitted to the above data, find the value of a0 and a1?

Code:

//Din Mohammad Al Amin id-201311044

#include<iostream>

#define S 50

using namespace std;


int main()
{
int n, i;
float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0, a, b;

/* Input */
cout<<"Enter The Number of n ";
cin>>n;

cout<<"Enter data:"<< endl;

for(i=1;i<=n;i++)
{
cout<<"x["<< i <<"] = ";
cin>>x[i];
cout<<"y["<< i <<"] = ";
cin>>y[i];
}

/* Calculating Required Sum */


for(i=1;i<=n;i++)
{
sumX = sumX + x[i];
sumX2 = sumX2 + x[i]*x[i];
22

sumY = sumY + y[i];


sumXY = sumXY + x[i]*y[i];
}
/* Calculating a and b */
b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
a = (sumY - b*sumX)/n;

/* Displaying value of a and b */


cout<<"Calculated value of a is "<< a << "and b is "<< b << endl;
cout<<"Equation of best fit is: y = "<< a <<" + "<< b<<"x";

return(0);
}

Output:

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