Ok so I think I followed the example https://ch.mathworks.com/help/optim/ug/fit-differential-equation-ode.html and adapted it accurately to my application but now I get the error 'LSQCURVEFIT requires all values returned by functions to be of data type double' which I don't understand since objective_fun returns pos which is similar to the one in the example and all values in pos are doubles
Here's my code. I use an anonymous function objective_fun to pass extra parameters into the real objective function which is paramfun. paramfun itself calls dthetadt to comptute the diiferential equation system.
n=5;k=[0.1 0.1 0.1 0.1 0.1 0.1];tspan = [0 5];theta0 = [deg2rad(linspace(5,-5,n)) zeros(1,n)];Fm=10;objective_fun=@(x,tspan)paramfun(x,tspan,n,theta0);param0=[k, Fm];xdata=5;ydata=[0.4239 0.1086 0.0213 -0.1879 -0.3618];[pbest] = lsqcurvefit(@(x,tspan)objective_fun,param0,xdata,ydata);function dthetadt = odefcn(t,theta,k,Fm,I,Fg,l,n)dthetadt = zeros(2*n,1);clear vars idthetadt(1) = theta(n+1);dthetadt(n+1) = (1/I)*(-k(1)*(theta(1))-k(2)*(theta(1)-theta(2))+Fm*l*sin(theta(1))-Fg*l*cos(theta(1)));for i=2:n-1 dthetadt(i) = theta(n+i); dthetadt(n+i) = (1/I)*(-k(i)*(theta(i)-theta(i-1))-k(i+1)*(theta(i)-theta(i+1))+Fm*l*sin(theta(i))-Fg*l*cos(theta(i)));enddthetadt(n) = theta(2*n);dthetadt(2*n) = (1/I)*(-k(n)*(theta(n)-theta(n-1))-k(n+1)*(theta(n))+Fm*l*sin(theta(n))-Fg*l*cos(theta(n)));endfunction pos= paramfun(x,tspan,n,theta0)k=x(1:n+1);Fm=x(n+2);Fg=0.1;l=30*10^(-3);I=1;[~,pos]=ode45(@(t,theta) odefcn(t,theta,k,Fm,I,Fg,l,n),tspan,theta0);end
It is also worth noting that I would like to fit the response to only some values so xdata and ydata are smaller than tspan and pos.
Best Answer