Damped Cosine Wave Fitting

조회 수: 25 (최근 30일)
Rob Mullins
Rob Mullins 2016년 1월 27일
댓글: uzzi 2023년 2월 6일
Hey everyone, I am having trouble fitting a damped cosine wave function form experimental data that I took using a Cavendish Balance. I am given the time(x axis) and mrads(yaxis). Here is the code presented along with attached data. I need to fit this data with this function: "theta + A .* exp(-b*t) * cos(omega*t + delta)" I have done research on the 'lsqcurvefit' function in matlab only I am having trouble. Some help would be awesome thanks!
clc;clear;
filename = 'Clockwise1.xls';
time1 = xlsread(filename,'A:A');
mrads1 = xlsread(filename,'B:B');
figure(1)
plot(time1,mrads1,'r.')
title('Cavendish Balance Decay (CW)')
xlabel('Time (sec)')
ylabel('Boom Position (mrads)')
xlim([0 1700]);

답변 (1개)

Star Strider
Star Strider 2016년 1월 27일
This works:
[d,si,r] = xlsread('Rob Mullins Clockwise1.xls');
x = d(:,1);
y = d(:,2);
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zx = x(yz .* circshift(yz,[1 0]) <= 0); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(cos(2*pi*x./b(2) + 2*pi/b(3))) .* exp(b(4).*x) + b(5); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Sum-squared-error cost function
s = fminsearch(fcn, [yr; per; -1; -1; ym]) % Minimise Least-Squares
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'b', 'LineWidth',1)
hold on
plot(xp,fit(s,xp), '--r', 'LineWidth',1.25)
hold off
grid
text(450, 7.6, sprintf('%.2f\\cdotcos(2\\cdot\\pi\\cdot%.4f\\cdotx %+.2f\\cdot2\\cdot\\pi)\\cdote^{%.4f\\cdotx} + %.2f', s(1),1/s(2),1/s(3),s(4),s(5)))
xlabel(char(si(1)))
ylabel(char(si(2)))
legend('Data', 'Regression')
  댓글 수: 10
Star Strider
Star Strider 2023년 2월 6일
The first line prints the function text in the plot.
The second one is the objective function (describing the data) that is used to fit the data.
The third one is the function that fminsearch uses to fit the data by comparing the objective function to the data.
Thjese most recent Comments have no direct relation to the original post, so I will delete them in a few days. They should actually be a new Question.
.
uzzi
uzzi 2023년 2월 6일
Thank you very much,

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by