简体   繁体   English

Java编程中使用欧拉求解常微分方程

[英]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.我正在尝试编写一个 Java 程序,该程序将使用欧拉方法求解任何常微分方程,但我不知道如何编写代码来从用户那里获取任何微分方程。 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.如果我能启发如何编写java代码来收集用户的任何常微分方程以便使用欧拉方法求解,我会很高兴。

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.有一个名为 JOOR 的包,它为您提供了一个包含 compile 方法的 Reflect 类。 The method takes two parameters (a package name:String and the Java code:String).该方法采用两个参数(包名:String 和 Java 代码:String)。 I've never personally used it, so can not vouch for its robustness, but here is a tutorial and the javadoc:我从来没有亲自使用过它,所以不能保证它的健壮性,但这里有一个教程和 javadoc:

https://www.jooq.org/products/jOOR/javadoc/latest/org.jooq.joor/org/joor/Reflect.html#compile(java.lang.String,java.lang.String) 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/ 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.请注意,您需要 100% 绝对无条件地保证用户只能输入要解决的功能。 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).对于您问题的第二部分 - 我如何使用 Euler 方法在 Java 中实现解决方案,也许可以查看此链接: Java 中的 Euler 方法或此https://rosettacode.org/wiki/Euler_method#Java几乎所有你能想象到的语言(可能还有一些你不能想象)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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