This question already has an answer here:
- Compile errors using std::bind in gcc 4.7 2 answers
I attempt to try my snippet of Newton's method on a multivariate function and used std::bind
and std::function
. But I was stuck on an error
error: conversion from 'std::_Bind_helper&, int>::type {aka std::_Bind, int))(double, double, double)>}' to non-scalar type 'std::function' requested
What does this error message mean and how should I fix my current code?
#include <iostream>
#include<functional>
#include<cmath>
double newton(std::function<double(double)> F, std::function<double(double)> f,
double x=0, int maxiter=1000, double epsilon=0.001)
{
int n = 0;
while((n < maxiter) && (fabs(F(x)) > epsilon))
{
x = x - F(x) / f(x);
n++;
}
return x;
}
// I'd like to fix x and z at 1 and 2 and find root for y
double ftest(double x, double y, double z)
{
return x * x + (y * y - 2 * y - 4) + z * z;
}
// Partial derivative of ftest with regards to y
double ftest1(double y)
{
return 2 * y - 2;
}
int main()
{
using namespace std::placeholders;
std::function<double(double)> F = std::bind(ftest, 1, _2, 2);
std::function<double(double)> f = ftest1;
std::cout << newton(F, f);
return 0;
}