Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <iostream>
- #include <functional>
- double function_(double x) {
- return x*x-10*std::sin(x)*std::sin(x)+2 ;
- }
- double derivative(double x) {
- return 2*x-10*std::sin(2*x);
- }
- double NewtonsMethod(std::function<double(double x)> f, std::function<double(double x)> df, double xn, double eps) {
- double x1 = xn - f(xn) / df(xn);
- double x0 = xn;
- while (abs(x0 - x1) >= eps) {
- if (df(x1) == 0) break;
- x0 = x1;
- x1 = x1 - f(x1) / df(x1);
- }
- return x1;
- }
- int main() {
- const double A = -3;
- const double B = 2;
- double eps;
- std::cin >> eps;
- double h;
- std::cin >> h;
- for (double it = A; it <= B; it += h) {
- if (function_(it)*function_(it+h) < 0){
- std::cout << NewtonsMethod(function_, derivative, it, eps) << std::endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement