FOR DERIVATIVE THIS IS THE FORMULA double derivative(double (*)(double), double,
ID: 3683798 • Letter: F
Question
FOR DERIVATIVE THIS IS THE FORMULA
double derivative(double (*)(double), double, double);
double derivative(double (*pf)(double t), double x, double h)
{
return ((*pf)(x+h) - (*pf)(x-h))/(2*h);
}
FOR INTEGRATION THIS IS THE FORMULA
double riemann(double (*)(double), double, double, int)
double riemann(double (*pf)(double t), double a, double b, int n)
{
double s = 0, h = (b-a)/n, x;
int i;
for (x = a, i = 0; i < n; x += h, i++)
s += (*pf)(x);
return s*h;
}
Explanation / Answer
/** C++ code to demostrate differentiatioon and integration of y =x^3 ***/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
void interval (double a[])
{
cout << "Low: ";
cin >> a[0];
cout << "High: ";;
cin >> a[1];
}
double cube (double t) // function x^3
{
return t*t*t;
}
double derivative(double (*pf)(double t), double x, double h)
{
return ((*pf)(x+h) - (*pf)(x-h))/(2*h);
}
double riemann(double (*pf)(double t), double a, double b, int n)
{
double s = 0, h = (b-a)/n, x;
int i;
for (x = a, i = 0; i < n; x += h, i++)
s += (*pf)(x);
return s*h;
}
int main()
{
while(1)
{
double a[2];
int choice;
cout << "Select Function: ";
cout << "1.Interval 2.Integration 3.Derivation ";
cin >> choice;
if (choice == 1)
{
interval(a);
}
else if(choice == 2)
{
int n;
cout << "Enter n: ";
cin >> n;
cout << "Value for function x^3: "<< riemann(cube,a[0],a[1],n)<< endl;
}
else if(choice == 3)
{
double x,h;
cout << "Enter x: ";
cin >> x;
cout << "Enter h: ";
cin >> h;
cout << "Value for function x^3: "<< derivative(cube,x,h)<< endl;
}
else break;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.