You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Define functions in code and edit code?
11 views (last 30 days)
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));
00694 1963 464.88 1287.62 ATLAS CENTAUR 2
00733 1964 764.58 812.41 THOR AGENA D R/B
00877 1964 645.03 754.12 SL-3 R/B
02802 1967 704.49 796.77 SL-8 R/B
03230 1968 569.05 607.13 SL-8 R/B
03597 1968 739.89 748.79 OAO 2
03669 1969 582.98 3453.08 ISIS 1
04327 1970 1044.43 1051.38 SERT 2
04814 1970 254.93 257.61 SL-3 R/B
05118 1971 468.90 519.18 SL-3 R/B
05560 1971 724.53 746.23 ASTEX 1
05730 1971 385.09 1280.28 SL-8 R/B
06073 1972 195.66 1293.35 COSMOS 482 DESCENT CRAFT
06153 1972 699.85 708.49 OAO 3 (COPERNICUS)
06155 1972 630.43 680.15 ATLAS CENTAUR R/B
08459 1975 763.11 785.39 SL-8 R/B
10114 1977 487.00 501.09 SL-3 R/B
10967 1978 748.53 750.94 SEASAT 1
11267 1979 594.02 614.77 SL-14 R/B
11574 1979 749.28 772.38 SL-8 R/B
11672 1980 595.68 619.22 SL-14 R/B
12139 1981 372.52 897.18 SL-8 R/B
12465 1981 484.17 521.18 SL-3 R/B
12585 1981 455.96 470.44 METEOR PRIRODA
12904 1981 524.37 553.48 SL-3 R/B
13068 1982 516.29 553.02 SL-3 R/B
13154 1982 538.47 589.85 SL-3 R/B
13403 1982 510.96 554.82 SL-3 R/B
13552 1982 394.93 413.20 COSMOS 1408
13553 1982 604.60 635.19 SL-14 R/B
13819 1983 480.31 513.57 SL-3 R/B
14032 1983 463.39 477.43 COSMOS 1455
14208 1983 491.84 534.10 SL-3 R/B
14372 1983 456.15 473.14 COSMOS 1500
14699 1984 526.18 550.09 COSMOS 1536
14819 1984 378.95 381.93 COSMOS 1544
14820 1984 608.80 634.91 SL-14 R/B
15483 1985 761.70 789.65 SL-8 R/B
15494 1985 431.15 433.84 COSMOS 1626
15772 1985 802.26 852.03 SL-12 R/B(2)
15945 1985 606.36 634.73 SL-14 R/B
16182 1985 837.54 851.83 SL-16 R/B
16496 1986 606.96 633.52 SL-14 R/B
16719 1986 488.99 500.73 COSMOS 1743
16792 1986 605.46 640.77 SL-14 R/B
16882 1986 609.30 636.84 SL-14 R/B
16908 1986 1485.82 1503.59 AJISAI (EGS)
17295 1987 483.59 487.61 COSMOS 1812
17567 1987 607.21 635.70 SL-14 R/B
17589 1987 840.70 871.98 COSMOS 1833
17590 1987 840.14 845.50 SL-16 R/B
17912 1987 609.88 637.67 SL-14 R/B
17973 1987 829.72 873.92 COSMOS 1844
18153 1987 608.71 637.25 SL-14 R/B
18187 1987 780.12 809.07 COSMOS 1867
18421 1987 471.52 484.43 COSMOS 1892
18749 1988 609.24 637.22 SL-14 R/B
18958 1988 543.23 549.07 COSMOS 1933
19046 1988 523.98 569.11 SL-3 R/B
19120 1988 818.54 850.58 SL-16 R/B
19210 1988 520.19 543.64 COSMOS 1953
19257 1988 757.40 786.45 SL-8 R/B
19573 1988 522.78 546.25 COSMOS 1975
19574 1988 608.97 636.73 SL-14 R/B
19650 1988 836.31 856.10 SL-16 R/B
20261 1989 504.62 2373.78 INTERCOSMOS 24
20262 1989 505.20 2441.25 SL-14 R/B
20323 1989 685.47 784.29 DELTA 1 R/B
20443 1990 762.04 776.92 ARIANE 40 R/B
20453 1990 408.96 704.04 DELTA 2 R/B(1)
20465 1990 557.03 574.19 COSMOS 2058
20466 1990 614.20 644.14 SL-14 R/B
20511 1990 622.92 644.13 SL-14 R/B
20580 1990 521.91 525.37 HST
20625 1990 838.48 861.70 SL-16 R/B
20663 1990 497.48 664.55 COSMOS 2084
20666 1990 514.28 649.72 SL-6 R/B(2)
20775 1990 367.84 1052.41 SL-8 R/B
21088 1991 964.82 996.23 SL-8 R/B
21397 1991 571.22 592.26 OKEAN-3
21422 1991 580.13 596.84 COSMOS 2151
21423 1991 621.27 647.49 SL-14 R/B
21574 1991 747.27 794.35 ERS-1
21610 1991 756.78 762.98 ARIANE 40 R/B
21819 1991 445.71 2855.22 INTERCOSMOS 25
21876 1992 960.88 1010.99 SL-8 R/B
21938 1992 965.20 1009.60 SL-8 R/B
22219 1992 839.24 867.61 COSMOS 2219
22220 1992 836.19 851.59 SL-16 R/B
22236 1992 589.54 612.88 COSMOS 2221
22285 1992 844.12 853.18 SL-16 R/B
22286 1992 585.69 613.83 COSMOS 2228
22566 1993 840.61 857.48 SL-16 R/B
22626 1993 588.21 613.36 COSMOS 2242
22803 1993 829.08 856.17 SL-16 R/B
22830 1993 784.88 799.31 ARIANE 40 R/B
23087 1994 847.07 858.21 COSMOS 2278
23088 1994 846.03 854.08 SL-16 R/B
23343 1994 637.86 648.19 SL-16 R/B
23405 1994 845.90 850.47 SL-16 R/B
23561 1995 764.04 775.48 ARIANE 40+ R/B
23705 1995 840.69 856.18 SL-16 R/B
24298 1996 847.89 868.07 SL-16 R/B
24883 1997 782.44 785.81 ORBVIEW 2 (SEASTAR)
25400 1998 806.86 819.58 SL-16 R/B
25407 1998 841.47 850.77 SL-16 R/B
25544 1998 421.96 428.47 ISS (ZARYA)
25732 1999 813.23 865.76 CZ-4B R/B
25860 1999 640.79 643.45 OKEAN-O
25861 1999 625.38 648.18 SL-16 R/B
25876 1999 497.48 1250.04 DELTA 2 R/B
25977 1999 622.81 624.20 HELIOS 1B
25994 1999 700.99 702.05 TERRA
26070 2000 834.96 859.78 SL-16 R/B
26474 2000 538.85 601.64 TITAN 4B R/B
27386 2002 769.52 771.49 ENVISAT
27422 2002 789.55 808.10 IDEFIX & ARIANE 42P R/B
27424 2002 702.18 704.76 AQUA
27432 2002 816.18 887.53 CZ-4B R/B
27597 2002 804.39 805.04 MIDORI II (ADEOS-II)
27601 2002 738.05 841.67 H-2A R/B
28059 2003 686.87 755.02 CZ-4B R/B
28222 2004 477.85 525.92 CZ-2C R/B
28353 2004 848.71 854.31 SL-16 R/B
28415 2004 500.75 559.43 CZ-4B R/B
28480 2004 709.31 913.37 CZ-2C R/B
28499 2004 566.73 692.02 ARIANE 5 R/B
28738 2005 493.69 509.46 CZ-2D R/B
28773 2005 451.73 452.67 SUZAKU (ASTRO-EII)
28931 2006 681.48 684.91 ALOS (DAICHI)
28932 2006 521.92 626.86 H-2A R/B
29228 2006 550.91 556.23 RESURS-DK 1
29252 2006 458.14 478.17 GENESIS 1
29507 2006 525.44 581.86 CZ-4B R/B
31114 2007 790.93 875.87 CZ-2C R/B
31598 2007 628.67 630.56 COSMO-SKYMED 1
31789 2007 451.12 493.64 GENESIS 2
31792 2007 849.89 864.60 COSMOS 2428
31793 2007 848.96 852.61 SL-16 R/B
33504 2009 488.17 510.14 KORONAS-FOTON
37731 2011 604.89 685.51 CZ-2C R/B
38341 2012 574.91 648.51 H-2A R/B
39271 2013 302.70 780.50 CUSAT 2 & FALCON 9 R/B
39358 2013 598.27 622.21 SHIJIAN-16 (SJ-16)
39364 2013 355.02 381.95 CZ-2C R/B
39679 2014 421.87 616.61 SL-4 R/B
39766 2014 636.91 639.19 ALOS-2
40354 2014 357.11 389.70 SL-27 R/B
41038 2015 629.39 633.54 YAOGAN-29
41337 2016 556.71 572.52 ASTRO-H (HITOMI)
42758 2017 523.09 524.49 HXMT (HUIYAN)
43521 2018 455.11 538.11 CZ-2C R/B
43641 2018 628.64 630.72 SAOCOM 1A
43682 2018 503.09 566.05 H-2A R/B
46265 2020 628.74 630.64 SAOCOM 1B
48274 2021 376.22 390.21 CSS (TIANHE)
48865 2021 465.82 477.25 COSMOS 2550
51842 2022 379.51 422.51 2022-019U
52794 2022 452.20 569.38 CZ-2C R/B
53131 2022 309.46 326.68 CZ-2C R/B
57800 2023 566.80 580.89 XRISM
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.
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.
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
I tried to rewrite the fucntions as follows but I got an error statement that Function definitions in a script must appear at the end of the file
% 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, []);
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
% 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));
Dyuman Joshi
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);
Unrecognized function or variable '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
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
Okay thanks, but is there a way to rewrite the code so a reshape function is not needed. This is the code I have right now for reference
% 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', perigee_heights);
haStr = sprintfc('%.2f', apogee_heights);
% 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));
%Define functions
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
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.
Dyuman Joshi
on 27 Mar 2024
I don't think this is a Cody question, most likely a HW assignment.
Answers (0)
See Also
Categories
Find more on Reference Applications in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)