combining function files to one .m file

hi everyone, my question is on how to combine function files.
i am running a plot vs time on a double pendulum and i haven't been able to get the script to run without having it run off 4 files. But for submission i have to submit it in one function file
function varargout=dp_S(t,y,flag)
switch flag
case ''
varargout{1}=dp_p(t,y);
case 'mass'
varargout{1}=dp_m(t,y);
otherwise
error(['unknown flag ''' flag '''.']);
end
function m =dp_m(t,y)
M1=1;
M2=1;
g=9.81;
l1=1;
l2=1;
a=[1,0,0,0];
b=[0,(M1+M2)*l1,0,M2*l2*cos(y(3)-y(1))];
c=[0,0,1,0];
d=[0,M2*l1*cos(y(3)-y(1)),0,M2*l2];
m=[a;b;c;d];
function p= dp_p(t,y)
M1=1;
M2=1;
g=9.81;
l1=1;
l2=1;
a=M2*9.81;
b=M1*9.81;
p=zeros(4,1);
p(1)=y(2);
p(2)=-(b+a)*sin(y(1))+M2*l2*(y(4)^2)*sin(y(3)-y(1));
p(3)=y(4);
p(4)=-a*sin(y(3))-M2*l1*(y(2)^2)*sin(y(3)-y(1));
tspan=[0 25];
y0=[0.1;0;0.2;0];
options=odeset('mass','M(t,y)');
[t,y]=ode113('dp_S',tspan,y0,options);
subplot(2,1,1)
plot(t,y(:,1))
grid
xlabel('Time')
ylabel('Theta1')
subplot(2,1,2)
plot(t,y(:,3))
grid
xlabel('Time')
ylabel('Theta2')

Respuestas (1)

Walter Roberson
Walter Roberson el 12 de Ag. de 2015

1 voto

If that code is working when it is in separate files then you must be using MATLAB 5.3 (R11) or earlier, as the ode solvers worked differently before function handles were introduced. Documentation for such an early version is not readily available; I do not know if any of the regular volunteers keeps such an early version on hand.

5 comentarios

garrett schander
garrett schander el 12 de Ag. de 2015
I am running 2015 student edition
Walter Roberson
Walter Roberson el 12 de Ag. de 2015
Nope. If you were running MATLAB 2015A Student Edition then the code would not run when it was in separate files. Your mass matrix is not set validly for any modern MATLAB. As you say that you have the code working, you cannot be using a modern MATLAB.
garrett schander
garrett schander el 12 de Ag. de 2015
Yes I am using the 2015 edition. It came with my asu course. With the script file inn one script and the fictions in a main function file with the 2 sub functions it piles up 2 plots 1 for theta 1 and the other for theta 2
Stephen23
Stephen23 el 12 de Ag. de 2015
Editada: Stephen23 el 12 de Ag. de 2015
The ode113 documentation clearly states that its first argument is " odefun A function handle that evaluates the right side of the differential equations", whereas you are providing it with a string like this:
ode113('dp_S',...
As Walter Roberson has tried to point out to you, this has not been a valid syntax for years and years... the best solution is to read the documentation.

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Ag. de 2015

Comentada:

el 12 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by