1

I am using JFreeChart to plot some equations like: x - x^2 I am using this code: (slightly modified by the example provided)

double[] a = {0.0, 1.0, -1.0}; // By the model: y = a0 + a1 * x + a2 * x^2 + ... + an * x^n
Function2D p = new PolynomialFunction2D(a); // create function
XYDataset dataset = DatasetUtilities.sampleFunction2D(p, -5.0, 5.0, 50, "Function");
final JFreeChart chart = ChartFactory.createXYLineChart("Equation","X","Y",dataset,lotOrientation.VERTICAL,true,true,false);

How do I plot this equation? f(x) = ln(x+3)+3 and e^x - 2*x

Grigore Dodon
  • 137
  • 3
  • 13
  • You can generate your own points and plot that if you can't find a Function2D implementation for your equation – vsnyc Nov 13 '15 at 20:16

1 Answers1

3

Update: Java 8 Solution I have left the old solution as is below. If your functions to plot are one-off expressions that you don't want to maintain as individual classes, in Java 8 you can implement the Function2D as a lambda expression.

For example ln(x+3)+3 would be written as:

double[] a = {1.0, 3.0, 3.0};
XYDataset dataset = DatasetUtilities.sampleFunction2D(v -> Math.log(a[0]* v + a[1]) + a[2], 1.0, 5.0, 50, "Function");
final JFreeChart chart = ChartFactory.createXYLineChart("Equation", "X", "Y", dataset, PlotOrientation.VERTICAL, true, true, false);

Original answer:

You can implement your own functions to do the plotting. For example for ln(x+3)+3 you can have something like below:

public class LogLinearFunction2D implements Function2D {
    //Plot ln(ax + b) + c
    double a;
    double b;
    double c;
    public LogLinearFunction2D(double[] params) {
        if(params.length != 3) throw new RuntimeException("Invalid parameters, expected array count 3");
        this.a = params[0];
        this.b = params[1];
        this.c = params[2];
    }
    @Override
    public double getValue(double v) {
        return Math.log(a* v + b) + c;
    }
}

And then you can use this to plot the function with the code snippet:

    double[] a = {1.0, 1.0, -1.0};
    Function2D p = new LogLinearFunction2D(a); // create function
    XYDataset dataset = DatasetUtilities.sampleFunction2D(p, 1.0, 5.0, 50, "Function");
    final JFreeChart chart = ChartFactory.createXYLineChart("Equation", "X", "Y", dataset, PlotOrientation.VERTICAL, true, true, false);
     ChartUtilities.saveChartAsPNG(new File("test.png"), chart, 500, 500);

Similarly for e^x - 2*x, you can have the following Function2D implementation generalized for plotting a*e^x + b*x + c and pass the array double[] a = {1.0, -2.0, 0}

private static class ExpLinearFunction2D implements Function2D {
    //Plot a*e^x + b*x + c
    double a;
    double b;
    double c;
    public ExpLinearFunction2D(double[] params) {
        if(params.length != 3) throw new RuntimeException("Invalid parameters, expected array count 3");
        this.a = params[0];
        this.b = params[1];
        this.c = params[2];
    }
    @Override
    public double getValue(double v) {
        return a * Math.exp(v) + b * v + c;
    }
}
vsnyc
  • 2,117
  • 22
  • 35
  • It plots pretty well, you have to take the right range though, for a high max value the graph gets skewed as e^x becomes very large. e.g. try this range, it matches graph generated by [Wolfram Alpha](https://www.wolframalpha.com/share/clip?f=d41d8cd98f00b204e9800998ecf8427efjgr20ic5m) `XYDataset dataset = DatasetUtilities.sampleFunction2D(v -> a[0] * Math.exp(v) + a[1] * v + a[2], -3.0, 3.0, 1000, "Function");` – vsnyc Nov 14 '15 at 14:58