Derivative
This commit is contained in:
parent
7750dbddb1
commit
f67da39fd4
BIN
derivative
Executable file
BIN
derivative
Executable file
Binary file not shown.
39
derivative.cpp
Normal file
39
derivative.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
double derivative(double (*f)(double), double x, double epsilon) {
|
||||||
|
return (f(x + epsilon) - f(x)) / epsilon;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool test_interval(double epsilon, double precision) {
|
||||||
|
for (double x = 0; x <= 3.14; x += 0.0001) {
|
||||||
|
double approx = derivative(sin, x, epsilon);
|
||||||
|
double precise = cos(x);
|
||||||
|
if (abs(approx - precise) > precision) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
for (double e = 0.003; e > 0.001; e -= 0.000001) {
|
||||||
|
if (test_interval(e, 0.001)) {
|
||||||
|
cout << "Epsilon found: " << e << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double max_diff;
|
||||||
|
double max_x;
|
||||||
|
for (double x = 0; x <= 3.14; x += 0.0001) {
|
||||||
|
double diff = abs(derivative(sin, x, 0.002) - cos(x));
|
||||||
|
if (diff > max_diff) {
|
||||||
|
max_diff = diff;
|
||||||
|
max_x = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << "Max diff at " << max_x << " of " << max_diff << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user