Import matrix with y variables into ode45
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Janne Daams
am 26 Okt. 2023
Beantwortet: Walter Roberson
am 26 Okt. 2023
Hi all,
I want to solve an ode45 equation for a multibody model. The matrixes I have to implement in the ode45 are very big. Now I am using the subs function to place the values for x into the matrixes. However because the fact that this is done inside the ode45 function running the function takes a while. Is there a way to already implement the x value in the matrixes as a variable outside the ode45 function?
In the code WT and w_bar are matrixes of 12x15 and12x1
The vector hulp_vec and hulp_vec_derivative contain both 15 syms variables
% Solve the equation
[t,x]=ode45(@(t,x)analytical_simplified(t,x,M,H,S,WT,w_bar,N,constr_eq,hulp_vec,hulp_vec_derivative),tspan,x0);
function [dx, lambda, constraint_eq]=analytical_simplified(t,x,M,H,S,WT,w_bar,N,constraint_eq,hulp_vec,hulp_vec_derivative)
tic
tau=[0;sin(t);0];
%make matrices afhankelijk van x and dx
% WT=subs(WT,hulp_vec',x(1:N));
WT=subs(WT,hulp_vec_derivative',x(N+1:2*N));
w_bar=subs(w_bar,hulp_vec',x(1:N));
w_bar=subs(w_bar,hulp_vec_derivative',x(N+1:2*N));
constraint_eq=subs(constraint_eq,hulp_vec',x(1:N));
toc
tic
Minv=inv(M);
% Stabilize the matrixes
wo=100;
alpha=wo/5; beta=1;
w_bar_stab = w_bar + 2*alpha*beta*WT*x(N+1:2*N) + alpha^2*constraint_eq;
% w_bar_stab=w_bar;
L1=(WT*Minv*WT');
L2=(WT*Minv*(H-S*tau)-w_bar_stab);
L1=double(L1); L2=double(L2);
lambda=L1\L2;
ddx=Minv*(S*tau-H+WT'*lambda);
dx(1:N)=x(N+1:2*N);
dx(N+1:2*N)=ddx;
dx=dx';
dx=double(dx);
toc
end
With kind regards,
Janne
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 26 Okt. 2023
You would probably want to do this before the ode45 call, and you would probably want to tell matlabFunction to write to a file, using fixed file names.
Once you have used matlabFunction to write to the files, be sure to "clear" the functions that you just wrote to disk -- this will signal to MATLAB that it should re-parse the file the next time you call the file.
When you ask matlabFunction to write to a file, the default is that optimization is done. Optimization of large expressions can take a long time -- sometimes a lot more time then you would save by not optimizing. But more of a problem is that historically the optimizer has had some significant bugs, so it is safer to ask 'optimize', false to matlabFunction()
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!