%Generate the data for m=2, c=2, and k=1 by solving the %differential equation at t=0, 0.5, 1, 1.5, ..., 10. m=2; c=2; k=1; x0=[1; 0]; tspan=0.:.5:10; options=odeset('RelTol',1e-5,'AbsTol',1.e-5); [tdata,xdata]=ode45('fcnspring',tspan,x0,options,m,c,k); plot(tdata,xdata(:,1),'b-*') grid on xlabel('Time') ylabel('Displacement') title('Simulated Data') %Inverse problem: Now given that we know the data at %t=0, 0.5, 1, 1.5, ..., 10, we want to determine the %parameters m, c, and k so that the solution of the %spring-mass-dashpot model will best fit the data at %t=0, 0.5, 1, 1.5, ..., 10 options=optimset('MaxFunEvals',2000,'MaxIter',1000,'TolFun',1.e-12,'TolX',1.e-10); parguess=[3 1 5]; [par,fval]=fminsearch('lscost',parguess,options,tdata,xdata) %Plot the solution of the model against the data m=par(1); c=par(2); k=par(3); x0=[1; 0]; options=odeset('RelTol',1e-5,'AbsTol',1.e-5); [tmodel,xmodel]=ode45('fcnspring',tdata,x0,options,m,c,k); plot(tdata,xdata(:,1),'b-*',tmodel,xmodel(:,1),'r-v') grid on xlabel('Time') ylabel('Displacement') title('Model Solution versus Simulated Data') legend('Data',['Model, 'num2str(par)],0)