K
K
Konstantin2013-12-15 19:12:59
Java
Konstantin, 2013-12-15 19:12:59

Can't reach xrez, yrez variables in nested if?

It is not possible to access the xrez, yrez variables in the nested if:

public class GradMethod {
    public double XREZ;
    public double YREZ;
    GuiLab2 accessObj = new GuiLab2();
    double x0 = Double.parseDouble(accessObj.strPn1);
    double eps = Double.parseDouble(accessObj.strEps);
    double a = Double.parseDouble(accessObj.strA);
    double b = Double.parseDouble(accessObj.strB);
    double c = Double.parseDouble(accessObj.strC);
    double d = Double.parseDouble(accessObj.strD);
    double xn = Double.parseDouble(accessObj.strPn2);
    double sigma = 0.5;
    double nxk;
    
  private double func(double x) {
    double f = a * x - b * Math.pow(x, 2) + Math.pow(Math.E, (c * x0 + d * xn));
    return f;
  }
  
  private double prFunc(double x) {
    double f = a - 2*b*x + (c + 2*d) * Math.pow(Math.E, (c*x0 + d*xn));
    return f;
  }
  
  public void GradMethod() {
    double xrez , yrez;
    prFunc(x0);
    int k = 0;
    double xk = x0 + eps;
    
    
    if (func(xk - eps * prFunc(xk)) - func(xk) <= - sigma * eps * Math.pow(Math.abs(prFunc(xk)), 2)) {
      nxk = xk - eps * prFunc(xk);
      prFunc(nxk);
      if(Math.abs(prFunc(nxk)) <= eps) {
        xrez = 2; //Например, присвоим тут 2-ку
        yrez = func(nxk);
        } else {
          k = k + 1;
          //continue;
        }
    } else {
        eps = eps / 2;
        //continue;
      }
    XREZ = xrez; //а тут всё равно просит инициализировать xrez
    YREZ = yrez;
    }
}

Please explain how to solve this problem. The result of the method must be displayed in the form, which does not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Skobelkin, 2013-12-15
@KChernenko

If the conditions are false, then the variables will be uninitialized. What value should then go into XREZ and YREZ? Such values ​​must be written to xrez and yrez at the time of their declaration so that they remain so by the time they are assigned. Or assign them to XREZ and YREZ only in the condition. It depends on the required logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question