Question: Please help I want to know if my code is correct USING C++ Design and construct a computer program in (C++) that will illustrate the
Please help I want to know if my code is correct
USING C++
Design and construct a computer program in (C++) that will illustrate the effects of rounding errors and truncation errors. Also, note that the arguments of all trigonometric functions in this course are measured in radians.
The following is a plot of the function f(x) = sin(x3) + x2
In order to illustrate the effects of the two major error sources, rounding and truncation, attempt to determine an approximation to the derivative of f(x) at x = 1.0 radians using the difference approximation given below. (The true answer is 2 + 3 cos(1) or about 3.620906917604419152202810).
Use the formula:
f'(x) (f(x+h) - f(x)) / h
with h=1, 0.5, 0.25, ... 1.8189894035459e-12
(i.e., keep halving h until it is less than 2.0e-12.)
Print out the values of h, your approximation to f'(x), and the error in the approximation for each value of h used. This error will include the effects of both truncation and rounding.
#include
#include
#include
using namespace std;
double f(double x){
return(sin(x*x*x)+(x*x));
}
int main()
{
double x=1;
double h=1;
double ff,ffr=0,fft=0;
ff=(f(x+h)-f(x))/h;
ffr=round(ff);
fft=trunc(ff);
cout<<"h"<<" "<<"f'(x)"<<" "<<"round error approx"<<" "<<"truncation error approx";
cout<<" ";
while(h>=2.0e-12){
h=h/2;
ff=(f(x+h)-f(x))/h;
cout< ffr=round(ff); fft=trunc(ff); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
