MATLAB Answers

How can I change my code so that more inputs can be added?

1 view (last 30 days)
Alexandre d'Orgeval
Alexandre d'Orgeval on 12 Nov 2020
Edited: Sourabh Kondapaka on 18 Nov 2020
Hi,
I'm working on a project where I have to solve a state space system using the ODE 45 command. This project basically uses relative motion equation to solve the motion of a satellite between two points. Each different motion is called a manoeuvre. The ODE command is in another function called Orbit which is called in the main for different inputs (see code). The y output is a n by 6 matrix.
I want to put all the inputs (r_f values) in one matrix, so that when i need to add more points, i just add them at the end of the matrix.
Also, this is my first time here so if something is wrong or else please tell me :)
r_0 = [-20;0;0];% Initial position in the debris frame
t = 1000; %Time duration of each manoeuver
r_f1 = [-3;0;0]; %desired position after the 2nd manoeuver
r_f2 = [-1;0;0]; %desired position after the 3rd manoeuver
r_f3 = [-0.5; 0; 0]; %desired position after the 4th manoeuver
r_f4 = [0;0;0]; %desired position after the 5th manoeuver
%This function outputs a n by 6 vector y with the relative positions and velocities
function [y] = Orbit(r_0, r_f)
global n t F
%Calculate the velocity impulse necessary from the Clohessy Wiltshire solutions
[v_0p, ~, ~] = speedcalc(t, n, r_f, r_0);
%speedcalc just does basic calculation
%% ODE solver
%Time to consider
tspan = [0 t];
%Using initial conditions
iniCon = [r_0, v_0p];
[~, y] = ode45(@(t, x) sys(t, x, n, F), tspan, iniCon);
end
%This function is used for the ODE45
function dx = sys(t, x, n, F)
mc = 140;%spacecraft mass
%Xdot = AX + BF
B = [0 0 0;
0 0 0;
0 0 0;
1/mc 0 0;
0 1/mc 0;
0 0 1/mc];
B1 = B*F;
%A is the Clohessy Wiltshire matrix
A = [0 0 0 1 0 0;
0 0 0 0 1 0;
0 0 0 0 0 1;
3*n^2 0 0 0 2*n 0;
0 0 0 -2*n 0 0;
0 0 n^2 0 0 0];
dx = A*x + B1;
end
%This creates the y matrices which are created from the orbit function. It takes the last position from the
%previous matrix and the desired final position (which can be different from the real final position).
%This is what I want to put in a for loop where it creates the yn variables at each loop.
[y] = Orbit(r_0, r_f);
[y1] = Orbit(y(length(y), 1:3)', r_f1);
[y2] = Orbit(y1(length(y1), 1:3)', r_f2);
[y3] = Orbit(y2(length(y2), 1:3)', r_f3);
[y4] = Orbit(y3(length(y3), 1:3)', r_f4);

  0 Comments

Sign in to comment.

Answers (1)

Sourabh Kondapaka
Sourabh Kondapaka on 18 Nov 2020
Edited: Sourabh Kondapaka on 18 Nov 2020
Initially you can define an empty matrix r_f and then you can use horzcat() function to add r_f1 at the end of the matrix. When we use horzcat() the r_f1 column vector will get appended to r_f as a new column.
I'm making the following assumption that:
When you say, 'append r_f2 to end of a matrix', you mean to add r_f2 as a new column after the last column
of a matrix .
There are 2 ways to go about doing this:
You can individually apend r_f{i} to r_f or you can do it all at once as shown by the 2 approaches
%Approach 1
r_f = [];
r_f = horzcat(r_f, r_f0);
r_f = horzcat(r_f, r_f1);
r_f = horzcat(r_f, r_f2);
r_f = horzcat(r_f, r_f3);
r_f = horzcat(r_f, r_f4);
% Approach 2. The below line does the same exact thing as above set of lines
r_f = [r_f0 r_f1 r_f2 r_f3 r_f4];
I would recommend you to check out the free MATLAB OnRamp Course to get upto speed with using MATLAB Programming Language.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by