![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/811454/image.png)
Using ode45 for a non linear differential equation with a term in a vector form
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Thank you in advance for your help
댓글 수: 0
채택된 답변
Star Strider
2021년 11월 24일
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
[VF,Sbs] = odeToVectorField(Eqn)
xusfcn = matlabFunction(VF, 'Vars',{T,Y,D2xs,K,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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/811454/image.png)
.
댓글 수: 6
Star Strider
2021년 11월 24일
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.)
.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!