How to plot a cfit derivative with functional bands?

5 views (last 30 days)
I basically want to do the same thing described here, but with the derivative of the fit. In other words, let's assume I have some data xdata and ydata; then, I fit my data with
fit1 = fit(xdata, ydata, 'poly4')
I can plot my data and the fit with the confidence bands as described in the link above, namely:
x_dummy = linspace(min(xdata), max(xdata), 100);
plot(xdata,ydata,'.')
hold on
plot(fit1)
conf1 = predint(fit1,x_dummy,0.95,'functional','off');
plot(x_dummy, conf1, 'r--')
hold off
Now, I want to do the same with the derivative of the fit. I'm looking for a way to obtain a certain cfit object called Dfit1 which is the derivative of fit1 (i.e., a 'poly3' fittype), and then apply to Dfit1 the same procedure above in order to plot it with its confidence bands. However, if I use
Dfit1 = differentiate(fit1,x_dummy)
I obtain a vector (as stated in the documentation) and not a cfit object, which has no more any information about the confidence bands. The function differentiate gives me the derivative that I want, but then I cannot figure out how to obtain its confidence bands.
Just to be complete, I have some other data ydata2. The goal of all of this is to show that ydata2 (that has its own error bars) is compatible with the derivative of ydata, obtained as the analytical derivative of its fit. Of course, this fit (and its derivative) has a certain confidence region; I want to plot together ydata2 with the error bars and Dfit1 with (maybe) a shaded region that indicates the confidence region of the fit, and show that the error bars of ydata2 fall into the confidence region of Dfit1.
I hope it was not too confused; thanks in advance for the help! :)

Answers (1)

Brandon Eidson
Brandon Eidson on 5 Jan 2017
Hey Matteo, it is a bit difficult to know exactly what the best approach is in this case. Usually to do the confidence interval of the derivative of a fit requires some kind of Monte Carlo simulation (where it is run some number of times for each point at which you want the derivative--in your case the "x_dummy" vector). I am not immediately sure what the equivalent would be when one is starting with the raw data rather than some model.
One somewhat straightforward alternative might be, rather than plotting the confidence interval of the derivative of your fit, is to plot the derivative of the confidence interval of your original fit. Essentially you would just perform the "differentiate" function on "conf1" (the results of the "predint" function). However, this may not fit your use case.

Community Treasure Hunt

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

Start Hunting!