简体   繁体   中英

Solving Ordinary Differential Equations using Euler in Java Programming

I'm trying to write a java program that will solve any ordinary differential equations using Euler method, but I don't know how to write a code to get any differential equation from the user. I was only able to write the code to solve a predefined ordinary differential equations.

I was able to come with a code to solve some particular ordinary differential equations which were written as functions in the program, I also made research online to look for similar problems but it seem they also wrote it to solve some designated problem not general questions on ordinary differential equations. This was found in most of the article have read online.

Here is my Euler class;

import java.lang.Math;

public class Euler {
    private double x0, y0, x1, y1, h, actual;

    public Euler (double initialx, double initialy,double stepsize,double finalx1) {  
        x0 = initialx; y0 = initialy; h=stepsize; x1 = finalx1;
    }

    public void setEuler (double initialx, double initialy,double stepsize, 
    double finalx1){
        x0 = initialx;y0 = initialy;h   =stepsize;x1 = finalx1;
    }

    public double getinitialx(){
        return x0;
    }

    public double getinitialy(){
        return y0;
    }

    public double getinitialexact(){
        return (double) (0.9048*Math.exp(0.1*x0*x0));
    }

    double func(double x, double y){
        return (double) (0.2*x*y);
    }

    double funct(double x){
        return (double) (java.lang.Math.exp(0.1*x*x));
    }

    public double getinitialerror(){
        return (double) Math.abs(actual - y0);
    }

    public double getEulerResult(){
        for (double i = x0 + h; i < x1; i += h){
            y0 = y0 + h *(func(x0,y0));
            x0 += h;
            double actual = (0.9048*funct(x0));
            double error = Math.abs(actual - y0);
            System.out.printf("%f\t%f\t%f\t%f\n",x0,y0,actual, error);
        }
        return y0;
    }

}

Here is my Driver's class

import java.util.Scanner;

public class EulerTest {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        Euler myEuler = new Euler(1.0,1.0,0.1,1.5);
        System.out.println( "x\t    explicit\tactual\t  error\t    " );

        System.out.printf("%f\t%f\t%f\t%f\n", myEuler.getinitialx(), 
        myEuler.getinitialy(),myEuler.getinitialexact(),
        myEuler.getinitialerror());

        System.out.printf("my approximated value is %f\n\n", 
        myEuler.getEulerResult ());

        System.out.println("enter another initial value of x: ");
        double initialx = input.nextDouble();

        System.out.println("enter another initial value of y: ");
        double initialy = input.nextDouble();

        System.out.println("enter another stepsize value of h: ");
        double stepsize = input.nextDouble();

        System.out.println("enter another upper  bound of x: ");
        double finalx1 = input.nextDouble();

        myEuler.setEuler(initialx,initialy,stepsize,finalx1);
        System.out.println( "x\t    explicit\tactual\t  error\t    " );

        System.out.printf("%f\t%f\t%f\t%f\n", myEuler.getinitialx(), 

        myEuler.getinitialy(),myEuler.getinitialexact(),
        myEuler.getinitialerror());

        System.out.printf("my approximated value is %f\n\n", 
        myEuler.getEulerResult ());

    }
}

I will be glad if i can en lighted on how to write the java code to collect any ordinary differential equation from the user so as to solve using Euler's method.

What you are looking for is the ability to compile some code at run time, where part of the code is supplied by the user.

There is a package called JOOR that gives you a Reflect class that contains a compile method. The method takes two parameters (a package name:String and the Java code:String). I've never personally used it, so can not vouch for its robustness, but here is a tutorial and the javadoc:

https://www.jooq.org/products/jOOR/javadoc/latest/org.jooq.joor/org/joor/Reflect.html#compile(java.lang.String,java.lang.String)

https://blog.jooq.org/2018/04/03/how-to-compile-a-class-at-runtime-with-java-8-and-9/

In your case, you would put your user supplied function in place of the following line of code:

return \"Hello World!\";\n"

Beware, you need to be 100% absolutely unconditionally guaranteed that the user can only ever enter a function to be solved. If they are supplying code, remember that unless you take safeguards, the code they enter could very easily be code the removes all of the files on your hard drive (or worse).

For the second part of your question - how do i implement a solution in Java using Euler's method, perhaps check out this link: Euler's Method in java or this https://rosettacode.org/wiki/Euler_method#Java which has it in pretty much every language you can imagine (and probably some you can't).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM