Plotting elements from a function
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hugo Hernández Hernández
am 19 Jan. 2021
Bearbeitet: Hugo Hernández Hernández
am 22 Jan. 2021
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 Kommentare
Prudhvi Peddagoni
am 22 Jan. 2021
Hi,
Why are you plotting same variables t and y for both subplots? Is that a mistake or is there any reason behind it?
Hugo Hernández Hernández
am 22 Jan. 2021
Bearbeitet: Hugo Hernández Hernández
am 22 Jan. 2021
Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!