Using ode45 for a non linear differential equation with a term in a vector form

5 views (last 30 days)
Maxime Jean Marc Lenoir on 23 Nov 2021
Commented: Star Strider on 24 Nov 2021
Hello,
I want to solve the 2nd order differential equation:
d^2(xus)/dt^2 = -K/Mus*xus-Ms/Mus*d^2(xs)/dt^2
I found as = d^2(xs)/dt^2 experimentally with potentiometer so I have as a 90000x1 vector that corresponds to the acceleration at each time t and now I want to find xus in function of this same time t using ode45
The problem is that I don't know how to deal with as in ode45 and I have the error:
Unable to perform assignment because the size of the left side is 1-by-1 and the size
of the right side is 0-by-1.
Considering that as is given the rest of my code is:
Mus=100;
Ms=10;
K=1000;
tspan=linspace(0,(size(as)-1)*0.02,size(as));
x0 = [0 0];
[t,x] = ode45(@(t,x) fun(t,x,as,Mus,Ms,K,tspan), tspan, x0);
function dx_dt = fun(t,x,as,Mus,Ms,K,tspan)
i=find(tspan==t);
a=as(i);
dx_dt(1,1) = x(2);
dx_dt(2,1) =-K/Mus*x(1)-Ms/Mus*a;
end

Star Strider on 24 Nov 2021
I do not understand the problem.
Is the objective to estimate a parameters of the differential equation from data?
The time value at each iteration is supplied to the ode45 function so there is no reason to specify it directly. If the data were collected at specific times, use that time vector for ‘tspan’ in the differential equation call. Are the values of ‘K’, ‘Ms’, and ‘Mus’ known, or are they to be estimated (as parameters) from the data?
The derivation is fairly straightforward using the Symbolic Math Toolbox —
syms D2xs K Ms Mus T t xus(t) Y
Dxus = diff(xus);
D2xus = diff(Dxus);
Eqn = D2xus == -K/Mus*xus-Ms/Mus*D2xs
Eqn(t) = [VF,Sbs] = odeToVectorField(Eqn)
VF = Sbs = xusfcn = matlabFunction(VF, 'Vars',{T,Y,D2xs,K,Ms,Mus})
xusfcn = function_handle with value:
@(T,Y,D2xs,K,Ms,Mus)[Y(2);-(K.*Y(1))./Mus-(D2xs.*Ms)./Mus]
It would help to upload/attach the time and vectors (as a matrix as (90000x2) matrix if the intent is to estimate parameters from it.
.
Star Strider on 24 Nov 2021
As always, my pleasure!
The order of the arguments can be changed by using the 'Vars' name-value pair in the matlabFunction call. That might make them easier to work with. (All calls to the function have to have their argument list changed to match it, if it the order is changed.)
.