Plotting elements from a function
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm trying to plot the position and velocity from a created function in twos different plots using subplot, the function solves a second order differential equation and I am getting the result in one plot, as I try to create two different graphics I got different errors or unexpected results, what could I do?
Here is part of my code:
yu = [x0 y0 z0 Vx0 Vy0 Vz0]; % Input y: position and velocity.
% Definition of time step 1
opts1 = odeset('InitialStep',5,'MaxStep',5); % Time step define : 5[s]
[t,y] = ode23(@yprime, t, yu, opts1);
subplot(2,1,1);
plot(t,y,'-'); % Trying to plot position result data here
subplot(2,1,2);
plot(t,y,'-'); % Trying to plot velocity result data here
function [yp] = yprime(t,y)
%Compute the derivative of y.
%Input y: position and velocity from previous tutorial.
%Output yp: derivative of y.
GM = (6.67408e-11)*(5.9722e24);
yp = zeros(6,1);
yp(1) = y(4); % x0 = Vx0
yp(2) = y(5); % y0 = Vy0
yp(3) = y(6); % z0 = Vz0
r = sqrt((y(1))^2+(y(2))^2+(y(3))^2); %
yp(4) = (-GM/(r)^3)*y(1); % Vx0 = Ax0
yp(5) = (-GM/(r)^3)*y(2); % Vy0 = Ay0
yp(6) = (-GM/(r)^3)*y(3); % Vz0 = Az0
end
2 个评论
Prudhvi Peddagoni
2021-1-22
Hi,
Why are you plotting same variables t and y for both subplots? Is that a mistake or is there any reason behind it?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!