Last active
January 5, 2022 21:08
-
-
Save Yegorsh/5159fde52fd5f0f2413e0d10db73d872 to your computer and use it in GitHub Desktop.
Find more at https://solutionarium.notion.site/1st-and-2nd-Derivative-25b7177903b643cf8e197632e1ca1f57
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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