Grigore Dodon
Grigore Dodon

Reputation: 147

JFreeChart plot a natural logarithm and e

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

Upvotes: 1

Views: 347

Answers (1)

vsnyc
vsnyc

Reputation: 2257

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;
    }
}

Upvotes: 3

Related Questions