Write a C++ program that calculates the derivative using three methods (the forw
ID: 3747042 • Letter: W
Question
Write a C++ program that calculates the derivative using three methods (the forward derivative, backward derivative, and the centered derivative) of a function that is defined separately from the main() part of the code. Have the user specify the value of , but print out the results for different step sizes: 01, 0.01, 0.001, and 0.0001 Use this code to calculate the derivatives of ( and sinh r for three values of 0.0,1.5, and 10.0. Use the smallest step size of the centered derivative to define the "correct" value (this is assuming no round-off error, of course), and determine the percent accuracy of each method/step size. Note: do not compare your result to the analytic result!Explanation / Answer
Refer the below code:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define f(x) (x-1)*(x-1)*(x-1)*(x-1)
#define g(x) sinh(x)
ld base;
ld h1 = 0.1,h2=0.01,h3=0.001,h4=0.0001;
ld fd(ld d,ld h)
{
ld fd_f = (f(d+h) - f(d))/h;
// cout << setprecision(10) << fd_f << endl;
return fd_f;
}
ld bd(ld d,ld h)
{
ld bd_f = (f(d) - f(d-h))/h;
// cout << setprecision(10) << bd_f << endl;
return bd_f;
}
ld cd(ld d,ld h)
{
ld cd_f = (f(d+h) - f(d-h))/(2*h);
// cout << setprecision(10) << cd_f << endl;
return cd_f;
}
void calulate_derivative(ld x,ld h)
{
ld a,b,c;
a = fd(x,h);
b = bd(x,h);
c = cd(x,h);
cout << setprecision(10);
cout << "Step size: " << h << endl;
ld dif;
dif = abs(base - a);
cout << "Forward Derivative = " << a << " Accuracy: " << 100 - abs(dif/base)*100 << endl;
dif = abs(base - b);
cout << "Backward Derivative = " << b << " Accuracy: " << 100 - abs(dif/base)*100 << endl;
dif = abs(base - c);
cout << "Centered Derivative = " << c << " Accuracy: " << 100 - abs(dif/base)*100 << endl;
}
void input(ld x)
{
cout << x << endl;
base = cd(x,h4);
calulate_derivative(x,h1);
calulate_derivative(x,h2);
calulate_derivative(x,h3);
calulate_derivative(x,h4);
}
ld fd_sinh(ld d,ld h)
{
ld fd_f = (g(d+h) - g(d))/h;
// cout << setprecision(10) << fd_f << endl;
return fd_f;
}
ld bd_sinh(ld d,ld h)
{
ld bd_f = (g(d) - g(d-h))/h;
// cout << setprecision(10) << bd_f << endl;
return bd_f;
}
ld cd_sinh(ld d,ld h)
{
ld cd_f = (g(d+h) - g(d-h))/(2*h);
// cout << setprecision(10) << cd_f << endl;
return cd_f;
}
void calulate_derivative_sinh(ld x,ld h)
{
ld a,b,c;
a = fd_sinh(x,h);
b = bd_sinh(x,h);
c = cd_sinh(x,h);
cout << setprecision(10);
cout << "Step size: " << h << endl;
ld dif;
dif = abs(base - a);
cout << "Forward Derivative = " << a << " Accuracy: " << 100 - abs(dif/base)*100 << endl;
dif = abs(base - b);
cout << "Backward Derivative = " << b << " Accuracy: " << 100 - abs(dif/base)*100 << endl;
dif = abs(base - c);
cout << "Centered Derivative = " << c << " Accuracy: " << 100 - abs(dif/base)*100 << endl;
}
void input_sinh(ld x)
{
cout << x << endl;
base = cd_sinh(x,h4);
calulate_derivative_sinh(x,h1);
calulate_derivative_sinh(x,h2);
calulate_derivative_sinh(x,h3);
calulate_derivative_sinh(x,h4);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ld x;
cout << "Derivative of (x-1)^4:" << endl;
cin >> x;
input(x);
input(0);
input(1.5);
input(10);
cout << "Derivative of sinh x:" << endl;
cin >> x;
input_sinh(x);
input_sinh(0);
input_sinh(1.5);
input_sinh(10);
return 0;
}
Here fd,bd,cd are functions for forward derivative,backward derivative and centered derivative respectively and functions with _sinh are for derivative of sinh(x).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.