0

I'm having trouble passing the updated values of initial_guess_1 and initial_guess_2 back into the GoldenSectionSearch method (I've attempted to have the method call itself until the terminating condition associated with the "if" is satisfied). My attempts at resolving this myself were influenced by what I found here:

https://softwareengineering.stackexchange.com/questions/286008/parameters-are-passed-by-value-but-editing-them-will-edit-the-actual-object-li

http://www.java-tutorial.com/java-tutorial/java-classes-methods/java-call-reference/

I'm still unable to figure out what I am doing wrong in trying to pass updated values back into the method. What am I doing wrong and how do I fix it? I'm a novice at programming and would like things broken down into the smallest,most easily understood terms possible.

public static double GoldenSectionSearch(double x_1, double x_2, double initial_guess_1,double initial_guess_2 ,double gradient_x, double gradient_y, double a,double a_0,double a_1,double a_2,double b_0,double b_1,double p,double N){

          //Stuff

         if(Math.abs((100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1) + (1- x_1)*(1- x_1)) - (100*(initial_guess_2 - initial_guess_1*initial_guess_1)*(initial_guess_2 - initial_guess_1*initial_guess_1) + (1- initial_guess_1)*(1- initial_guess_1)) )/Math.abs(1+(100*(initial_guess_2 - initial_guess_1*initial_guess_1)*(initial_guess_2 - initial_guess_1*initial_guess_1) + (1- initial_guess_1)*(1- initial_guess_1))) >=0.50*Math.pow(10,-6)){

                  initial_guess_1=x_1;
                  initial_guess_2=x_2;
                  //Double Boxed_a = new Double(a);
                  Double Boxed_initial_guess_1 = new Double(initial_guess_1);
                  Double Boxed_initial_guess_2 = new Double(initial_guess_2);


                  initial_guess_1.GoldenSectionSearch(initial_guess_1);
                  initial_guess_2.GoldenSectionSearch(initial_guess_2);
                  gradient_x = (400*initial_guess_1*(initial_guess_1*initial_guess_1 - initial_guess_2) + 2*(initial_guess_1 -1));
                  gradient_y = (200*(initial_guess_2 - initial_guess_1*initial_guess_1));

                  GoldenSectionSearch(initial_guess_1,initial_guess_2,x_1, x_2, gradient_x, gradient_y, a, a_0, a_1, a_2, b_0, b_1, p,N);      


         } else{



               double f_x=(100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1) + (1- x_1)*(1- x_1));
               System.out.print("alpha = " + a + "    " + "x_1 = " + x_1 + "    " + "x_2 = " + x_2 + "    " + "f_x = " + f_x);
         }



        // Double Boxed_a = new Double(a);


         return x_2; 

      }
Jakolcz
  • 564
  • 2
  • 12
  • 32

1 Answers1

0

You can't modify primitives. You have to create wrapper class with getters/setters

public class MyWrapperObject {

    private double x_1;
    private double x_2;
    private double initial_guess_1;
    private double initial_guess_2;
    private double gradient_x;
    private double gradient_y;
    private double a;
    private double a_0;
    private double a_1;
    private double a_2;
    private double b_0;
    private double b_1;
    private double p;
    private double N;

    public double getX_1() {
        return x_1;
    }

    public void setX_1(double x_1) {
        this.x_1 = x_1;
    }
    /* The rest of getters/setters */
}

than pass it to your method and use it like

public static double GoldenSectionSearch(MyWrapperObject obj){
        // ...
        double initial_guess_1 = obj.get_initial_guess_1();
        double initial_guess_2 = obj.get_initial_guess_2();
        obj.setGradient_x(400*initial_guess_1*(initial_guess_1*initial_guess_1 - initial_guess_2) + 2*(initial_guess_1 -1));
        // ...
    }

There is also a question here on SO whether java is pass-by-reference or pass-by-value https://stackoverflow.com/a/40523/2022162

Jakolcz
  • 564
  • 2
  • 12
  • 32