Need to plot a graph of -y(t) and x(t) on MATLAB

4 visualizaciones (últimos 30 días)
Paul Jones
Paul Jones el 18 de Feb. de 2021
Editada: Rik el 19 de Feb. de 2021
I dont know what im doing if im being honest and i really need help. I need to plot a graph of -y(t) and x(t).
function [t,x,pos]=lab1(r0,k)
% assume pendulum mass m=1
m=1;
dt=0.01; % integration time step
tspan=0:dt:500; % timespan of integration from 0 to 500
opt=odeset('Reltol',1e-9,'AbsTol',1e-9);
x0= input('enter initial condition in the form of [xinit0;vxinit0;yinit0;vyinit0] : ');
x0=[r0;0;0;0];
[t,x]=ode45(@pend,tspan,x0,opt,k,m,r0);
pos(:,1)=x(:,1).*sin(x(:,3));
pos(:,2)=-x(:,1).*cos(x(:,3));
function RHS=pend(t,x,k,m,r0)
r=sqrt(x(1)^2+x(3)^2);
dr=r-r0;
RHS(1,1)=0;
RHS(2,1)=0;
RHS(3,1)=0;
RHS(4,1)=0;
end
end
  11 comentarios
Rik
Rik el 19 de Feb. de 2021
You can edit your question to make it look like the original again. There is currently no direct option to restore a previous version other than to edit your question and put back the same information.

Iniciar sesión para comentar.

Respuestas (2)

Alan Stevens
Alan Stevens el 18 de Feb. de 2021
Here are the equations that you will need to include in your pend function.
Now let's see you implement them!

Steven Lord
Steven Lord el 18 de Feb. de 2021
pos(:,1)=x(:,1).*sin(x(:,3));
pos(:,2)=-x(:,1).*cos(x(:,3));
So pos(:, 1) is the x(t) you're trying to plot and pos(:, 2) is the -y(t)? If so just call the plot function.
plot(pos(:, 1), pos(:, 2))
  2 comentarios
Alan Stevens
Alan Stevens el 18 de Feb. de 2021
Here's the syntax using the equations I generated. You should be able to modify this to use your equations if you think they are correct:
% Data
r0 = 5; % m
k = 10; % N/m
m = 1; % kg
g = 9.81; % m/s^2
dt = 0.1; % s
tspan = 0:dt:20;
% Initial conditions
xy0 = [r0, 0, 0, 0]; % [x0 y0, vx0, vy0]
[t, xy] = ode45(@(t,xy) rates(t,xy,r0,k,m,g), tspan, xy0);
x = xy(:,1); y = xy(:,2);
vx = xy(:,3); vy = xy(:,4);
plot(t,x,t,-y),grid
xlabel('t'),ylabel('x and -y')
legend('x','-y')
function dxydt = rates(~,xy,r0,k,m,g)
x = xy(1); y = xy(2);
vx = xy(3); vy = xy(4);
r = sqrt(x^2+y^2);
theta = atan(-x/y);
dxydt = [vx;
vy;
-k/m*(r-r0)*sin(theta);
-g+k/m*(r-r0)*cos(theta)];
end
The above results in
Alan Stevens
Alan Stevens el 18 de Feb. de 2021
I put nothing at the beginning - it's a script in its own right.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by