-1

How to solve following mathematic equation in java?

Equation: x + sin(x) = constant, where x is variable. I encountered this equation after 18 years. I forgot this basic concept. Please help me on this basic high school question.

I tried to code above equation x + sin(x) = constant as following, however, it is giving wrong answer. Please let me know where i am wrong.

    public double balanceLength(double total_weight) {
        // 5.00 assume inical value of x
        return newtonRaphson( 5.00,  total_weight);
    }
    private static double derivFunc(double x)
    {
        return sin(x) + x;
    }
    private static double func(double x, double weight)
    {
        return sin(x) + x - weight;
    }
    static double newtonRaphson(double x, double weight)
    {

        double h = func(x, weight) / derivFunc(x);
        while (abs(h) >= EPSILON)
        {
            h = func(x, weight) / derivFunc(x);
            x = x - h;
        }

        return round(x * 100.0) / 100.0 ;
    }
Neelabh Singh
  • 2,600
  • 12
  • 51
  • 88
  • 4
    This is not a `math` forum :( – DOOM Dec 23 '18 at 07:50
  • 1
    Hint: The function `f(x)=x + sin(x)` is increasing because its derivative is positive or zero, therefore if `f(y) - constant > 0` there is no solution for x >y –  Dec 23 '18 at 08:18
  • 2
    You can use Newton's method (As we're on SO). Also: http://math.stackexchange.com/ – IsaacLevon Dec 23 '18 at 09:08
  • transcendental equations are solvable only numerically or by approximation if no special case or some identity can convert it to polynomial one ...see [How approximation search works](https://stackoverflow.com/q/36163846/2521214) however this case is very similar to [Solving Kepler's equation `M=E-e*sin(E)`](https://stackoverflow.com/a/25403425/2521214) so simple iteration might do ... – Spektre Dec 23 '18 at 10:33
  • See [this](https://introcs.cs.princeton.edu/java/13flow/Sin.java.html) : when `x` is expressed in radians `sin(x)+x` can be approximated by `2x - x^3/3! + x^5/5! - x^7/7! + ...` – c0der Dec 23 '18 at 11:53
  • 1
    You got the derivation wrong. should be `1 + cos(x)`. – Henry Dec 23 '18 at 12:40

1 Answers1

1

This is a very basic implementation, only partially tested. It reruns x in radians, which satisfies y=six(x) +x for a given y :

//returned value in radians
static double evaluateSinxPlusx(double y){

    double delta = y>0 ?  0.01 : -0.01 ;//change constants
    double epsilon = 0.01;            //to change
    int iterations = 100;             //accuracy

    double x = 0;
    double sum = 1;

    while(Math.abs(y - sum) > epsilon) {
        x+=delta;
        //based Taylor series approximation
        double term = 1.0;
        sum  = x;
        double d = 1;
        for (int i = 1; i< iterations; i++) {
             term = Math.pow(x, i);
             d*=i;
             if (i % 4 == 1) {

                sum += term/d;
            }
             if (i % 4 == 3) {
                sum -= term/d;
            }
         }
    }
    return x;
}

//test it 
public static void main(String[] args) throws Exception{

    double y = 0.979;
    //expected x = 0.5 radians
    System.out.println("for x="+ evaluateSinxPlusx(y)+"(radians), sin(x)+x = "+ y);

    y = -0.979;
    //expected x = - 0.5 radians
    System.out.println("for x="+ evaluateSinxPlusx(y)+"(radians), sin(x)+x = "+ y);

    y = 0.33256;
    //expected x = 0.16666 radians
    System.out.println("for x="+ evaluateSinxPlusx(y)+"(radians), sin(x)+x = "+ y);
}

This is not a robust implementation and should be used as demo only.

c0der
  • 18,467
  • 6
  • 33
  • 65