MATLAB Answers

First order derivative using Curve Fitter (smoothing spline fit)

5 views (last 30 days)
Timmy Ngo
Timmy Ngo on 3 May 2021
Answered: John D'Errico on 4 May 2021
Hello. I'm having trouble computing the first order derivative of my fitted data, using piecewise polynomials. The attached file represents a Thermogravimetric Analysis (TGA) curve and in order to get a Derivative thermogravimetry (DTG) curve I would need to compute the first derivative of the fitted TGA curve. xData and yData are respectively temperature (C) and mass difference (mg) in the attached file.
[xData, yData] = prepareCurveData(CPET, mPET);
% Set up fittype and options.
ft = fittype('smoothingspline');
% Fit model to data.
[fitresult, gof] = fit(xData, yData, ft)
[fitresult, gof] = fit( xData, yData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'mPET vs. CPET', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'CPET', 'Interpreter', 'none' );
ylabel( 'mPET', 'Interpreter', 'none' );
grid minor
I have been trying to use
xdiff = linspace(xData(2),xData(end),500);
[~,fitdiff2] = differentiate(fitresult,xdiff);
but I believe it is not what I'm looking for as I get a very odd plot.
Many thanks,
Timmy Ngo
Timmy Ngo on 3 May 2021
Sorry, I totally missed that! This is what I used for xData and yData:

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 4 May 2021
For example, we might try using the same fit, on some data where I know the answer.
x = rand(100,1)*2*pi;
y = sin(x) + randn(size(x))/100; % a moderate amount of noise.
Of course, we know the derivative function should look a lot like cos(x). Now I'll use the same fit you used. Since you did two fits, I'll look at the second.
ft = fittype('smoothingspline');
% Fit model to data.
[fitresult, gof] = fit( x, y, ft, 'Normalize', 'on' );
hold on
I note that while the curve does sort of fit the data, that the smoothing spline used a very poor choice of default smoothing parameter. The result would be a nasty looking mess if I were to then differentiate that TWICE.
xdiff = linspace(0,2*pi,500);
[~,fitdiff2] = differentiate(fitresult,xdiff);
So total garbage. The point is, differentiating a spline is a dangerous thing. Numerical differentiation is in fact a noise amplification process, an ill-posed problem. TWICE differentiating that same curve is just exponentially worse.
What did you do? You allowed the curve fitting toolbox to choose the smoothing spline fit. And, clearly that was a poor idea. Sadly, they don't give a lot of intuition as to what the smoothing parameter would be, or how to specify something more intelligent than their default.
Sorry about that. Oh, yeah. I did not write their code, nor did I write their documentation. So someone should potentially be sorry.
Differentiating a curve TWICE, where you have no idea how the curve fit was generated is probably a bad idea. When that curve is a spline, you would be asking for trouble. (Note that had I done this using my SLM toolbox, it actually gives a decent curve fit out of the box, and then gives what is at least a not unreasonable second derivative plot.) SLM is avavilable from the file exchange.

Community Treasure Hunt

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

Start Hunting!