Symbolic manipulation and integration / not enough input argument

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)

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

Actually my w is a variable to integrate, the equation is: theta = integral of w
I dont think w (omega) is linear rising.
no limits on w, just the time span changes
can you show how w is evaluated??
w is just a symbolic variable
syms m l t theta thetad th(t) th_d(t) w w_d
@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.
@stephen maybe but I’m stuck at this point .

Sign in to comment.

ode45(@(theta, w) Pendulum(theta, w, SomeValueForL)

3 Comments

First error typing your code:
When the first argument to ode45 is a function handle, the tspan and y0 arguments must be
supplied.
If I put in tspan and y0 as suggested:
@(THETA,W)PENDULUM(THETA,W,2) returns a vector of length 3, but the length of initial conditions vector is 2. The vector returned by @(THETA,W)PENDULUM(THETA,W,2) and the initial conditions vector must have the same number of elements.
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
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.

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

I get that, but i can't modify the function pendulum like that because I must use matlabFunction
% Create set of ODE for pendulum solution
Y = [w ; om_d];
matlabFunction(Y, 'File', 'Pendulum', 'Vars', [theta w l],...
'Outputs',{'DY'});
where om_d is a symbolic function and w is a symbolic variable
Then you should use the answer from Walter.

Sign in to comment.

Categories

Asked:

on 9 Oct 2018

Commented:

on 10 Oct 2018

Community Treasure Hunt

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

Start Hunting!