% single-mass-spring data analysis by Tiernan Fogarty x=rfcut2(:,2); % load position data xd=x % keep rough data m=length(x); x=rfcut2(11:m,2); % cut the first few values to start at the max of a period vshift=(max(x)+min(x))/2; % calculate vert shift x=x-vshift; % shift position values to be centered at zero td=rfcut2(:,1); t=rfcut2(11:m,1); % cut the first few values to start at the max of a period amp=(max(x)-min(x))/2; % compute amplitude phi=22; % start time (phase shift) k=200*(real(acos(x(1:7)/amp))./(t(1:7)-phi)).^2; % estimate k from data points kest=mean(k); % k-estimate is average of k values from first half-period modx=amp*cos(sqrt(kest/200)*(t-phi)); % compute model (estimate) data given k estimate figure(1) % plot data vs model for initial k-estimate subplot(2,1,1) plot(t,x,t,modx,'r') title('Initial Estimate for k') legend('Data', 'Model','Location','SouthEast') ylim([-.12 .12]) diff=x-modx; subplot(2,1,2) plot(t,diff) title('Difference between Data and Model') ylim([-.2 .2]) % revise k estimate via least squares minimization KFunction = @(ke,t) amp*cos((sqrt(ke/200))*(t-phi)); [o1] = lsqcurvefit(KFunction,[kest],t,x); kest=o1; modx=amp*cos(sqrt(kest/200)*(t-phi)); % recompute model with new k estimate figure(2) % view data, model, and the difference between model and data subplot(2,1,1) plot(t,x,t,modx,'r') title('Least Squares Fit for k') legend('Data', 'Model','Location','SouthEast') ylim([-.12 .12]) diff=x-modx; subplot(2,1,2) plot(t,diff) title('Difference between Data and Model') ylim([-.2 .2]) figure(3) plot(td,xd,'*',td,xd) title('Data points')