Errors (Round-Off & Truncation Error)
Objective : - To understand the numerical errors, those occur in the numerical calculations and to compute the Absolute & Relative Errors
Theory : -
Numerical errors arise during computations due to round-off errors and truncation errors.
True Value = Approximation + Error
Error(E) = True Value - Approximation
This Error is called Absolute Error, which is the magnitude of the difference between true value & the approximate value.
Normalized Error(e) = Absolute Error/True Value
Round-off Errors :-
Round-off error is the difference between an approximation of a number used in computation and its exact (correct) value. In certain types of computation, round-off error can be magnified as any initial errors are carried through one or more intermediate steps.
Round-off error occurs because computers use fixed number of bits to store the value of numbers . In a numerical computation round-off errors are introduced at every stage of computation. Hence though an individual round-off error due to a given number at a given numerical step may be small but the cumulative effect can be significant.
Truncation Errors :-
In numerical analysis and scientific computing, truncation error is the error made by truncating an infinite sum and approximating it by a finite sum. For instance, if we approximate the sine function by the first two non-zero term of its Taylor series, as in for small , the resulting error is a truncation error. Truncation error refers to the error in a method, which occurs because some series (finite or infinite) is truncated to a fewer number of terms. Such errors are essentially algorithmic errors and we can predict the extent of the error that will occur in the method.
Computational Error = Roundoff Error + Truncation Error
Program 1: To compute the Round Off Error
(Program to Calculate the Surface Area of the Earth)
Algorithm: (C++ Program)
Step 1: Include the Header Files needed for the program
Step 2: Start the main() Function
Step 3: Declare the Variables Sc, St, Pi, R, Aer, and Rer & Initilize the St = 510072000, R = 6371.
Step 4: Read the Value of Pi
Step 5: Compute the Value of Sc = 4*Pr*R*R
Step 6: Compute the Aer = St-Sc & Rer = Aer/St
Step 7: Display the Values of St, St, Aer & Rer
C++ Program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
clrscr( );
double Sc, St = 510072000, Pi, R = 6371, Aer, Rer;
cout<<"Enter the value of Pi:"<<endl;
cin>>pi;
Sc = 4*Pi*R*R;
Aer = St - Sc;
Rer = Aer/St;
cout<<"\n The True Surface Area of Earth is"<<setprecision(10)<<st<<"sq.km";
cout<<"\n The Computed Surface Area of Earth is"<<setprecision(10)<<sc<<"sq.km";
cout<<"\n The value of pi entered is "<<Pi;
cout<<"\n The Absolute Error is"<<Aer;
cout<<"\n The Relative Error is"<<setprecision(10)<<Rer;
getch( );
}
Output:
Program 2: To compute the Truncation Error
(Program to Calculate the Truncation Error in Logarithmic Function)
Algorithm: (C++ Program)
Step 1: Include the Header Files needed for the program
Step 2: Declare & Define the Function to calculate the factorial of a number
Declare & Initialize fact = 1
for i = 1 to n, loop fact = fact*i
return fact
Step 3: Start the main function and declare the variables, (initilize the variables)
Step 4: Compute the Logarithmic value of function using power of function and factorial function with in a for loop
Step 5: Compute the Absolute Error, Relative Error
Step 7: Display the Values of Computed Logarithmic Value, Actual Logarithmic Value, Absolute Error & Relative Error.
C++ Program:
include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
//Factorial Function
int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
//Main Function
void main()
{
char c;
int x=1,n=1;
long double s=1;
do
{
clrscr();
s=1;
cout<<"\n Enter the Value of x:";
cin>>x;
cout<<"\n Enter the Value of n:";
cin>>n;
for(int i=1; i<=n; i++)
{
s=s+(pow(x,i)/fact(i));
}
cout<<" \n The Computed Exponential Value of "<<x<<" is "<<setprecision(10)<<s;
cout<<" \n The Actal Exponential Value of "<<exp(x);
cout<<" \n The Absolute Error is: "<<exp(x)-s;
cout<<" \n The Relatiove Error is: "<<(exp(x)-s)/exp(x);
cout<<" \n Do You Want to Continue...(Y/N)";
cin>>c;
}while(c == 'y');
}