Clear Filters
Clear Filters

Symbolic manipulation and integration / not enough input argument

1 view (last 30 days)
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?

Answers (3)

madhan ravi
madhan ravi on 9 Oct 2018
Edited: madhan ravi on 9 Oct 2018
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 Comments
Stephan
Stephan on 10 Oct 2018
Edited: Stephan on 10 Oct 2018
@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.

Sign in to comment.


Walter Roberson
Walter Roberson on 9 Oct 2018
ode45(@(theta, w) Pendulum(theta, w, SomeValueForL)
  3 Comments
Walter Roberson
Walter Roberson on 9 Oct 2018
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 on 9 Oct 2018
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.

Sign in to comment.


Stephan
Stephan on 9 Oct 2018
Edited: Stephan on 10 Oct 2018
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 Comments

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!