# Curve Fitting of large Data Measurement?

20 views (last 30 days)
Hossam Amin on 23 Jan 2022
Commented: Star Strider on 30 Jan 2022
I have a measurement data which I have uploded, and I am trying to utilize a function to fit the measurement with a damping sinusoidal order of the measurement dataset. But without success. Can anyone please point me in the right direction? The curve fitting app in matlab is unable to make it. I tried a damping function in it, it didn't work. Also the lsqcurvefit nothing is working. I tried a sine function with not success too.
##### 2 CommentsShowHide 1 older comment
Hossam Amin on 23 Jan 2022
Maybe I forgot to mention. The data is composed of speed and torque measurements.
You would need to add the x-axis at time using linspace command.

Star Strider on 24 Jan 2022
Try this —
format longE
x = T1.Time;
yc(:,1) = T1.('Torque[Nm]');
yc(:,1) = smoothdata(yc(:,1), 'sgolay', 250); % Remove Some Noise To Make The Fit Easier
yc(:,2) = T1.('RPM[1/min]');
for k = 1:size(yc,2)
yk = yc(:,k);
y = yk;
% y = detrend(y);
ym = mean(y); % Estimate offset
y = y - ym;
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zci = @(v) find(diff(sign(v))); % Returns Approximate Zero-Crossing Indices Of Argument Vector
zt = x(zci(y));
per = 2*mean(diff(zt)); % Estimate period
fit = @(b,x) b(1) .* exp(b(2).*x) .* (sin(2*pi*x./b(3) + 2*pi/b(4))) + b(5); % Objective Function to fit
fcn = @(b) norm(fit(b,x) - yk); % Least-Squares cost function
[s,nmrs] = fminsearch(fcn, [yr; -10; per; -1; ym]) % Minimise Least-Squares
xp = linspace(min(x),max(x), 500);
figure
plot(x,yk,':b', 'LineWidth',1.5)
hold on
plot(xp,fit(s,xp), '--r')
hold off
grid
xlabel('Time')
ylabel('Amplitude')
legend('Original Data', 'Fitted Curve')
text(0.1*max(xlim),0.7*min(ylim), sprintf('$y = %.3f\\cdot e^{%.3E\\cdot x}\\cdot sin(2\\pi\\cdot x\\cdot %.3E%+.3f) %+.4f$', [s(1:2); 1./s(3:4); s(5)]), 'Interpreter','latex')
end
Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 7.253750
s = 5×1
1.0e+00 * 2.054070381848720e-01 -1.211851657300356e-04 3.381604644061252e+03 -1.273801702648572e+00 -6.517492492161443e-03
nmrs =
7.253749849224849e+00
Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 6954.008039
s = 5×1
1.0e+00 * 5.565560752001775e+02 -3.434517388063775e-04 3.862185655766382e+03 -6.747881574969156e-01 3.384702768264112e+02
nmrs =
6.954008038834750e+03
Because it uses the least-square approach, the fit is dominated by the highest peaks, and fits them preferentially. Experiment with tweaking ‘fit’ to get different results. (This uses a slightly-modified version of my original code to process and fit the data.)
.
Star Strider on 30 Jan 2022
As always, my pleasure!

R2021a

### Community Treasure Hunt

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

Start Hunting!