Mathematical Integration

edited January 2018 in How To...

I'm new to processing and i'm trying to find a way to run integration in processing. I've looked everywhere on ways to do it, but i have nothing so far. Can anybody help me?

Tagged:

Answers

  • Integration? Do you have a link? What did you try?

    Kf

  • I was thinking he was trying to run a lib called Integration. My bad.

    @AcuteMacaroon: What do you have so far? What have you tried?

    Kf

  • Kind of nothing really. I was hoping i could have somewhere to start off from. I know how to use integration from a mathematical standpoint, but i'm unable to translate that into Processing.

  • this uses the library grafica

    you can install it via menu | Sketch | Import Library... | Add Library

    here is a simple example; look at reference of grafica to find out more

    https://jagracar.com/grafica.php

    and

    https://www.processing.org/reference/libraries/

    import grafica.*;
    import java.util.*;
    import java.text.*;
    
    GPlot plot;
    
    void setup() {
      size(800, 410);
    
      Table table = loadTable("samples5.csv", "header");
      table.setColumnType("Elapsedtime", Table.STRING);
      table.setColumnType("ECG", Table.FLOAT);
    
    
      GPointsArray points = new GPointsArray();
    
    
      for (int row = 1; row < 111; row++) {
    
        float y=3 * row + 7; 
    
        points.add(row, y);
      }
      // Create the plot
      plot = new GPlot(this);
      plot.setDim(700, 300);
      // plot.getXAxis().setNTicks(0);
      plot.setPoints(points);
      plot.setPointColor(color(100, 100, 255, 50));
    }
    
    void draw() {
      background(255);
    
      // Draw the plot  
      plot.beginDraw();
      plot.drawBox();
      plot.drawXAxis();
      plot.drawYAxis();
      plot.drawTopAxis();
      plot.drawRightAxis();
      plot.drawTitle();
      plot.drawPoints();
      plot.drawGridLines(GPlot.VERTICAL);
      plot.drawFilledContours(GPlot.HORIZONTAL, 0);
      plot.endDraw();
    }
    
  • I can help with an integration demonstration using visuals.

    For starters, you need to define your function, your integration range and your step(resolution) when drawing this function (We can work in 2D or 3D). Then, you need to define your area(Volume) estimation model. Trapezoids, rectangles, etc. Base on this approach, you can estimate an error in the final value. I plot a sine+linear function in this post: https://forum.processing.org/two/discussion/comment/115283#Comment_115283

    Consider using this function as a starting point to do a simple integration. This is doing the integration of a defined function. If you want to create a versatile program, you still need to understand the integration algorithms and the errors associated with them. On the other hand, I believe @quark wrote a library that could do this and more: http://www.lagers.org.uk/qscript/index.html

    Kf

  • Integration can be either algebraic, for example consider 2x + 5 then

    the integral of 2x + 5 is given by the expression x^2 + 5x + c where c is the constant of integration

    If this is what you want to do, then I think you are out of luck as there is no suitable Processing or Java library for you to use (AFAIK)

    calculate the area under the curve 2x + 5 for a given range of x.

    I say calculate but in reality is more of an estimate but a reasonably accurate estimate. :)

    I see that @kfrajer has suggested this and has given links to examples using this approach.

    The main problem is that the expressions to integrate must be hard coded but it would be more flexible if we had a library that accepted a string for the expression to be integrated e.g. "sin(x) + 0.1 * x + 123" at run time. Fortunately I have 2 libraries to do that, QScript and Jasmine.

    In this case I would use Jasmine, not only is it much faster than QScript but it is better suited for this task.

    The sketch code below demonstrates the Jasmine library using simple Strings for the expression to be integrated. Note: you could use a GUI library such as G4P or controlP5 to provide text boxes to allow expressions to entered at runtime.

    The code is fairly self explanatory except for 3 lines

    Line 1: import org.quark.jasmine.*;
    In Processing you must download the Jasmine library using the Contributions Manager.

    Line 15: Expression e = Compile.expression(expr, false);
    this will compile our expression into Java byte code which we can execute whenever we like. The false means we don't want Jasmine to time how long it takes to do the evaluation. For simple expressions this can take longer than the actual evaluation.

    Line 18: integral += e.eval(x).answer().toFloat();
    This line says evaluate the compiled expression e using the given value of x, get the answer and return the value as a float. This is then added to integral

    The sketch code:

    import org.quark.jasmine.*;
    
    void setup(){
      integrateFunction("x ^ 2", -0.5, 0.5, 2000);
      integrateFunction("sin(x)", 0, PI/2, 10000);
      integrateFunction("2*x + 5", 0, 5, 5000);
    }
    
    void integrateFunction(String expr, float lowX, float highX, int nbrSlices){
      int time = millis();
      float sliceWidth = (highX - lowX) / nbrSlices;
      println("Estimated integral of y = " + expr);
      println("  from " + lowX + " to " + highX);
      println("  using X slice width " + sliceWidth);
      Expression e = Compile.expression(expr, false);
      float integral = 0;
      for(float x = lowX; x < highX; x += sliceWidth){
        integral += e.eval(x).answer().toFloat();
      }
      integral *= sliceWidth;
      println("  is " + integral);
      time = millis() - time;
      println("Calculation took " + (time/1000.0) + " seconds");
      println();
    }
    

    If you run the code below you get the following output

    Estimated integral of y = x ^ 2
      from -0.5 to 0.5
      using X slice width 5.0E-4
      is 0.083457105
    Calculation took 0.062 seconds
    
    Estimated integral of y = sin(x)
      from 0.0 to 1.5707964
      using X slice width 1.5707963E-4
      is 0.99991333
    Calculation took 0.004 seconds
    
    Estimated integral of y = 2*x + 5
      from 0.0 to 5.0
      using X slice width 0.001
      is 50.009605
    Calculation took 0.002 seconds
    
  • Jasmine... right, I forgot about Jasmine. I have never used it but I saw it in your site. Can you clarify briefly what i the diff between Jasmine and QScript?

    Second Q: Expression e = Compile.expression(expr, false);, compiles the expression into Java byte code which we can execute whenever we like. Can you expand on this? What is the meaning of converting this string to java byte code at this point of the program? You are creating your small executable before the whole thing is compiled?

    Kf

  • @kfrajer I am more than willing to discuss QScript and Jasmine but it is off topic and I don't want to hijack this discussion. If you create a new discussion 'QScript vs Jasmine' or something similar I will answer then. :)

Sign In or Register to comment.