NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Doing Integrals in C code
November 04, 2010 by vburguess |
I am thinking of a project for my math class and I wanted to basically measure the amount of work on a given spring essentially using a potentiometer but I need to know how to do Integrals, I am a novice in C and it was just an Idea so I was more looking to see how hard it would be to do before I went too far into it and get over my head. Are integrals possible and if so does anyone know where I can get some examples of this. |
---|---|
November 04, 2010 by Ralphxyz |
Here is a very interesting thread about coding integrals http://www.dreamincode.net/forums/topic/54110-integrals/. I just googled "C integrals". Apparently it "might" be doable but not much fun. Ralph |
November 04, 2010 by hevans (NerdKits Staff) |
Hi vburguess, I'm assuming you are not asking whether there is a way you can input a function and have it tell you (symbolically) what the integral of the function is. There are programs that do that, but you don't really want to write one, and you should learn to do them by hand anyway. I'm assuming what you have is a bunch of measurements coming in, and you want to (numerically) find the integral using the MCU. Fundamentally an integral is just a whole bunch of additions, so it is absolutely possible to do it aboard the MCU. However, you do need to think a little bit about the conversion from the analog real world to a digital world of your MCU. Your physical system is continuous time system, but you are going to be measuring it at discrete intervals. You need to handle that in order to be able to compute an estimate of what the integral is. Think of it this way. Say I gave you measurements of the velocity of a ball (in m/s) every second, for 10 seconds: [0,5,10,5,10,20,10,0,-5,-10]. Could you, on paper, integrate the velocity to get the position of the ball? It would probably help if you graphed it on a piece of paper (remember an integral is the area under a curve). In order to do the integration you need to make some assumptions about how to treat the points in between the samples you have. Do you just draw straight rectangles from each sample to the next and find the areal of the rectangles? Do you draw a straight line between successive points and find the area that way? There are many ways to get a pretty good answer, and if you can do a version of it on paper, I'm sure you won't have too much trouble coding the solution in C. In reality this is a much deeper question than you think, it is actually a big part of signal processing. For example what would happen if the signal changed faster than you could measure it? So the ball velocity was able to dip from 5 all the way down to -1000 then back up to 10 between the second and third measurements. Your calculation of the integral would be way off, or at least you have a much bigger chance of being way off than if you were sure you have a slowly changing signal (relative to your sampling rate). Doing this project would certainly teach you quite a bit, the issues you run into will not be with C itself, it will be the trickiness of doing a discrete time integral, and measuring the input in the first place. Don't do it if it is an extremely time critical project, but if you feel you have a handle on the math I think it would be a great project. Humberto |
November 06, 2010 by vburguess |
I'm sorry I should have been more specific on what I was doing, basically Im going to have an integral on an interval from 0-x and the x value would come from the pot and would be converted so the integral would seem to be very simple. F(x)-F(0) so in essence I guess in my explanation I found me answer where the integration will happen before I even make the code and then it will be simple math equations so get the answer. |
November 07, 2010 by hevans (NerdKits Staff) |
Hi vburguess, Glad you found your answer. We would love to hear more about the project once it is done and you have some time to write about what you did. Humberto |
Please log in to post a reply.
Did you know that you can aim and fire a servo-controlled water gun from your computer? Learn more...
|