Quadratic Equation Graph

edited December 2016 in Library Questions

The equation is not graphing properly, how do I fix it? I am trying to make a program that accepts a quadratic equation from a user, solves the equation and also graphs the equation. I am pretty new to Processing so if I made any obvious mistakes, please help me to point it out. I adapted the code from the graph from the grafica library but I can't seem to implement it properly. Here is my code:

import javax.swing.JOptionPane;
import grafica.*; 
GPlot plot1;
String input;


void setup(){
  size(500, 500);
  background(255);

  float[] firstPlotPos = new float[] {0, 0};
  float[] panelDim = new float[] {200, 200};
  float[] margins = new float[] {60, 70, 40, 30};

  GPlot plot1 = new GPlot(this);
  plot1.setPos(firstPlotPos);
  plot1.setMar(0, margins[1], margins[2], 0);
  plot1.setDim(panelDim);
  plot1.setAxesOffset(0);
  plot1.setTicksLength(-4);
  plot1.getXAxis().setDrawTickLabels(false);

  String num = JOptionPane.showInputDialog(null,"Enter the coefficient of x²: ", "Text input", JOptionPane.QUESTION_MESSAGE);
  int num1=Integer.parseInt(num);
  String num4 = JOptionPane.showInputDialog(null,"Enter the coefficient of x: ", "Text input", JOptionPane.QUESTION_MESSAGE);
  int num2=Integer.parseInt(num4);
  String num5 = JOptionPane.showInputDialog(null,"Enter the constant: ", "Text input", JOptionPane.QUESTION_MESSAGE);
  int num3=Integer.parseInt(num5);

  println(quadratic(num1, num2, num3));
  println(quadratic1(num1, num2, num3));

   int nPoints = 21; 
  GPointsArray points1 = new GPointsArray(nPoints);

  for (int i = 0; i < nPoints; i++) {
    points1.add(i,(num1*((i)*(i))+(num2*(i))+num3));
    }


 plot1.setPoints(points1);
  plot1.setTitleText("Quadratic equation graph");
  plot1.getTitle().setRelativePos(1);
  plot1.getTitle().setTextAlignment(CENTER);
  plot1.getYAxis().setAxisLabelText("Y Axis");

  plot1.beginDraw();
  plot1.drawBox();
  plot1.drawXAxis();
  plot1.drawYAxis();
  plot1.drawTopAxis();
  plot1.drawRightAxis();
  plot1.drawTitle();
  plot1.drawPoints();
  plot1.drawLines();
  plot1.endDraw();
}

float quadratic(float a, float b, float c){
  return (-b + sqrt( b*b - 4*a*c)) / (2*a);
  }
  float quadratic1(float d, float e, float f){
  return (-e - sqrt( e*e - 4*d*f)) / (2*d);
  }

Answers

  • I don't understand why you need to use grafica library.
    P.S. I say this bcz I don't know how to even use it.

  • edited December 2016

    Please format your code so that it is readable in this forum. Go back and edit your post highlight the code and press Ctrl+O (letter O) making sure there is a blank line before and after the code. More about that here.

    The graphica library will draw and label the axes for you.

    Getting the coefficients using dialog boxes is not very attractive. Processing has 2 good GUI libraries controlP5 and G4P both of these provide a range of input controls. G4P also has a companion tool called GUI Builder which is a visual GUI design tool. These links give you more info about G4P and GUI Builder.

    With regard to solving quadratic formulae, they don't all have real solutions e.g.

    y = x^2 + 2x + 8

    does not have real roots. In other words there is no real value of x that will make y = 0. This happens when b^2 < 4ac

    Long time since I used graphica will try and help when I can read your code.

  • Thanks for your advice, I edited the question accordingly. I will try and look into the GUI libraries as well as the real roots issue.

  • To check if it has real roots, you first check the value of b^2 - 4ac. If it is 0, you have equal (real) root. Less than 0, no real roots. More than 0, then two distinct real roots.

  • Answer ✓

    Just tried your code and have changed lines 36 - 38 to

      for (int i = 0; i < nPoints; i++) {
        float x = i - nPoints/2.0;
        points1.add(i, num1*x*x + num2*x + num3);
      }
    

    This will roughly centre the graph about the y axis. Don't forget that the coefficients need not be integers e.g. y = 2.3x^2 -1.99x - 2.8

    In fact it would be better if num1 etc were floats because when you do the root calculations the roots are likely to be non-integers and you want to avoid doing integer division which could give you the wrong result.

  • Thanks for the help. I think I have gotten everything sorted now.

Sign In or Register to comment.