# how to evaluate integral for given input function ?

4 views (last 30 days)
sun on 24 Aug 2021
Edited: sun on 26 Aug 2021
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.

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)
ans = 1.5630e-07
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:
ans = 3.4148e-07
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.
John D'Errico on 24 Aug 2021
It should do what you want, I think. If you don't have the curvefitting toolbox tell me, but it is well worth the money as a nice set of tools.

Steven Lord on 24 Aug 2021
You don't have a function, you have a vector of data. Unless you know the form of the function x(u) from the theory underlying your original problem consider trapz or cumtrapz to integrate the data directly.
sun on 25 Aug 2021
thank you!