# How can i fit ODE parameters to given frequency and amplitude data pairs with "lsqcurvefit"?

15 views (last 30 days)
Paul-Adam on 10 Aug 2022
Commented: Paul-Adam on 11 Aug 2022
Hello there,
i have a problem to implementing a ODE to the lsqcurvefit function. My ODE: a simple spring-damper-mass oszillator. And i have amplitude (mampl) and frequency (frqSet) data to which the parameters should be adjusted.
I have written this so far but i am not getting anywhere:
xd=frqSet;
yd=mampl;
x0=[1e-4 1e+0 1e-10 1e-9];
opts = optimoptions('lsqcurvefit','MaxFunctionEvaluations',10000,'FunctionTolerance',1e-8,'StepTolerance',1e-8,'MaxIterations',10000,'OptimalityTolerance',1e-8);
lx = [1e-4 1e+0 1e-10 1e-9];
ux = [1e-2 1e+2 1e-7 1e-6];
x = lsqcurvefit(@fitfun,x0,xd,yd,lx,ux,opts);
function fitfun(x,xd)
ts = [2*pi/1.001e+5 2*pi/1.002e+5];
u0 = 1e-7;
[ta,ua] = ode45(@(u,t) f(u,t),ts,u0);
function dudt= f(u,t)
dudt = x(4).*sin(xd.*t)./(x(2))-2.*x(1).*sqrt(x(2)./x(3)).*x(3).*u(1)./x(2)-u(2).*x(3)./x(2);
end
end
Thanks for help in advance.
Paul-Adam on 11 Aug 2022
Yes. frqSet are an array of excitation frequencies. Bjorn Gustavsson on 10 Aug 2022
There are two issues you will have to solve. The first is to convert your second order ODE to two coupled first-order ODEs. The second is to wrap everything into an "error-function" for the fit to your data such that you can optimise the parameters.
The first step will be to re-write the equation-of-motion function to something like this:
function dudtdu2dt2= f(u,t)
% First derivative/Velocity
dudt = u(2);
% Second derivative/acceleration note the changes of indices v v
du2dt2 = x(4).*sin(xd.*t)./(x(2))-2.*x(1).*sqrt(x(2)./x(3)).*m.*u(2)./x(2)-u(1).*x(3)./x(2);
dudtdu2dt2 = [dudt;du2dt2]; % Return the column vector of [v;a]
end
For the second step have a look at the solution to a similar enough problem here: monod-kinetics-and-curve-fitting. The ODEs that are solved there are completely different but the technique/programming pattern is identical.
HTH
Bjorn Gustavsson on 11 Aug 2022
With harmonic expansion I mean that you can make the ansatz: This happily ignores the onset but will give you the right long-term behaviour. The velocity and acceleration you get: These you can then plug into your equation of motion - and notice that the complex exponentials cancels out - what remains is an algebraic equation for - the amplitude of the oscillation. You will find that it is now a complex variable, but that only means that the real and imaginary parts explain how much the oscillation is phase-shifted relative to the driving force. This equation you can solve for and calculate its absolute value for. This should be the analytical expression for the relation between the amplitude and frequency. We can do this because your equation of motion is linear, that gives us only complex exponentials that can be factored out. If you had a non-linear equation-of-motion this would not always be possible, for example if we had a term with a factor then this harmonic expansion would lead to one term with - this would couple the equations for different frequencies and be much more challenging.

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!