FunctionGraph.zip

  • HT6688
    了解作者
  • Java
    开发工具
  • 2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2019-12-05 06:22
    上传日期
这显示了如何显示x等函数。 这是通过绘制x轴和y轴以及从函数定义折线然后显示折线来完成的。
FunctionGraph.zip
  • FunctionGraph.java
    8.9KB
内容介绍
import java.util.function.Function; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.Group; import javafx.scene.shape.Line; import javafx.scene.shape.Polyline; /** * This shows how to display a function such as x goes to x*x. * It is done by drawing the x-axis and the y-axis as well as * defining a polyline from the function and then displaying the * polyline. * * @version 2018-08-28 * @author Manfred Kerber */ public class FunctionGraph extends Application{ /** X_SIZE is the width of the panel in pixels. */ public static final int X_SIZE = 600; /** Y_SIZE the height of the panel in pixels. */ public static final int Y_SIZE = 600; /** dYSize adds some extra to the y-size of the window in order to foster for the header. */ public static final int D_Y_SIZE = 50; /** * f is the function to be displayed. We introduce it as a global * variable so that it can be used in the start method, but be * defined in the main method. */ private static Function<Double,Double> f; /** * a is the left border of the interval on which the function is * to be displayed. We introduce it as a global variable so that * it can be used in the start method, but be defined in the main * method. */ private static double a; /** * b is the right border of the interval on which the function is * to be displayed. We introduce it as a global variable so that * it can be used in the start method, but be defined in the main * method. */ private static double b; /** min is an approximation of the minimum of f in the interval * [a,b]. It is introduced as a global variable so that it does * not have to be recomputed. */ private static double min; /** min is an approximation of the minimum of f in the interval * [a,b]. It is introduced as a global variable so that it does * not have to be recomputed. */ private static double max; /** * n is the granularity. More concretely, the interval [a,b] will * be subdivided in n parts and on each the function will be * approximated by a straight line. */ private static int n; /** * The polyline will be the approximation of the function and be displayed. */ private static Polyline polyline; /** * The method approximates the maximal value of the function f * in the interval [a,b] by dividing the interval in n equal * parts and evaluating the function at the (n+1) different * elements. It is assumed that b is greater than a. * @param f The function to be evaluated. * @param n The number of points to be evaluated. * @param a The lowest x-value to be considered. * @param b The highest x-value to be considered. * @return An approximation of the maximal value of f in the * interval [a,b] (evaluated at a and b, and at the n-1 * equidistant values in between). */ public static double max(Function<Double,Double> f, int n, double a, double b) { double x, y; double max = f.apply(a); double delta = (b - a)/n; /* Loop invariant: max contains the maximum of all values of * the function f(x) considered so far, i.e., the maximum of * f(a), f(a + delta), f(a + 2 * delta), ..., f(a + i * delta). * Consider for i = 0: f(a); for i = n: f(b). */ for (int i = 0; i <= n ; i++){ x = a + delta * i; y = f.apply(x); if (y > max) { max = y; } } return max; } /** * The method approximates the minimal value of the function f * in the interval [a,b] by dividing the interval in n equal * parts and evaluating the function at the (n+1) different * elements. It is assumed that b is greater than a. * @param f The function to be evaluated. * @param n The number of points to be evaluated. * @param a The lowest x-value to be considered. * @param b The highest x-value to be considered. * @return An approximation of the minimal value of f in * the interval [a,b] (evaluated at a and b, and at the n-1 * equidistant values in between). */ public static double min(Function<Double,Double> f, int n, double a, double b) { double x, y; double min = f.apply(a); double delta = (b - a)/n; /* Loop invariant: min contains the minimum of all values of * the function f(x) considered so far, i.e., the minimum of * f(a), f(a + delta), f(a + 2 * delta), ..., f(a + i * delta). * Consider for i = 0: f(a); for i = n: f(b). */ for (int i = 0; i <= n ; i++){ x = a + delta * i; y = f.apply(x); if (y < min) { min = y; } } return min; } /** * The method generates a polyline corresponding to the graph of * function f in the interval [a,b] * @param f The function to be drawn. * @param n The number of equidistant intervals to be drawn. * @param a The minimal x-value in the interval. * @param b The maximal x-value in the interval. * @return The polyline corresponding to the function f over the * interval [a,b] with granularity n. */ public static Polyline functionToPolyline(Function<Double,Double> f, int n, double a, double b) { if (a >= b) { throw new IllegalArgumentException(); } else { double[] points = new double[2*(n+1)]; double x, y; double max = max(f,n,a,b); double min = min(f,n,a,b); /* Loop: Add x and y values to the corresponding arrays * for a, b and n-1 equidistant values in between. */ for (int i = 0; i <= n ; i++){ x = a + (b - a) * i / n; y = f.apply(x); points[2*i] = (x-a) * X_SIZE/(b-a); points[2*i+1] = (max - y)* Y_SIZE/(max-min); } /* Draw graph */ return new Polyline(points); } } /** * The method draws the x-axis if 0 is in the interval [min,max] * It is assumed that the function is not constant. * @param root The group to which the x-axis is to be added. */ public static void drawXAxis(Group root){ if (min <= 0 && max >=0 && min != max) { Line line = new Line(0, max * Y_SIZE/(max-min), X_SIZE, max * Y_SIZE/(max-min)); root.getChildren().add(line); } } /** * The method draws the y-axis if 0 is in the interval [a,b] * @param root The group to which the y-axis is to be added. */ public static void drawYAxis(Group root) { if (a <= 0 && b >= 0 && a != b){ Line line = new Line(b * X_SIZE/(b-a), 0, b * X_SIZE/(b-a), Y_SIZE); root.getChildren().add(line); } } /** * @param stage The window to be displayed. */ @Override public void start(Stage stage) throws Exception { //Creating a line object // Create a Group (scene graph) with the line as single element. Group root = new Group(polyline); drawXAxis(root); drawYAxis(root); // The scene consists of just one group. Scene scene = new Scene(root, X_SIZE,Y_SIZE+D_Y_SIZE); // Give the stage (window) a title and add the scene. stage.setTitle("Function Graph"); stage.setScene(scene); stage.show(); } /**
评论
    相关推荐
    • a040_Optimization_with_Matlab_cost_function.zip
      toolbox link cst mws function 2020
    • Code for Function 2.zip
      to plot a function in matlab
    • HashFunction.rar
      Hash function complete 11 papers
    • hook-function
      npm install hook-function // or with yarn yarn add hook-function 例子 样例功能 function first ( ) { console . log ( '1st' ) ; } function second ( ) { console . log ( '2nd' ) ; } function main ( ) {...
    • commonFunction:commonFunction
      commonFunction commonFunction是一个Android库,可以帮助您为您的应用提供一些常用功能。 添加依赖项 要使用它,您需要在要使用commonFunction的模块的build.gradle文件中添加以下gradle依赖关系(而不是根文件)...
    • js-throttle-function
      var throttle = require("throttle-function"); throttle(function () { // Regardless of how often throttle is called // this function will only be called a maximum // of one every 500 milliseconds }...
    • java-function-invoker
      Java函数调用器 它能做什么 “ Java函数调用程序”使您可以专注于将业务逻辑作为Java函数编写,而调用程序则负责在安装了riff的Kubernetes集群中运行函数所需的其余工作。...Java Function Invoker
    • S-Function 的编写.rar
      S-Function 可以使用MATLAB®,C,C ,Ada,或Fortran 语言来编写。使用MEX 实用工具,将C,C ,Ada,和Fortran 语言的S-Function 编译成MEX-文件,在需要的时候,它们可与其它的MEX-文件一起动态地连接到MATLAB 中...
    • callbag-from-function
      功能调用包 从函数生成 。 每当调用该函数时,源将发出该函数返回的内容。 npm install callbag-from-function 用法 const {source, emitter} = from...const fromFunction = require ( 'callbag-from-function' ) ;
    • promise-function
      $ npm install promise-function 例子 你可以做: var promiseFunc = require ( 'promise-function' ) ; promiseFunc ( function ( param ) { return 'justReturn ok: ' + param ; } , 'paramObject' ) . then ...