Symbolic manipulation and integration / not enough input argument

1 次查看(过去 30 天)
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.

请先登录,再进行评论。


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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by