user61139
user61139

Reputation: 33

JavaFX function plots are different than in Wolfram Alpha

I have created a calculator application which is supposed to plot graphs.User gets a textbox where he can enter function formula, which is then validated and parsed to ChartXY and so on to draw it.Basicaly I am using for all my functions libraries from javafx.I have spotted that my results are quite different.Most of the functions run very well but some functions differ, it's like I have real part + imaginary part but wolfram has also it's mirror reflection added.Plese look at my screenshots.

This example is regarding log(x) domain (-6,6) codomain (-2,2)

Wolfram Alpha complex graph: http://oi61.tinypic.com/2uij7l2.jpg

My calculator app screenshot: http://oi62.tinypic.com/2m7xoi9.jpg

Another example is for log(sin(x)) domain(-6.3,6.3) codomain (-7,2)

Wolfram real: http://oi57.tinypic.com/2a5hoif.jpg

Wolfram complex: http://oi59.tinypic.com/kesq49.jpg

My calculator screenshot: http://oi58.tinypic.com/2v13qpu.jpg

arctan(tan(x)) for domain(-6.3,6.3) codomain(-2,2)

Wolfram plot: http://oi58.tinypic.com/hupze0.jpg

My application plot: http://oi62.tinypic.com/8wktmr.jpg

My question is: Is it supposed to be like this, if not then why is it like this and can it be fixed ?

Here is part of my class which contains all the parsing

public class Controller {

@FXML
private TextField domainTextField;
@FXML
private TextField codomainTextField;
@FXML
private LineChart coordinateLineChart;
@FXML
private TextField functionTextField;
@FXML
private NumberAxis xNumberAxis;
@FXML
private NumberAxis yNumberAxis;
@FXML
private ComboBox lineStyleComboBox;
@FXML
private ComboBox lineThiknessComboBox;
@FXML
private ColorPicker colorPicker;
@FXML
private Pane numericPane;
@FXML
private TextField descriptionTextField;
@FXML
private Label descriptionLabel;
@FXML
private VBox legendVBox;
@FXML
private CheckBox modificationModeCheckBox;
@FXML
private AnchorPane toSnapshot;

Stage stage;
MessageBox msgBox;
Intervals intervals = new Intervals();
LinkedList<FunctionViewHelper> functions = new LinkedList<FunctionViewHelper>();
boolean axisXdescriptionFlag = true;
boolean editingMode = false;
int editingFunctionId = 0;
int fontFactor = 0;
ObservableList<XYChart.Series> seriesListChart = FXCollections.observableArrayList();
ArrayList<XYChart.Series> lastSeries = new ArrayList<XYChart.Series>();
ArrayList<CustomFunction> exp4jCustomFunction = new ArrayList<CustomFunction>();

// Draws a graph
private void drawFormula(Calculable expression, ArrayList<String> conditions, String cssStyle) throws ScriptException {
    ArrayList<XYChart.Series> seriesList = new ArrayList<XYChart.Series>();
    seriesList.add(new XYChart.Series());
    double lastY = 0;
    for (double x = intervals.domainStart; x < intervals.domainEnd; x += 0.009) {
        if (Validator.ifAllConditionsTrue(conditions, x)) {
            if (x != intervals.domainStart) lastY = expression.calculate();
            expression.setVariable("x", x);
            if (Math.abs(lastY - expression.calculate()) > Const.brakeLineConst)
                seriesList.add(new XYChart.Series());
            seriesList.get(seriesList.size() - 1).getData().add(new XYChart.Data(x, expression.calculate()));
        }
    }

    seriesListChart.addAll(seriesList);
    lastSeries.addAll(seriesList);

    setSeriesStyle(seriesList, cssStyle);
}

// Parsing string to Calculable which we can draw.
    private Calculable buildExpression(String expression) throws UnparsableExpressionException, UnknownFunctionException {
        return new ExpressionBuilder(expression)
                .withVariableNames("x")
                .withCustomFunctions(exp4jCustomFunction)
                .build();
    }
 private void drawButtonHandler() {
        //TODO remove
        //domainTextFieldOnChange();
        Function function = null;
        if (!Validator.validateFofEmptyFields(domainTextField, functionTextField)) {
            msgBox.show("You have to specify domain and formula.");
            return;
        }
        if (!Validator.validateIntervals(intervals, codomainTextField)) {
            msgBox.show("Specify proper domain and codomain.");
            return;
        }
        try {
            if (!editingMode) {
                function = new Function(functionTextField.getText(), colorPicker.getValue(), (String) lineStyleComboBox.getValue(),
                        lineThiknessComboBox.getValue().toString());
                colorPicker.setValue(Const.getNextColor());
            } else {
                function = getByFunctionId(editingFunctionId).function;
                function.formula = functionTextField.getText();
                seriesListChart.removeAll(getByFunctionId(editingFunctionId).series);
            }
            ArrayList<String> conditions = new ArrayList<String>();
            drawFunctionReq(function.formula, conditions, function.getCss());
            if (!editingMode)
                functions.add(new FunctionViewHelper(function, addLegendFunctionView(function), new ArrayList<XYChart.Series>(lastSeries)));
            else {
                getByFunctionId(editingFunctionId).series = new ArrayList<XYChart.Series>(lastSeries);
                ((Label) getByFunctionId(editingFunctionId).view.getChildren().get(1)).setText(function.formula);
            }
        } catch (UnknownFunctionException ex) {
            ex.printStackTrace();
            msgBox.show("Unknown function.");
        } catch (UnparsableExpressionException ex) {
            ex.printStackTrace();
            msgBox.show("Unparsable expression.");
        } catch (Exception ex) {
            ex.printStackTrace();
            msgBox.show("Unknown error.");
        } finally {
            lastSeries.clear();
        }
    }
 @FXML
    private void domainTextFieldOnChange() {
        domainTextField.setStyle("");
        try {
            intervals.setDomain(domainTextField.getText());
            xNumberAxis.lowerBoundProperty().set(intervals.domainStart);
            xNumberAxis.upperBoundProperty().set(intervals.domainEnd);
        } catch (Exception e) {
            domainTextField.setStyle("-fx-focus-color: red;");
            e.printStackTrace();
        }
    }

    @FXML
    private void codomainTextFieldOnChange() {
        codomainTextField.setStyle("");
        if (!codomainTextField.getText().isEmpty()) {
            try {
                intervals.setCodomain(codomainTextField.getText());

                yNumberAxis.setAutoRanging(false);
                yNumberAxis.setUpperBound(intervals.codomainEnd);
                yNumberAxis.setLowerBound(intervals.codomainStart);
            } catch (Exception e) {
                codomainTextField.setStyle("-fx-focus-color: red;");
                e.printStackTrace();
            }
        } else yNumberAxis.setAutoRanging(true);
    }

Upvotes: 1

Views: 418

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

WA is correct. In the imaginary component, which is arg(z), there should be a jump from pi to 0 at z=0. The real component is log(|z|).

Guess into the blue: Be sure to use arctan2 when computing angles.

Upvotes: 1

Related Questions