function [fa,ifaila,icounta]=pidlsq(xa) % PIDLSQ can accept multiple input vectors and return a matrix of outputs. % % [nr,nc]=size(xa); fa=[]; ifaila=zeros(nc,1); fcounta=zeros(nc,1); for i=1:nc [fap,ifaila(i),icounta(i)]=serial_pidlsq(xa(:,i)); fa=[fa, fap]; end function [f,ifail,icount]=serial_pidlsq(x) % % Parameter ID example formulated as nonlinear least squares problem. % % global pid_data time_pts pid_parms pid_y0 pid_tol; ifail=0; icount=1; % % Call the integrator only if x is physically reasonable, ie if % x(1) and x(2) are nonnegative. Otherwise, report a failure. % if min(x) < 0 ifail=1; icount=0; f=NaN; disp(' failure in pidobj') else pid_parms=x; tol=pid_tol; options=odeset('RelTol',tol,'AbsTol',tol,'Jconstant',1); y0=pid_y0; [t,y]=ode15s(@yfunsp, time_pts, y0, options); r=y(:,1)-pid_data(:,1); % f=r'*r/2; f=r; end function yp=yfunsp(t,y) % % simple harmonic oscillator for parameter id example % % system form of y'' + c y' + k y = 0 % global pid_parms; yp=zeros(2,1); yp(1)=y(2); c=pid_parms(1); k=pid_parms(2); yp(2)= - k* y(1) - c*y(2);