I am having a difficulty fixing the error I’m encountering in my program. I keep getting this error that the y0 is null and I’ve tried everything but the error persists. I’m using Apache Netbeans as my programming tool.
Here is the complete code:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.Scanner;
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction;
import org.apache.commons.math3.analysis.solvers.NewtonRaphsonSolver;
public class NewtRaphNonLinear {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompting user to input equations
System.out.print("Enter equation 1 (in terms of x and y): ");
String equation1 = scanner.nextLine();
System.out.print("Enter equation 2 (in terms of x and y): ");
String equation2 = scanner.nextLine();
// Prompting user to input initial guesses
System.out.print("Enter initial guess for x and y: ");
double x0 = scanner.nextDouble();
double y0 = scanner.nextDouble(); // Declare y0 without final
// Prompting user to input relative error
System.out.print("Enter relative error: ");
double relativeError = scanner.nextDouble();
// Using Apache Commons Math library for solving equations
NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
UnivariateDifferentiableFunction systemFunction = new UnivariateDifferentiableFunction() {
@Override
public double value(double point) {
double x = point; // Extract x from the point
double y = point; // Extract y from the point
return evaluateEquation(x, y, equation1, equation2);
}
@Override
public DerivativeStructure value(DerivativeStructure t) throws UnsupportedOperationException {
return null;
}
};
// Solving equations
double solutionX = solver.solve(100, systemFunction, x0, relativeError);
double solutionY = evaluateEquation(solutionX, y0, equation1, equation2);
System.out.println("Solution:");
System.out.println("x = " + solutionX);
System.out.println("y = " + solutionY);
}
// Function to evaluate equations
private static double evaluateEquation(double x, double y, String equation1, String equation2) {
// Replace 'x' and 'y' in the equations with their values
String replacedEquation1 = equation1.replaceAll("x", "(" + x + ")").replaceAll("y", "(" + y + ")");
String replacedEquation2 = equation2.replaceAll("x", "(" + x + ")").replaceAll("y", "(" + y + ")");
// Evaluate the expressions using ScriptEngineManager
double result1 = evaluateExpression(replacedEquation1);
double result2 = evaluateExpression(replacedEquation2);
return result1 - result2; // Returns the difference for the Newton-Raphson solver
}
// Function to evaluate mathematical expressions
private static double evaluateExpression(String expression) {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
try {
return (double) engine.eval(expression);
} catch (ScriptException e) {
e.printStackTrace();
return Double.NaN;
}
}
}
The equations I’m using is this:
Enter equation 1 (in terms of x and y): x^2+2y^2=22
Enter equation 2 (in terms of x and y): 2x^2-xy+3y=11
Enter initial guess for x and y: 1.5 2.5
Enter relative error: 0.01
I’ve expected the result to be:
Solution: (Approximately)
x = 2.000000002808708
y = 3.0000000001706063
but the actual result is this:
Exception in thread “main” java.lang.NullPointerException: Cannot invoke “org.apache.commons.math3.analysis.differentiation.DerivativeStructure.getValue()” because “y0” is null
Vimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.