Plotting ode 45 results

1 回表示 (過去 30 日間)
imran shaikh
imran shaikh 2021 年 10 月 9 日
編集済み: imran shaikh 2021 年 10 月 9 日
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 件のコメント
Stephen23
Stephen23 2021 年 10 月 9 日
Note that inside your function the output f is always just an array of zeros, while the variable Xdot is totally unused.

サインインしてコメントする。

回答 (1 件)

Stephen23
Stephen23 2021 年 10 月 9 日
編集済み: Stephen23 2021 年 10 月 9 日
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 件のコメント
Stephen23
Stephen23 2021 年 10 月 9 日
"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.
Stephen23
Stephen23 2021 年 10 月 9 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by