Plotting elements from a function
조회 수: 1 (최근 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!