0

I have the following arbitrary data:

t=[0 1 2 3 4 5 6 7 8 9 10 12 14];
C=[0 1 5 8 10 8 6 4 3.0 2.2 1.5 0.6 0];
area=trapz(t,C);
E=C./area;
F=cumtrapz(t,E);

When plotted, it looks as follows: enter image description here

Now, I want to smoothen the plots:

t_int=[];
E_int=[];
F_int=[];

xmin=min(t);
xmax=max(t);
points=5000;
stepSize=(xmax-xmin)/points;

for i=xmin:stepSize:xmax
    t_int=[t_int i];
    E_int=[E_int interp1(t, E, i,'pchip')];
    F_int=[F_int interp1(t, F, i,'pchip')];
end

enter image description here

Okay, so here we get to the actual part where the problem is. In reality, I have some F plot and I would like to get its corresponding E plot. Since the F plot is the integral of E, E is the differentiated F plot.

So then:

dy=diff(F_int);
dx=diff(t_int);
dydx = dy./dx;
plot(t_int(:,1:length(dydx)),dydx,t_int,E_int)
legend('E (calculated)','E (actual)')

enter image description here

Why is the calculated E plot so wonky?

Mierzen
  • 566
  • 1
  • 5
  • 25
  • Because of the interpolation, of course. – Ander Biguri Oct 22 '15 at 16:30
  • Yes, it is caused by the interpolation. But since the interpolated graph is smooth, why isn't the diff() output smooth as well? – Mierzen Oct 22 '15 at 16:34
  • Because....Its the derivative of the interpolation graph... if you zoom in Im pretty sure you can see that its not 100% smooth. In fact, the best way to see if it is completely smooth woudl be to calulate the derivative of it.... Try other interpolation techniques, e.g. spline – Ander Biguri Oct 22 '15 at 16:39
  • I've zoomed in on the _int graphs and they look perfectly smooth. I will try spline on my real data - hopefully it fits fine – Mierzen Oct 22 '15 at 17:00
  • Here is the last plot, fitted with the spline interp1: http://i.imgur.com/z4YuUbV.png – Mierzen Oct 22 '15 at 17:07
  • Good! Interpolation definitely matters! Spline interpolation does use the adjacent points to not only fit the data in the value, but also in the derivative. Thats why it works. – Ander Biguri Oct 22 '15 at 17:11
  • I really need the derivative to go through the data points... With my real data, I don't have the "E points", which is why I'm trying to kind-of reverse engineer it with other data, to first find a method that works – Mierzen Oct 22 '15 at 17:18
  • But it wont. You can not create a method that will generate the exact data. thats impossible. an interpolation its just and educated guess of what the point will be...I guess that you may get better results if you derive and then interpolate that. – Ander Biguri Oct 22 '15 at 17:23
  • One minute, lemme try that :-) – Mierzen Oct 22 '15 at 17:28
  • Sorry it took so long. Here it is: http://paste.ofcode.org/3bvLCpNykKDaNGzzzPqSe6g and http://imgur.com/nzwIcp7.png – Mierzen Oct 22 '15 at 17:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93095/discussion-between-ander-biguri-and-mierzen). – Ander Biguri Oct 22 '15 at 17:45

0 Answers0