We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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?
Answers
Integration? Do you have a link? What did you try?
Kf
https://en.wikipedia.org/wiki/Integral
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/
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
See also this related discussion of approaches:
Integration can be either algebraic, for example consider
2x + 5
thenIf 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)
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 ofx
, get the answer and return the value as a float. This is then added tointegral
The sketch code:
If you run the code below you get the following output
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. :)