I have all values of position as a function of time, attached graph. I'm trying to fit an exponential function to the upper peaks. Any tips?
MATLAB: Get amplitude decrease as a function of time in damped harmonic oscillations
amplitudedamped harmonic oscillationsexponential fit
Related Solutions
The exponential ddecay is likely multi-exponential, since a single expoinential provides a decent although inexact fit:
D = load('Breather_Data.m');x = D(:,1);y = D(:,2);y = detrend(y); % Remove Linear Trend
yu = max(y);yl = min(y);yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % 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) + b(4))) + b(5); % Objective Function to fit
fcn = @(b) norm(fit(b,x) - y); % Least-Squares cost function
[s,nmrs] = fminsearch(fcn, [yr; -1E+11; 1/per; -1; ym]) % Minimise Least-Squares
xp = linspace(min(x),max(x), 500);figureplot(x,y,'b', 'LineWidth',1.5)hold onplot(xp,fit(s,xp), '--r')hold offgridxlabel('Time')ylabel('Amplitude')legend('Original Data', 'Fitted Curve')text(min(xlim)+0.05*diff(xlim), min(ylim)+0.8*diff(ylim), sprintf('$y = %.3E\\cdot e^{%.3E\\cdot x}\\cdot sin(2\\pi\\cdot x\\cdot %.3E + %.3f) %+.3f$', s), 'Interpreter','latex')
This is slightly revised from the previous code, since your data required some special considerations because of the magnitude of the independent variable.
Try this:
D = load('Stashu Kozlowski DHM.mat'); % File Attached
x = D.x;y = D.y;y = detrend(y); % Remove Linear Trend
yu = max(y);yl = min(y);yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % 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);figureplot(x,y,'b', 'LineWidth',1.5)hold onplot(xp,fit(s,xp), '--r')hold offgridxlabel('Time')ylabel('Amplitude')legend('Original Data', 'Fitted Curve')text(0.3*max(xlim),0.7*min(ylim), sprintf('$y = %.3f\\cdot e^{%.0f\\cdot x}\\cdot sin(2\\pi\\cdot x\\cdot %.0f%.3f)$', [s(1:2); 1./s(3:4)]), 'Interpreter','latex')
The estimated parameters are:
s = -1.398211481931498e+00 -6.142349926864338e+02 2.591368008158479e-04 -5.442228857001487e+00 -3.075267405594925e-15
and the fit is nearly perfect:
Best Answer