Skip to content

Instantly share code, notes, and snippets.

@kuzemkon
Created November 27, 2014 19:31
Show Gist options
  • Select an option

  • Save kuzemkon/42f1ddb4632f91d5a63d to your computer and use it in GitHub Desktop.

Select an option

Save kuzemkon/42f1ddb4632f91d5a63d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
using namespace std;
float a, b, E, Dx, Ep, x;
int i;
float f(float x) {
return tan(x) - ((tan(x)*tan(x)*tan(x) + 1) / 3) + 0.2*tan(x)*tan(x)*tan(x)*tan(x)*tan(x);
}
float fp(float x) {
return (f(x + Dx) - f(x)) / Dx;
}
float f2p(float x) {
return (fp(x + Dx) - fp(x)) / Dx;
}
int main() {
cout << "Enter limit a, b and accuracy E\n";
cout << "a =";
cin >> a;
cout << "b =";
cin >> b;
cout << "E =";
cin >> E;
Dx = E / 1000;
Ep = fabs(x - b);
x = b;
if (f(x)*f2p(x)<0) {
cout << "Tabulate the function, please/n";
system("pause");
}
else {
i = 0;
while (fabs(Ep) > E) {
x = x - f(x) / fp(x);
Ep = fabs(x - b);
b = x;
i = i + 1;
cout << i << " x =" << x;
cout << " f(x) =" << f(x) << endl;
}
}
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment