

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


2 2 2
2 2 2
% load experimental data
clear load freq_resp_data % this loads the data file with variables freq (in % Hz) and mag
% some initial values – picked at Random K = 10; % gain zeta = 0.1; % damping ratio guess fn=2; % natural frequency guess in Hz
%let's plot what this guess looks like (this is always a good idea) % let's generate a frequency vector that has more components than the % experimental ones
f_toplot = [0:0.1:7.0]; r = f_toplot/fn; mag_init = K./sqrt((1-r.^2).^2+(2zetar).^2);
plot(freq,mag,'o-',f_toplot,mag_init) legend('experiment','theory - initial guess')
% not let's use fminsearch to minimize the difference between the % experiment and the theoretical equation
x0 = [zeta fn K]; % initial guess of values
% the next lines set up the inputs for fminsearch options=optimset(@fminsearch) options=optimset(options,'Display','iter');
% run fminsearch to minimize a cost function J. The function is defined % by the m-file "lab2.m" and the outputs are given in the variable coeff. coeff=fminsearch(@lab3,x0,options)
zeta = coeff(1); fn = coeff(2); K = coeff(3);
% now let's plot our final curve figure r = f_toplot/fn; mag_best = K./sqrt((1-r.^2).^2+(2zetar).^2);
plot(freq,mag,'o-',f_toplot,mag_best) legend('experiment','theory - best fit')
function J = lab3(x)
load freq_resp_data % load the data mag and freq (in Hz)
zeta = x(1); fn = x(2); K = x(3);
r = freq/fn;
% calculate the magnitude for the model mag_theory = K./sqrt((1-r.^2).^2+(2zetar).^2);
% mag_theory_db = 20log10(mag_theory); % If using dB % mag_db = 20log10(mag); % if using dB % J = norm(mag_db-mag_theory_db); % if comparing mag in dB
J = norm(mag-mag_theory); % if comparing mag not in dB