Hello, I'm trying to create a plot for an equation of motion defined as a function.
Show older comments
I am trying to create a plot for this function but when I put 'end' before the initial conditions I get an error that reads "Function definitions in a script must appear at the end of the file. Move statements to before the function definitions." I've tried to move the initial conditions to be before the function definition and that didn't run properly. I've also tried to move 'end' to the end of the code and the plots didn't show up at all. I would appreciate any help for this. Thanks!
function dXdt=eom(~,X)
%%First Conditions
theta=X(1); %Position HC
thetadot=X(2); %Velocity HC
phi=X(3); %Position Disk
phidot=X(4); %Velocity Disk
%EOMS
thetaddot=-(g/R)*sin(theta); %Half cylinder
phiddot=-(g/r)*sin(phi); % Disk
dXdt=[thetadot;thetaddot;phidot;phiddot];
end
%Initial conditions
thetazero=0;
phizero=pi/18;
Xzero=[thetazero;0;phizero;0];
%timeframe
tspan=[0,6];
%solution
[t,X]=ode45(@eom,tspan,Xzero);
%plot
figure
subplot(2,1,1);
plot(t,X(:,1));
xlabel('Time')
ylabel('\theta')
title('Angular Position of Half Cylinder')
subplot(2,1,2);
plot(t,X(:,3));
xlabel('Time')
ylabel('\phi')
title('Angular Position of Disk')
hold on
2 Comments
Les Beckham
on 4 Dec 2024
You haven't defined g, R, and r that are used in your eom function.
Amanda
on 4 Dec 2024
Accepted Answer
More Answers (1)
Please show (or even attach) the full and exact file. My suspicion is that the lines where you define those constants are ahead of the function declaration, but that you forgot to actually call the function you defined, something like:
x = 1
function y = timestwo(x)
y = 2*x;
surf(peaks)
end
While I've defined the timestwo function, I have not called that function so all this code does is define x to be 1. So there's no graphics plotted when I run this code.
If I'd put y = timestwo(x) immediately before or after the line x = 1 in the code, then it would perform the multiplication and create the peaks plot.
Categories
Find more on Loops and Conditional Statements 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!




