How to select a range within a variable?

2 views (last 30 days)
needsmorestruts on 14 May 2021
Commented: the cyclist on 18 May 2021
I want to be able to select a range from within a variable to control the length of the ellipse I'm plotting. I want the range of the variable to be under a certain value. For example, I want to extract the values of x, under -4000 for a variable x = [1000:-2:-5000].
atx = 30000;
Rb = 42164.17;
nu = 157.67;
e = 0.781;
RE = 6378;
Ra = 200 + RE;
theta = linspace(0,2*pi,1000);
theta1 = linspace(0,pi,1000);
xre = RE.*cos(theta);
yre = RE.*sin(theta);
plot(xre,yre,"Color","Black", "LineWidth", 2);
hold on
x2 = Rb.*cos(theta);
y2 = Rb.*sin(theta);
axis("square")
plot(x2,y2,"Color","Black");
hold on;
axis("square")
plot(Rb*cosd(nu),Rb*sind(nu),'rx',"MarkerSize",20, "Color","Black");
hold on;
x3 = atx.*cos(theta1)-atx+Ra;
y3 = atx.*sqrt(1-e^2).*sin(theta1);
x4 = x3(:,1:674);
y4 = y3(:,1:674);
axis("square")
plot(x4,y4,'--',"Color","Blue");
hold on;
My code here is trying to plot an elliptical orbit transfer trajectory between a circular starting and circular final orbit given the transfer ellipse's semi-major axis, I've added a figure to show what I want to achieve. In the code I've added, the number 674 represents when the value of x3> -Rb. This is so that the transfer ellipse does not exceed the outer orbit.
If there is a more efficient way to do this, please let me know!

the cyclist on 14 May 2021
Take a look at this small example.
First, define a smaller x vector:
x = 1000:-1000:-6000
x = 1×8
1000 0 -1000 -2000 -3000 -4000 -5000 -6000
The variable
x_is_small = (x < -4000)
x_is_small = 1×8 logical array
0 0 0 0 0 0 1 1
is a logical variable that is True where x<-4000, and False otherwise.
You can use that variable to index into other variable, such extracting the values of x that are less than -4000:
small_x_values = x(x_is_small)
small_x_values = 1×2
-5000 -6000
We could also have just done that without defining the intermediate variable:
x(x<-4000)
ans = 1×2
-5000 -6000
I hope that helps.
the cyclist on 18 May 2021
There's some confusion here. The code x(x < -4000) definitely pulls out the values of x that are less than -4000, regardless of how x was derived.
Here is your code, where instead of selecting the hard-coded value of 674, I set the condition x3 > - Rb.
atx = 30000;
Rb = 42164.17;
nu = 157.67;
e = 0.781;
RE = 6378;
Ra = 200 + RE;
theta = linspace(0,2*pi,1000);
theta1 = linspace(0,pi,1000);
xre = RE.*cos(theta);
yre = RE.*sin(theta);
plot(xre,yre,"Color","Black", "LineWidth", 2);
hold on
x2 = Rb.*cos(theta);
y2 = Rb.*sin(theta);
axis("square")
plot(x2,y2,"Color","Black");
hold on;
axis("square")
plot(Rb*cosd(nu),Rb*sind(nu),'rx',"MarkerSize",20, "Color","Black");
hold on;
x3 = atx.*cos(theta1)-atx+Ra;
y3 = atx.*sqrt(1-e^2).*sin(theta1);
x4 = x3(:,x3 > -Rb); % I changed this code to remove hard-coded 674
y4 = y3(:,x3 > -Rb); % I changed this code to remove hard-coded 674
axis("square")
plot(x4,y4,'--',"Color","Blue");
hold on;
The dashed line extends beyond the outer circle. It seems that the condition x3 < -Rb does not make a transition at the 674th element. Here is a plot of x3, with a horizontal line at -Rb.
figure
plot(x3)
yline(-Rb)
So, I'm not quite sure what is going wrong -- but the code I gave you for selecting a certain range of x3 is working as I would expect.