function [cost,d_model]=cost_beam(q0,time,d_data) %The function defines a cost function for the vibrating beam inverse problem %It requires as inputs a vector of parameters, time, and your collected data. %Recall that the inputs are dummy variables. That is to say that even though %'time' appears here, it may not be the same 'time' as from inv_beam, it is %only what is sent through the function. C=q0(1); K=q0(2); x0=[d_data(1) 0]; %initial conditions [position, velocity] %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); d_model=x(:,1); %This gives the positions of the beam. %Here the cost is actually calculated. cost=sum((d_data(:)-d_model(:)).^2);