Clear Filters
Clear Filters

Sine curve fitting in MATLAB

38 views (last 30 days)
M
M on 24 Mar 2023
Commented: Star Strider on 24 Mar 2023
I have used @Star Stider code for sin fitting, and I got the following errors, how to fix it ?
y = [ 0.0060 ;0.0077 ;0.0058];
x = 0:0.1:3;
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zci = @(v) find(v(:).*circshift(v(:), 1, 1) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector (>= R2016b)
zx = x(zci(yz)); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x.*b(2) + 2*pi*b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
s = fminsearch(fcn, [yr; 1/per; -1; ym]) % Minimise Least-Squares
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-31.

Error in fminsearch (line 216)
fv(:,1) = funfcn(x,varargin{:});
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'b', xp,fit(s,xp), 'r')
grid
  1 Comment
Cris LaPierre
Cris LaPierre on 24 Mar 2023
It won't help with the error, but it seems odd to define x in what appears to be radian frequency, and yet still be multip[lying it by inside your fit equation. You typically only do that if your frequency is in Hz.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 24 Mar 2023
Edited: Cris LaPierre on 24 Mar 2023
For fminsearch to work correctly, your function must return a scalar. From the fminsearch documentation:
"fun is a function that accepts a vector or array x and returns a real scalar f (the objective function evaluated at x)."
Your y vector is not the same length as x, so the result of (fit(b,x) - y).^2 is a 3x189 array. Since this is a 2D array, sum will sum the values along the first dimension (columns) resulting in a 1x189 vector.
Instead, define an objective function that captures the overall goodness of fit in a single scalar value. The traditional way to do this would be to define the actual Y value for each x position.

More Answers (1)

Star Strider
Star Strider on 24 Mar 2023
@M — I was in the process of answering this when Win 11 crashed. Again. For the fifth time in two days, and three times when I was in the middle of doing something on Answers. My hatred of Windows and its infernal stability issues knows no bounds at this point! Break up Micro$oft!
Anyway, the post you need is: Damped harmonic motion curve fit instead of the one you’re quoting.
For what it’s worth (which isn’t much at this point) —
A = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1334739/test4.txt');
A = fillmissing(A,'linear');
L = size(A,1);
Fs = 1000;
t = linspace(0, L-1, L).'/Fs;
x = t;
for k = 1:size(A,2)
y = A(:,k);
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zci = @(v) find(diff(sign(v-mean(v)))); % Returns Approximate Zero-Crossing Indices Of Argument Vector
zt = x(zci(y));
per = 2*mean(diff(zt)); % Estimate period
ym = mean(y); % Estimate offset
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) - y); % 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,y,':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.3*max(xlim),min(ylim)+0.05*diff(ylim), sprintf('$y = %.3f\\cdot e^{%.3f\\cdot x}\\cdot sin(2\\pi\\cdot x\\cdot %.0f%.3f) %+.2f$', [s(1:2); 1./s(3:4); s(5)]), 'Interpreter','latex')
title("Column "+k)
end
s = 5×1
1.4409 -0.9392 0.0803 -1.4744 1.0053
nmrs = 7.0983
s = 5×1
1.0111 -0.9376 0.0803 -1.4730 1.0012
nmrs = 5.2914
s = 5×1
0.6272 -0.9372 0.0803 -1.4725 1.0012
nmrs = 3.7098
s = 5×1
0.3026 -0.9355 0.0803 -1.4712 0.9985
nmrs = 2.1786
The fitted functions are not perfect, however the data themselves do not appear to have single-exponential decay characteristics that my code assumes.
.
  2 Comments
M
M on 24 Mar 2023
@Star Strider I just want to do curve fitting for these 3 points, the test4 file was another problem
This what I wanted to do, is there any wrong with it?
y = [ 0.0060 0.0077 0.0058];
x = 0:1:2;
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zci = @(v) find(v(:).*circshift(v(:), 1, 1) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector (>= R2016b)
zx = x(zci(yz)); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x.*b(2) + 2*pi*b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
s = fminsearch(fcn, [yr; 1/per; -1; ym]) % Minimise Least-Squares
s = 4×1
0.0019 0.4902 -1.0710 0.0068
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'b', xp,fit(s,xp), 'r')
grid
Star Strider
Star Strider on 24 Mar 2023
Fitting four parameters with three data pairs is actually not possible, or only minimally possible if you estimate one less parameter (for example eliminating ‘b(4)’), otherwise, there are likely an infinity of curves that could fit those four parameters, since they would not be unique. Estimating three parameters with three data pairs is actually not appropriate. Notice that the curve intersects those three points precisely, however the sine curve itself does not approximate the actual data.
For example, these two illustrations —
y = [ 0.0060 0.0077 0.0058];
x = 0:1:2;
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zci = @(v) find(v(:).*circshift(v(:), 1, 1) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector (>= R2016b)
zx = x(zci(yz)); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x.*b(2) + 2*pi*b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
[s,rn] = fminsearch(fcn, [yr; 1/per; -1; ym]) % Minimise Least-Squares
s = 4×1
0.0019 0.4902 -1.0710 0.0068
rn = 3.1375e-13
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'b', xp,fit(s,xp), 'r')
grid
per = mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x.*b(2) + 2*pi*b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
[s,rn] = fminsearch(fcn, [yr; 1/per; -1; ym]) % Minimise Least-Squares
s = 4×1
0.0037 0.8358 -0.5908 0.0040
rn = 1.4713e-12
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'b', xp,fit(s,xp), 'r')
grid
per = 4*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x.*b(2) + 2*pi*b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
[s,rn] = fminsearch(fcn, [yr; 1/per; -1; ym]) % Minimise Least-Squares
s = 4×1
0.0016 0.2653 -1.0055 0.0061
rn = 9.2874e-13
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'b', xp,fit(s,xp), 'r')
grid
All of these waveforms fit the data, with approximately the same residual norm (‘rn’) values.
.

Sign in to comment.

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!