Numerical Lab-Report
Numerical Lab-Report
Lab Report
Date:10/04/2022
2
Index
Contents Page
4. Iteration Method…………………………………………….... 16 - 20
6. Newton Interpolation………………………………………….
Newton Forward………………………………………. 22 - 23
Newton Backward…………………………………….. 24 – 25
Question:
Code:
For Question-01
//Din Mohammad Al Amin Id-201311044
#include<bits/stdc++.h>
using namespace std;
int main(){
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;
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;
Output:
For Question-04
// b
int x1 = 1;
6
return 0;
}
Output:
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;
}
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;
}
while(fabs(f) > e);
cout<<endl<<"Root is: "<< x << endl;
return 0;
}
10
Output:
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;
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++;
}
}
return 0;
}
Output:
13
Code:
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
//Formeting x= g(x)
#define g(x) (10+sin(x))/10
int main()
{
int step=1, N;
float x0, x1, e;
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;
return(0);
Output:
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);
Output:
17
Code:
int main()
{
// Number of values given
int n = 4;
float x[] = { 1, 3, 5, 7 };
// Value to interpolate at
float value = 8;
cout << "\n Value at " << value << " is "
<< sum << endl;
return 0;
}
Output:
19
Code:
// newton backward interpolation
#include <bits/stdc++.h>
using namespace std;
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 };
// Value to interpolate at
float value = 150;
cout << "\n Value at " << value << " is "
<< sum << endl;
return 0;
}
Output:
21
Code:
#include<iostream>
#define S 50
/* Input */
cout<<"Enter The Number of n ";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"x["<< i <<"] = ";
cin>>x[i];
cout<<"y["<< i <<"] = ";
cin>>y[i];
}
return(0);
}
Output: