Plotting ode 45 results

Matlab 2020B was used to draft code:
Question
Drafted Code:
function [f] = twobody (t,X)
% Designed to call two body eqautions of motion
%x(1)= x position;
%x(2)= y position;
%x(3) = z position;
%x (4) = x velocity;
%x (5) = y velocity;
%x (6) = z velocity;
mu = 398600; % km^3/s^2
f = zeros (size(X));
Xdot(1:3) = X (4:6);
r=norm(X(1:3));
Xdot(4:6)= (-mu/r^3)*X(1:3);
end
Script file:
R0 = [6510.75956901532 2676.16546382759 333.33402937319]; %km. Transpose of column matrix
V0 = [-2.23202460862428 9.49860960555864 1.18311436869621]; % Km/s. Transpose of column matrix
X0= [R0,V0]; % Column vector input to ODE45
options=odeset; options = odeset('RelTol',1e-12,'AbsTol', 1e-12);
t = [0:10:86400]; % 24hrs converted to sec with 10 sec step size
[t,X]=ode45(@twobody,t,X0,options);
clear all
plot(t,R0);
xlabel('Time(seconds)');
ylabel('Radius (Km)');
Error message:
Unrecognized function or variable 't'.
Error in twobodyscript (line 9)
plot(t,R0);
Can someone help resolve the error message and help plot the three plots? and i am not sure how to approach below question yet, some help would be greatly appreciated.

1 comentario

Stephen23
Stephen23 el 9 de Oct. de 2021
Note that inside your function the output f is always just an array of zeros, while the variable Xdot is totally unused.

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 9 de Oct. de 2021
Editada: Stephen23 el 9 de Oct. de 2021

0 votos

clear all % <- get rid of this.
It seems that the main purpose of CLEAR ALL is to introduce bugs into beginners' code when they use it totally inappropriately, e.g. at the start of every script or in the middle of their code:

3 comentarios

imran shaikh
imran shaikh el 9 de Oct. de 2021
Hi Stephen,
Thank you, variable 't' issue was resolved.
but another error popped up as follows:
>> twobodyscript
Error using plot
Vectors must be the same length.
Error in twobodyscript (line 8)
plot(t,R0);
I imagine this is because time t is from 0 to 86400 and R0 is only three numbers. Any idea on how to resolve that?
Stephen23
Stephen23 el 9 de Oct. de 2021
"I imagine this is because time t is from 0 to 86400 and R0 is only three numbers"
Yes.
"Any idea on how to resolve that?"
Not really, because you did not explain what you expect to happen when you try to plot different numbers of X and Y values.
Making a few guesses about the meaning of your variables, perhaps something like this:
R0 = [6510.75956901532,2676.16546382759,333.33402937319]; % km
V0 = [-2.23202460862428,9.49860960555864,1.18311436869621]; % Km/s
Op = odeset('RelTol',1e-12,'AbsTol', 1e-12);
ts = 0:10:86400; % 24hrs converted to sec with 10 sec step size
[t,Y] = ode45(@twobody,ts,[R0,V0],Op);
plot(t,Y(:,1:3));
ylabel('Radius (km)');
xlabel('Time (seconds)');
function Xdot = twobody (t,X)
mu = 398600; % km^3/s^2
Xdot = zeros(size(X));
Xdot(1:3) = X(4:6);
r = norm(X(1:3));
Xdot(4:6)= (-mu/r^3)*X(1:3);
end

Iniciar sesión para comentar.

Preguntada:

el 9 de Oct. de 2021

Editada:

el 9 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by