フィルターのクリア

Symbolic manipulation and integration / not enough input argument

1 回表示 (過去 30 日間)
Mauro De Francesco
Mauro De Francesco 2018 年 10 月 9 日
コメント済み: Stephan 2018 年 10 月 10 日
Hello, I have two symbolic functions that i want to integrate. I have converted with matlabFunction the equations into a file here reported:
function DY = Pendulum(theta,w,l)
%PENDULUM
% DY = PENDULUM(THETA,W,L)
% This function was generated by the Symbolic Math Toolbox version 8.2.
% 09-Oct-2018 12:30:20
DY = [w;(sin(theta).*(-9.81e2./1.0e2))./l];
Where the variables are theta and w and l is a constant. Now I want to integrate like this:
X0 = [90*pi/180 ; 1.2];
[tout,yout] = ode45(@Pendulum, [0 5], X0, 2 );
and i get the error Not enough input arguments.
Can someone please explain what is the problem?

回答 (3 件)

madhan ravi
madhan ravi 2018 年 10 月 9 日
編集済み: madhan ravi 2018 年 10 月 9 日
X0 = [90*pi/180 ; 1.2];
[tout,yout] = ode45(@Pendulum, [0 5], X0)
plot(tout,yout(:,1),'r' )
hold on
plot(tout,yout(:,2),'b')
function DY = Pendulum(theta,w,l)
%PENDULUM
w=1; % error was here because w and l were not defined , I gave w and l as an example change it according to your values
l=2;
% DY = PENDULUM(THETA,W,L)
% This function was generated by the Symbolic Math Toolbox version 8.2.
% 09-Oct-2018 12:30:20
DY = [w;(sin(theta).*(-9.81e2./1.0e2))./l];
end
  8 件のコメント
Stephan
Stephan 2018 年 10 月 10 日
編集済み: Stephan 2018 年 10 月 10 日
@Madhan: The ode of pendulum is a second order ode. The system of odes given here is the converted first order sytem which is equivalent to the equation of pendulum i think.
madhan ravi
madhan ravi 2018 年 10 月 10 日
@stephen maybe but I’m stuck at this point .

サインインしてコメントする。


Walter Roberson
Walter Roberson 2018 年 10 月 9 日
ode45(@(theta, w) Pendulum(theta, w, SomeValueForL)
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 10 月 9 日
You are copying all of w into the output and another value as well. w is length 2 so the result is length 3. You should probably be using w(2) instead of w in constructing the output
Walter Roberson
Walter Roberson 2018 年 10 月 9 日
w = sym('w', [2 1]);
syms theta L
om_d = .... something involving w, theta, and L
Y = [w(1) ; om_d];
matlabFunction(Y, 'File', 'Pendulum', 'Vars', {theta w L},...
'Outputs',{'DY'});
X0 = [90*pi/180 ; 1.2];
L = 2;
[tout,yout] = ode45(@(theta, w) Pendulum(theta, w, L) , [0 5], X0);
... When I am typing on my phone in the middle of the night, I sometimes only give the outline of the change, as editing by poking with one figure is not all that productive.

サインインしてコメントする。


Stephan
Stephan 2018 年 10 月 9 日
編集済み: Stephan 2018 年 10 月 10 日
Edit
Here is another approach, inspired by Walters answer:
syms phi(t) g l
eqn = diff(phi,t,2) + g/l * sin(phi) == 0;
eqn = odeToVectorField(eqn);
Pendulum = matlabFunction(eqn, 'File', 'Pendulum','vars',{'t','Y','g','l'},'Outputs',{'DY'});
X0 = [90*pi/180, 1.2];
l = 2;
g = 9.81;
[tout,yout] = ode45(@(t,Y)Pendulum(t,Y,g,l), [0 5], X0);
plot(tout,yout(:,1),'r',tout,yout(:,2),'b')
With the resulting Pendulum.m file:
function DY = Pendulum(t,Y,g,l)
%PENDULUM
% DY = PENDULUM(T,Y,G,L)
% This function was generated by the Symbolic Math Toolbox version 8.2.
% 10-Oct-2018 08:21:21
DY = [Y(2);-(g.*sin(Y(1)))./l];
Theta and w are Y(1) and Y(2) here.
Best regards
Stephan
  3 件のコメント
Stephan
Stephan 2018 年 10 月 9 日
Then you should use the answer from Walter.
Stephan
Stephan 2018 年 10 月 10 日
See my edited answer

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by