Skip to content

Instantly share code, notes, and snippets.

@Yegorsh
Last active January 5, 2022 21:08
Show Gist options
  • Select an option

  • Save Yegorsh/5159fde52fd5f0f2413e0d10db73d872 to your computer and use it in GitHub Desktop.

Select an option

Save Yegorsh/5159fde52fd5f0f2413e0d10db73d872 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
using namespace std;
double f(double x){
return sin(x);
}
double rightDerivative (double x, double h){
double rightDer = (f(x+h) - f(x))/h;
return rightDer;
}
double centralDerivative (double x, double h){
double centralDer = (f(x+h) - f(x-h)) / (2*h);
return centralDer;
}
double secondDerivative (double x, double h){
double secondDer = (rightDerivative(x+h, h) - rightDerivative(x, h)) / h;
return secondDer;
}
int main(){
const double epsilon = 0.01;
cout << "epsilon = " << epsilon << endl;
double a, b;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
double length = b - a;
int step = length/epsilon;
cout << "segment = " << step << endl;
double x = a;
cout << "x" << "\t\t\t\t" << "f(x)" << "\t" << "Правая производная" << "\t" << "Центральная производная" << "\t" << "Вторая производная" << endl;
for (int i=1; i<step; i++){
cout << x + i*epsilon << "\t\t" << f(x + i*epsilon) << "\t\t" << rightDerivative(x + i*epsilon, epsilon) << "\t\t\t\t" << centralDerivative(x + i*epsilon, epsilon) << "\t\t\t" << secondDerivative(x + (i-1)*epsilon, epsilon) << endl;
}
/*cout << "x = " << x << endl;
cout << "f(x) = " << f(x) << endl;
cout << "Правая производная = " << rightDerivative(x, epsilon) << endl;
cout << "Центральная производная = " << centralDerivative(x, epsilon) << endl;
cout << "Вторая производная = " << secondDerivative(x, epsilon) << endl;*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment