ODE45 requires more parameters than it should
1 回表示 (過去 30 日間)
古いコメントを表示
Hello All,
I am solving a parametric systems of ODEs in Matlab. I have defined 11 parameters in matlabfunction command, but when it comes to use the ode45 command it requires 14 parameters.
syms m A alpha theta(t) b u(t) I B n J x(t) y(t) D c cr %J=Ir
X=A*sin(alpha*t)
phi=B*sin(n*alpha*t)
%D=c*(diff(x)*cos(theta)+diff(y)*sin(theta))/2+cr*theta^2/2
lambda= m*(I+J)*(-2*diff(X,2)*sin(theta)+u*diff(theta)-(b*(J*diff(phi,2)+cr*diff(theta))/(I + J)))/(I+J+m*b^2)
ode1= diff(theta,2) == -(lambda*b+J*diff(phi,2)+cr*diff(theta))/(I+J)
ode2= diff(u) == -c*u/m+diff(theta)^2*b-2*diff(X,2)*cos(theta)
[V] = odeToVectorField(ode1,ode2)
M = matlabFunction(V,'vars', {'t','Y','A','I','alpha','b','m','B','J','n','c','cr'})
IC = [0 0 0] % Init conds for u , theta, thetadot
tspan_solve=0:0.1:20
tspan_plot=[0 20];
sol = ode45(M,tspan_solve,IC,1,0.00001,1,1,1,1,0.1,1,1,100,100) % Parameters for A, I, alpha, b, m, B, J, n, c, cr
I changed that first parameters and it seemed that has no effect on the solution, but still need to make sure what that is and fix this issue. Every time I try to change the parameters and see the results I get confused.
2 件のコメント
Torsten
2022 年 11 月 29 日
Use
M = matlabFunction(V)
to see which parameters are included in your equations.
After this, you can arrange them as you like.
回答 (3 件)
Cris LaPierre
2022 年 11 月 29 日
The first 2 inputs to your function must by t and y, which they are. Those correspond to tspan_solve and IC. The remaining input arguments must come after all other inputs for ode45 have been entered, including options.
It would appear your input A does not have much of an impact on the results.
syms m A alpha theta(t) b u(t) I B n J x(t) y(t) D c cr K X(t)%J=Ir
phi=B*sin(n*alpha*t);
%D=c*(diff(x)*cos(theta)+diff(y)*sin(theta))/2+cr*theta^2/2
lambda= m*(I+J)*(-2*diff(X,2)*sin(theta)+u*diff(theta)-(b*(J*diff(phi,2)+cr*diff(theta))/(I + J)))/(I+J+m*b^2);
ode1= diff(theta,2) == -(lambda*b+J*diff(phi,2)+cr*diff(theta))/(I+J);
ode2= diff(u) == -c*u/m+diff(theta)^2*b-2*diff(X,2)*cos(theta);
ode3= diff(X,2) == 2*lambda*sin(theta)+c*u*cos(theta)-2*K*X;
[V] = odeToVectorField(ode1,ode2,ode3);
M = matlabFunction(V,'vars', {'t','Y','A','I','alpha','b','m','B','J','n','c','cr','K'})
IC = [0 0 0 1 1]; % Init conds for u , theta, thetadot, X, Xdot
tspan_solve=0:0.1:100;
tspan_plot=[0 100];
sol = ode45(M,tspan_solve,IC,[],1,5,1,1,1,1,1,1,1,1,1); % Parameters for A, I, alpha, b, m, B, J, n, c, cr
% ^^ ^ my changes
plot(sol.x,sol.y(1,:))
2 件のコメント
Cris LaPierre
2022 年 11 月 30 日
編集済み: Cris LaPierre
2022 年 11 月 30 日
You do not need to add an extra parameter. You do need to be aware of the expected inputs. I think your 'extra' parameter is being interpretted as the options input, as I pointed out previously.
Sticking with the syntax you are using, your inputs should be
sol = ode45(M, tspan_solve,IC,options,I,alpha,b,m,B,J,c,cr,K)
syms m alpha theta(t) b u(t) I B J x(t) y(t) D c cr K X(t)%J=Ir
phi=B*sin(alpha*t);
lambda= m*(I+J)*(-2*diff(X,2)*sin(theta)+u*diff(theta)-(b*(J*diff(phi,2)+cr*diff(theta))/(I + J)))/(I+J+m*b^2);
ode1= diff(theta,2) == -(lambda*b+J*diff(phi,2)+cr*diff(theta))/(I+J);
ode2= diff(u) == -c*u/m+diff(theta)^2*b-2*diff(X,2)*cos(theta);
ode3= diff(X,2) == 2*lambda*sin(theta)+c*u*cos(theta)-2*K*X;
[V] = odeToVectorField(ode1,ode2,ode3);
M = matlabFunction(V,'vars', {'t','Y','I','alpha','b','m','B','J','c','cr','K'})
opts = [];
IC = [0 0 0 1 1]; % Init conds for u , theta, thetadot, X, Xdot;
tspan_solve=0:0.1:100;
tspan_plot=[0 100];
sol = ode45(M,tspan_solve,IC,opts,1,1,1,1,1,1,1,1,1); % Parameters for I, alpha, b, m, B, J, c, cr, K
plot(sol.x,sol.y(1,:))
Steven Lord
2022 年 11 月 30 日
That syntax, where you pass additional input arguments after the options structure, is an older syntax that is no longer documented and is intended for backwards compatibility.
Instead I recommend using one of the techniques described in the Parameterizing Functions link in the description of the odefun input argument on the ode45 documentation page. There's also an example on the ode45 documentation page that demonstrates the technique; see "Pass Extra Parameters to ODE Function" on that page.
0 件のコメント
Walter Roberson
2022 年 11 月 30 日
Change
M = matlabFunction(V,'vars', {'t','Y','A','I','alpha','b','m','B','J','n','c','cr'})
to
M = matlabFunction(V, 'vars', {[t Y A I alpha b m B J n c cr]})
参考
カテゴリ
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!