clear all %this clears any variables that may be in matlab's cache (unless you have a good reason, most code should begin with this) load('data1') %This calls the your date file. %under the assumption that the data is labeled in data1 as %trace_x (first column of data), trace_y (second column of data) time=trace_x; %the first column in your data file is time d_data=trace_y(1,:);%the second column in your data is displacement timelength=length(time); %if you wanted the maximum displacement... val=max(d_data'); %this finds the index (array position) of the maximum value of your data index=find(d_data==val) index=index(1); %this zeros the time data and restarts time at the time of maximum displacement time=time(index:timelength)-time(index); %this restarts your displacement data at the time of maximum displacement d_data=d_data(index:timelength); %these two parameters are our initial guesses for C, K C0=3; K0=2; %we collect them in an initial parameter vector q0 q0=[C0;K0]; %this runs a matlab algorithm which searches the function cost_beam for a minimum given %certain input parameters...initial parameter guesses,(use help fminsearch to figure out why [] is there), time array, displacement array %it outputs the parameters which minimize the cost and the cost [q,cost]=fminsearch('cost_beam',q0,[],time,d_data); %These are the optimal values for C and K C=q(1); K=q(2); %Now we want to see how well the algorithm did by simulating the ode %This is the initial [position, velocity] vector x0=[d_data(1) 0]; %This runs one of matlab's preprogrammed ode solvers (type help ode23 in the command window and check out other solvers) %The ode ode_model is called, the time range is given, initial [position, velocity], , and parameters %time and [position,velocity] vectors are the outputs [t,x]=ode23('ode_model',time,x0,[],C,K); %[t,x]=ode23(@(time,x0,C,K)ode_model(time,x0,C,K),x0); %this take all rows, 1st column of x (the position) and saves it as d_model d_model=x(:,1); %Here we plot the collected data and compare it with the results of the model. plot(time,d_data,'--',time, d_model,'r') legend('experimental data','model displacement') title('Two-parameter model estimation'); xlabel('time (s)'); ylabel('displacement');