# Damped Oscillation Equation Fitting

19 views (last 30 days)

Show older comments

I am attempting to fit Data Tables in Excel to a Sin curve in order to determine its Omega for a physics experiment, where I need the equation to be in the form

x' = Ae^-bt sin(ωt' + φ)

Unfortunately I have no idea about Matlab but when I plot them as Column vectors, it is the same as I would recieve in excel and nowhere close to a sin curve. Any and all help is appreciated, as I have been through the previous questions that are similar and they pick up from a sin curve, unable to find instructions to that step, I am posting this here. Attached is the different trials I wish to do this for.

##### 2 Comments

Star Strider
on 6 Nov 2023

### Answers (2)

Sam Chak
on 29 Oct 2023

Hi @Haardhik

You can probably estimate the value of omega (angular frequency) by counting the number of zero-crossing events. If you want to fit the damped sinusoidal equation to the data, you can try this:

Tab = readtable('No Mass (final).xlsx')

%% Trial 1

t = Tab.Time_s_(1:end-2); % Time

y = Tab.Position_m_(1:end-2); % Position

[~, count] = zerocrossrate(y, Method="comparison"); % count zero-crossing

T = t(end)/(count/2); % Period of a sine wave

omega = 2*pi/T % Estimate the angular frequency

% Curve-Fitting

fo = fitoptions('Method','NonlinearLeastSquares',...

'Lower',[ 0, 0, omega-5],...

'Upper',[0.03, 1, omega+5],...

'StartPoint',[max(y) 0.5, omega]);

ft = fittype('a*exp(-b*x)*cos(c*x)', 'options',fo);

[curve, gof] = fit(t, y, ft) % check if omega ≈ c

% Making plots

plot(t, y, '.'), hold on

plot(curve), grid on

xlabel('Time'), ylabel('Position'), title('Trial 1')

legend('Data', 'Fitted Sine')

##### 7 Comments

Sam Chak
on 6 Nov 2023

The reason I asked for the initial velocity is to verify the values of a, b, c, and d found by Alex. Additionally, your data is undersampled because there are fewer than 2 data points per cycle (period). Please follow @Star Strider's advice to obtain more data.

% Using Alex Sha's finding

a = 0.023540378407752; % amplitude

b = 0.247177601901692; % decay rate constant

c = 69.1369475428933; % omega

d = 1.32644349058437; % phi

% period

T = 2*pi/c

% number of points per second

numPts = 110/5 % 110 data points over 5 sec

% How many point in a period?

T*numPts % less than two points

% Proposed fitting model

f = @(t) a*exp(-b*t).*sin(c*t + d);

t = linspace(0, 5, 19811); % minimum 19810 points over 5 sec to have 360 points per period

plot(t, f(t)), xlabel('t'), title('f(t) = a*exp(-b*t).*sin(c*t + d)')

% Velocity by formula (df/dt)

df = a*c*exp(-b*t).*cos(c*t + d) - a*b*exp(-b*t).*sin(c*t + d);

plot(t, df), xlabel('t'), title('df/dt')

% initial velocity based on df formula

df0 = df(1)

% initial velocity computed directly from {a, b, c, d}

df0 = a*c*cos(d) - a*b*sin(d)

% Test if initial velocity is -0.939057777778

True_df0 = -0.939057777778;

ToF = logical(abs(df0 - True_df0) < 1e-4) % True (1) / False (0)

Alex Sha
on 29 Oct 2023

if taking fitting function as: x=a*exp(-b*t)*sin(w*t+phi)

for trial-1

Sum Squared Error (SSE): 9.1948847955911E-5

Root of Mean Square Error (RMSE): 0.000914274913678052

Correlation Coef. (R): 0.999583948620075

R-Square: 0.999168070338902

Parameter Best Estimate

--------- -------------

a 0.023540378407752

b 0.247177601901692

w 69.1369475428933

phi 1.32644349058437

for trial-2

Sum Squared Error (SSE): 0.000115663498814737

Root of Mean Square Error (RMSE): 0.0010162233075687

Correlation Coef. (R): 0.999720103291284

R-Square: 0.999440284924735

Parameter Best Estimate

--------- -------------

a 0.0200937306323941

b 0.236486196043985

w 69.1712756320905

phi 0.896068977752904

for trial-3

Sum Squared Error (SSE): 9.64366701017117E-5

Root of Mean Square Error (RMSE): 0.000936320992461801

Correlation Coef. (R): 0.999614594177523

R-Square: 0.999229336892693

Parameter Best Estimate

--------- -------------

a 0.0158395749665591

b 0.215537219553407

w 69.1871168416771

phi 7.30505431226722

Note: phi is not unique

##### 4 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!