First order derivative using Curve Fitter (smoothing spline fit)
14 次查看(过去 30 天)
显示 更早的评论
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);
plot(xdiff,fitdiff2)
but I believe it is not what I'm looking for as I get a very odd plot.
Many thanks,
Timmy
2 个评论
John D'Errico
2021-5-3
编辑:John D'Errico
2021-5-3
While you seem to show your data in that file, we don't know what
[xData, yData] = prepareCurveData(CPET, mPET);
does. That makes it impossible to see what might be odd about your result. And your data has THREE columns of information in it.
回答(1 个)
John D'Errico
2021-5-4
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.
plot(x,y,'o')
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
plot(fitresult)
axis([0,2*pi,-2,2])
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.
figure
xdiff = linspace(0,2*pi,500);
[~,fitdiff2] = differentiate(fitresult,xdiff);
plot(xdiff,fitdiff2)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Smoothing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!