# how to evaluate integral for given input function ?

3 views (last 30 days)

Show older comments

Hi guys, Please check the picture below.

A = [a1 a2 a3 ....] A is given, they are temperature number array. You can assume any value you want.

u = [1 2 3 4 ...] u is the time, like hours

So, this temperature functino x(u), will be feed into function out (t, x(t)) to do the intergation. This output of "out" will be only between [0 1] for any given time, t. t is just like u, >=0.

##### 0 Comments

### Accepted Answer

John D'Errico
on 24 Aug 2021

Edited: John D'Errico
on 24 Aug 2021

No, you don't want to fit a polynomial to that vector. Polynomials are just bad news for anything.

After seeing the your picture of the real data, here is what I would do. I would use a spline model to interpolate the data, actually pchip may be the best choice. The nice thing about a spline is you can integrate it easily.

First though, since I don't really have any actual data, let me build some. You seem to have a highly oscillatory pattern that repeats multiple times. I'll just use a simple sinusoial curve as an example.

H = 0:100;

T = 330 + 20*sin(H) + 10*sin(3*H);

plot(H,T,'o-')

That should be close enough. Now, while I could interpolate that curve, and then use that function to integrate the exponential function...

For example, a simple solution might be:

C = 0.01;

B = 4.5e3;

X_t = pchip(H,T);

fun = @(t) C*exp(-B./fnval(X_t,t));

Now, for any time t, we can produce an integral value from 0 to that point in time. The problem is, this function does not have an analytical integral form. At best, we might use integral to perform the numerical integration. For example:

out = @(t) integral(fun,0,t);

out(10)

And that is ok, except that integral is not well vectorized for this sort of problem.

Perhaps better is to build the spline differently. Then we will simply integrate the resulting spline model directly.

K_t = pchip(H,C*exp(-B./T));

So you can see I have chosen to build a spline model of the exponential form that you have inside the integral. We can integrate that spline directly. fnint comes from the curve fitting toolbox, as I recall.

K_int = fnint(K_t);

fnval(K_int,23) % the integral from 0 to 23 is:

fnplt(K_int) % and it is easy to plot the resulting function, as a function

fnval is also nicely vectorized, so you can generate the integral you want at any list of points.

If you lack the curve fitting toolbox, I would get it. It is very useful. But if not, things like fnint are not that difficult to write.

##### 3 Comments

John D'Errico
on 24 Aug 2021

### More Answers (1)

Steven Lord
on 24 Aug 2021

##### 3 Comments

Steven Lord
on 24 Aug 2021

Based on the other question to which you linked, what you want to integrate is essentially a stairs plot?

x = 0:0.5:5;

y = x(randperm(numel(x)));

stairs(x, y)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!