Plotting elements from a function

조회 수: 1 (최근 30일)
Hugo Hernández Hernández
Hugo Hernández Hernández 2021년 1월 19일
편집: Hugo Hernández Hernández 2021년 1월 22일
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
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?
Hugo Hernández Hernández
Hugo Hernández Hernández 2021년 1월 22일
편집: Hugo Hernández Hernández 2021년 1월 22일
Hi,
It was a misunderstanding by my side, I didn't understand at all how the function worked and also the use of the integer ode23 MATLAB structure, at the end I got the expected plot, here is how I solved my problem:
yu = [x0 y0 z0 Vx0 Vy0 Vz0]; % Input y: position and velocity.
% Definition of time step 1
% opts1 = odeset('InitialStep',5,'MaxStep',5); % if options is used, 5 [s] step
%[t,y] = ode23(@yprime, t, yu); % ode23 Matlab method computation
%[t,y] = ode45(@yprime, t, yu); % ode45 MATLAB method computation
[t,y] = ode113(@yprime, t, yu); % ode113 MATLAB method computation
Nu_x = y(:,1); % Nmx
Nu_y = y(:,2); % Nmy
Nu_z = y(:,3); % Nmz
Nu_vx = y(:,4); % Nmvx
Nu_vy = y(:,5); % Nmvy
Nu_vz = y(:,6); % Nmvz
% Differences between numerical and analythical solutions.
SZ1 = Nmx - Sen1;
SZ2 = Nmy - Sen2;
SZ3 = Nmz - Sen3;
SZ4 = Nmvx - VSen1;
SZ5 = Nmvy - VSen2;
SZ6 = Nmvz - VSen3;
figure();
subplot(2,1,1);
plot(t, SZ1,'Linewidth',1);
hold on
plot(t, SZ2,'Linewidth',1);
plot(t, SZ3,'Linewidth',1);
grid on
xlabel('time[s]');
ylabel('Difference in position [m]');
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

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by