500149643 Getty Images

How do you integrate with a computer? Let’s start with an example.

Suppose a car travels only in the x-direction. It starts at x = 0 m with a velocity of 0 m/s. If the car has a constant acceleration of a (let’s pick 1.5 m/s2), how far will it travel after four seconds? You should be able to solve this problem in a number of ways. You could start with the definition of acceleration and integrate twice or you could use the kinematic equations. I’m not going to go over either of these solutions since they aren’t very interesting.

How would you solve this numerically (when I say “numerical,” others might say “computational”)? The key to just about every numerical solution is to break a complicated problem down into a bunch of simpler problems. But what’s simpler than a constant acceleration problem? A constant velocity problem. Yes, let’s do that. If an object moves with a velocity v, how far does it travel during some time interval? Let’s start with the definition of velocity (in one dimension):

La te xi t 1

But what if I represent this as a graph? Here is a velocity vs time graph for this same situation.

Spring 2016 Sketches key

As you can see from this plot, the distance traveled would be equivalent to the area under the velocity-time graph. OK, so what if the velocity is changing? What about the case of a constant acceleration? We can still find the displacement as the area under the curve by using a similar method. Let’s just break the curve into many small rectangles where we assume the velocity is constant.

Spring 2016 Sketches key

Here I am calling the width of this rectangle dt instead of Δt to emphasize that it is a very small time interval. The other big difference is that the velocity is not constant and it also changes with time. But note that I have a strategy for calculating the displacement (which is the same as integrating).

  • Start with initial values for position, velocity, and time.
  • Pick a tiny time interval (dt).
  • Calculate the area of this tiny rectangle with width dt and add this to the total area.
  • Increase the time value by dt.
  • Use this new time to calculate the new velocity.
  • Repeat.

Let’s do this with some python. One important note: If you don’t have exact values, you can’t get an answer. You have to use numbers. Also, this only gives a numerical answer and not a function (we can fix that later). I will also include an analytic solution so that we can compare results.

You can see the two values for the displacement. With a fairly large time interval of 0.1 seconds, I still get a displacement fairly close to the analytical solution of 12 meters. Making a smaller time interval will clearly give a better solution. Also, some might complain that my method sucks. I am using the velocity at the beginning of the interval instead of at the end or the middle. Yes, you can debate which velocity would be best, but this is a beginner’s guide to numerical integration. Hopefully these differences won’t matter as my time interval gets small.

But this isn’t what you wanted—I know. You want a function that represents this integral. I can do that, but let me first write out analytically what you are looking for.

La te xi t 1

You want the solution for all values of t. To get this I can find the displacement for t = 0.1 s, then 0.2 s, and then 0.3 s and so on. That means doing the same numerical integration above a bunch of times. The easiest way to do that is with a python function. I’m not going to go over all the details of a function, but here is a quick tutorial.

Hopefully this code will make at least a little bit of sense. I plot both the analytical and numerical solutions.

There you go. That’s the function you were looking for—and it seems to work just fine.

Now how about a complicated case? The integration problems that always caused me problems were ones that involved trig substitution. How an integral that uses both trig sub and integration by parts? Here is the integral we will solve.

La te xi t 1

I did something wrong here, because I am lazy. I shouldn’t have the integration variable the same as the function variable. Really, inside the integral it should say “x‘”, but that would look weird. OK, I’m sorry.

Let me just jump right into the numerical solution. I can also plot the analytical solution by following the answer from this page. Oh, one note. I am going to call the stuff inside the integral g(x) just to make the calculation easier.

Notice that I used the analytic solution from that same website so you can see the two plots are nearly identical. You can change the size of dx to make an even better fit. But yes, numerical integrations can be fairly easy—and useful.

Continue at source: 

Dominate Calculus With a Few Easy Tricks