user466534
user466534

Reputation:

Gradient descend algorithm

Here is my work on gradient descend algorithm

#include<iostream>
#include<math.h>
#include<float.h>
using namespace std;
double f_prime(double x)
{

    return (double)(4.0*powf(x,3)-9.0*powf(x,2));


}
void gradient()
{
    double x_old=0;
    double x_new=6;
    double eps=0.01;
    double precision=0.00001;
    while(abs(x_new-x_old)>precision)

    {
        x_old=x_new;
    x_new=x_old-eps*f_prime(x_old);


    }
    cout<<" local minimum at : "<<x_new<<endl;

}
int main()
    {

        gradient();



        return 0;
}

The above code gives me warnings of a non correct conversion from double to float, possible loss of data, so as a result it gives me some undefined values like -1.IND. Can anyone explain why this is?

Upvotes: 1

Views: 225

Answers (2)

Thomas W.
Thomas W.

Reputation: 2224

abs is defined only for int and long types. For floating point numbers use fabs.

Upvotes: 2

Andreas Brinck
Andreas Brinck

Reputation: 52519

Change powf to pow although I'm not sure that will solve your problem.

Upvotes: 1

Related Questions