Define functions in code and edit code?
Show older comments
Can you define the following 3 vectorized functions in the code: launch_yr_4d(y2d), apogee, and perigee. The code is below:
% Fetch TLE data from the URL
url_recent = 'https://celestrak.org/NORAD/elements/gp.php?GROUP=visual&FORMAT=tle';
line_list = string(splitlines(webread(url_recent)));
% Ensure line_list has a multiple of 3 number of lines for proper reshaping
line_list = line_list(1:floor(numel(line_list)/3)*3);
% Reshape the line list to separate each TLE into its own column
tle_data = reshape(line_list, 3, []);
% Define Constant used in function
earth_mean_radius = 6371; % km
% Define the functions: launch_yr_4d(y2d), apogee(mean_motion, eccentricity), perigee(mean_motion, eccentricity)
launch_yr_4d = @(y2d) (y2d >= 24) .* (1900 + y2d) + (y2d < 24) .* (2000 + y2d);
apogee = @(mean_motion, eccentricity) (8681663.653 ./ mean_motion) .^ (2/3) .* (1 + eccentricity) - earth_mean_radius;
perigee = @(mean_motion, eccentricity) (8681663.653 ./ mean_motion) .^ (2/3) .* (1 - eccentricity) - earth_mean_radius;
% Extract satellite numbers, launch years, eccentricities, and mean motions in batches
satellite_numbers = str2double(extractBetween(tle_data(2,:), 3, 7));
launch_years_2digit = str2double(extractBetween(tle_data(2,:), 10, 11));
launch_years = launch_yr_4d(launch_years_2digit);
eccentricities = str2double(extractBetween(tle_data(3,:), 27, 33)) * 0.0000001;
mean_motions = str2double(extractBetween(tle_data(3,:), 53, 63));
% Compute perigee and apogee heights in batches
hps = perigee(mean_motions, eccentricities);
has = apogee(mean_motions, eccentricities);
% Convert numerical values to strings with formatting
satNumStr = sprintfc('%05d', satellite_numbers);
launchYearStr = sprintfc('%04d', launch_years);
hpStr = sprintfc('%.2f', round(hps, 2));
haStr = sprintfc('%.2f', round(has, 2));
% Concatenate all string parts
outputStrings = strcat(satNumStr, " ", launchYearStr, " ", hpStr, " ", haStr, " ", string(tle_data(1, :)));
% Join and print all strings
fprintf('%s\n', join(outputStrings, newline));
16 Comments
Steven Lord
on 26 Mar 2024
From the prohibition against using reshape or round, this sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Bob Meyes
on 26 Mar 2024
Steven Lord
on 26 Mar 2024
You have defined them.
% Define the functions: launch_yr_4d(y2d), apogee(mean_motion, eccentricity), perigee(mean_motion, eccentricity)
launch_yr_4d = @(y2d) (y2d >= 24) .* (1900 + y2d) + (y2d < 24) .* (2000 + y2d);
apogee = @(mean_motion, eccentricity) (8681663.653 ./ mean_motion) .^ (2/3) .* (1 + eccentricity) - earth_mean_radius;
perigee = @(mean_motion, eccentricity) (8681663.653 ./ mean_motion) .^ (2/3) .* (1 - eccentricity) - earth_mean_radius;
So it's not clear what you're asking us to do.
Bob Meyes
on 26 Mar 2024
Steven Lord
on 26 Mar 2024
Edited: Steven Lord
on 26 Mar 2024
Okay, so how did you try to rewrite the code to define the functions that way? What happened when you tried to run the code with those rewritten functions?
If you're not sure how to write functions in general, read through this documentation page (especially the Syntax for Function Definition section.) Then use the function you showed above and the functions on the documentation page as a model to rewrite the functions you have currently defined as anonymous functions into "named" functions.
And please keep discussion of this question here, in this post. There's no need to create a copy of the question as a new post; just add comments here.
Bob Meyes
on 26 Mar 2024
Shift the functions to the end/bottom of the script and then run your code.
Though there is atleast one undefined variable in your code -
% Fetch TLE data from the URL
url_recent = 'https://celestrak.org/NORAD/elements/gp.php?GROUP=visual&FORMAT=tle';
line_list = string(splitlines(webread(url_recent)));
% Ensure line_list has a multiple of 3 number of lines for proper reshaping
line_list = line_list(1:floor(numel(line_list)/3)*3);
% Reshape the line list to separate each TLE into its own column
tle_data = reshape(line_list, 3, []);
% Extract satellite numbers, launch years, eccentricities, and mean motions in batches
satellite_numbers = str2double(extractBetween(tle_data(2,:), 3, 7));
launch_years_2digit = str2double(extractBetween(tle_data(2,:), 10, 11));
launch_years = launch_yr_4d(launch_years_2digit);
eccentricities = str2double(extractBetween(tle_data(3,:), 27, 33)) * 0.0000001;
mean_motions = str2double(extractBetween(tle_data(3,:), 53, 63));
% Compute perigee and apogee heights in batches
perigee_heights = perigee(mean_motions, eccentricities);
apogee_heights = apogee(mean_motions, eccentricities);
% Convert numerical values to strings with formatting
satNumStr = sprintfc('%05d', satellite_numbers);
launchYearStr = sprintfc('%04d', launch_years);
hpStr = sprintfc('%.2f', hps);
haStr = sprintfc('%.2f', has);
% Concatenate all string parts
outputStrings = strcat(satNumStr, " ", launchYearStr, " ", hpStr, " ", haStr, " ", string(tle_data(1, :)));
% Join and print all strings
fprintf('%s\n', join(outputStrings, newline));
%Any and all functions definitions in a script shifted to bottom of the
%script page
function launch_year = launch_yr_4d(y2d)
launch_year = (y2d >= 24) .* (1900 + y2d) + (y2d < 24) .* (2000 + y2d);
end
function apogee_height = apogee(mean_motion, eccentricity)
earth_mean_radius = 6371.0; % Earth's mean radius in kilometers
apogee_height = (8681663.653 ./ mean_motion) .^ (2/3) .* (1 + eccentricity) - earth_mean_radius;
end
function perigee_height = perigee(mean_motion, eccentricity)
earth_mean_radius = 6371.0; % Earth's mean radius in kilometers
perigee_height = (8681663.653 ./ mean_motion) .^ (2/3) .* (1 - eccentricity) - earth_mean_radius;
end
Bob Meyes
on 26 Mar 2024
Dyuman Joshi
on 26 Mar 2024
"Is there any way to not have the reshape fucntion in line 7 to make the code simpler?"
Why do you think that the presence of reshape() makes the function not simpler/complex?
"Also is there any reason why functions need to be at the bottom of the page. "
That is how the syntax was initially defined.
However, version R2024a onwards, functions can be placed anywhere in a script.
Bob Meyes
on 26 Mar 2024
Dyuman Joshi
on 26 Mar 2024
Once again, why do you want to remove reshape()?
That line does an operation, and the rest of code is written according to the operation performed. If you remove it, you will need to adjust the rest of code accordingly.
Is there a restriction on its usage?
Steven Lord
on 26 Mar 2024
Yes, as of release R2024a you can define functions almost anywhere inside a script. The "almost" excludes inside conditional statements.
Bob Meyes
on 26 Mar 2024
Voss
on 26 Mar 2024
An alternative to using reshape:
tle_data = [line_list(1:3:end) line_list(2:3:end) line_list(3:3:end)].'
Rik
on 27 Mar 2024
Why is it restricted? Is this Cody? Or homework?
Dyuman Joshi
on 27 Mar 2024
I don't think this is a Cody question, most likely a HW assignment.
Answers (0)
Categories
Find more on Gravitation, Cosmology & Astrophysics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!