Can someone skilled in java help me with the error I am having in my code about Non Linear Newton Raphson?

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

New contributor

Vimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật