MATLAB Answers

Function will not run or produce answers

7 views (last 30 days)
Areg Arzoomanian
Areg Arzoomanian on 30 Sep 2020
Commented: Walter Roberson on 30 Sep 2020
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clear all;
clc
function [r, v] = rv_from_coe(coe,mu)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%{
This function computes the state vector (r,v) from the
classical orbital elements (coe).
mu - gravitational parameter (km^3;s^2)
coe - orbital elements [a e incl Om w TA]
where
a - semimajor axis (km)
e = eccentricity
incl = inclination of the orbit (rad)
Om = longitude of the ascending node (rad)
w = argument of perigee (rad)
TA = true anomaly (rad)
p - semi-latus rectum (km)
h - the magnitude of H (km^2/s)
R3_w - Rotation matrix about the z-axis through the angle w
R1_i - Rotation matrix about the x-axis through the angle i
R3_W - Rotation matrix about the z-axis through the angle Om
Q_pX - Matrix of the transformation from perifocal to geocentric
equatorial frame
rp - position vector in the perifocal frame (km)
vp - velocity vector in the perifocal frame (km/s)
r - position vector in the geocentric equatorial frame (km)
v - velocity vector in the geocentric equatorial frame (km/s)
User M-functions required: none
%}
% ----------------------------------------------
mu= 398600; %km3/s2
coe=[10632 0.3184 0.523409 1.74101 0.981203 2.56632]
a = coe(1);
e = coe(2);
incl = coe(3);
Om = coe(4);
w = coe(5);
TA = coe(6);
p = a*(1-e^2) ;
h = sqrt(mu*p) ;
%...Equations 4.45 and 4.46 (rp and vp are column vectors):
rp = (h^2/mu) * (1/(1 + e*cos(TA))) * (cos(TA)*[1;0;0] + sin(TA)*[0;1;0]);
vp = (mu/h) * (-sin(TA)*[1;0;0] + (e + cos(TA))*[0;1;0]);
%...Equation 4.34:
R3_W = [ cos(Om) sin(Om) 0
-sin(Om) cos(Om) 0
0 0 1];
%...Equation 4.32:
R1_i = [1 0 0
0 cos(incl) sin(incl)
0 -sin(incl) cos(incl)];
%...Equation 4.34:
R3_w = [ cos(w) sin(w) 0
-sin(w) cos(w) 0
0 0 1];
%...Equation 4.49:
Q_pX = (R3_w*R1_i*R3_W)';
%...Equations 4.51 (r and v are column vectors):
r = Q_pX*rp;
v = Q_pX*vp;
%...Convert r and v into row vectors:
r = r';
v = v';
end
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Every time I run my script I get this error, if I change the file name then I get no results in my workspace or command window? Please help, I'm not sure whats wrong.
Error: File: rv_from_coe.m Line: 5 Column: 19
Local function name must be different from the script name.

  0 Comments

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 30 Sep 2020
Edited: madhan ravi on 30 Sep 2020
Remove first 2 lines.
Your function doesn’t need any input arguments.
You must call a function not simply clicking the green triangular button.
doc function % a must read

  2 Comments

Areg Arzoomanian
Areg Arzoomanian on 30 Sep 2020
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function [r, v] = rv_from_coe(coe,mu)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%{
This function computes the state vector (r,v) from the
classical orbital elements (coe).
mu - gravitational parameter (km^3;s^2)
coe - orbital elements [a e incl Om w TA]
where
a - semimajor axis (km)
e = eccentricity
incl = inclination of the orbit (rad)
Om = longitude of the ascending node (rad)
w = argument of perigee (rad)
TA = true anomaly (rad)
p - semi-latus rectum (km)
h - the magnitude of H (km^2/s)
R3_w - Rotation matrix about the z-axis through the angle w
R1_i - Rotation matrix about the x-axis through the angle i
R3_W - Rotation matrix about the z-axis through the angle Om
Q_pX - Matrix of the transformation from perifocal to geocentric
equatorial frame
rp - position vector in the perifocal frame (km)
vp - velocity vector in the perifocal frame (km/s)
r - position vector in the geocentric equatorial frame (km)
v - velocity vector in the geocentric equatorial frame (km/s)
User M-functions required: none
%}
% ----------------------------------------------
mu= 398600; %km3/s2
coe=[10632 0.3184 0.523409 1.74101 0.981203 2.56632];
a = coe(1);
e = coe(2);
incl = coe(3);
Om = coe(4);
w = coe(5);
TA = coe(6);
p = a*(1-e^2) ;
h = sqrt(mu*p) ;
%...Equations 4.45 and 4.46 (rp and vp are column vectors):
rp = (h^2/mu) * (1/(1 + e*cos(TA))) * (cos(TA)*[1;0;0] + sin(TA)*[0;1;0]);
vp = (mu/h) * (-sin(TA)*[1;0;0] + (e + cos(TA))*[0;1;0]);
%...Equation 4.34:
R3_W = [ cos(Om) sin(Om) 0
-sin(Om) cos(Om) 0
0 0 1];
%...Equation 4.32:
R1_i = [1 0 0
0 cos(incl) sin(incl)
0 -sin(incl) cos(incl)];
%...Equation 4.34:
R3_w = [ cos(w) sin(w) 0
-sin(w) cos(w) 0
0 0 1];
%...Equation 4.49:
Q_pX = (R3_w*R1_i*R3_W)';
%...Equations 4.51 (r and v are column vectors):
r = Q_pX*rp;
v = Q_pX*vp;
%...Convert r and v into row vectors:
r = r';
v = v';
end
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thank you, but even when I do this nothing even appears in my work space
Walter Roberson
Walter Roberson on 30 Sep 2020
You have to invoke the function from the command line, in a way that assigns values to variables.
[MyR, MyV] = rv_from_coe();
The function is written to accept arguments, but there is no point in passing any arguments to the function as the function overwrites the variables involved .

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!