Question: I need help with my MATLAB code. If I have multiples files of data, for example vBody 1 , vBody 2 , vRef 1 ,

I need help with my MATLAB code. If I have multiples files of data, for example vBody1, vBody2, vRef1, vRef2, I don't want to copy and paste this code again and again. Can you help me create a function of this code.
% Initializing arrays
z = zeros(3,1);
B_mat = zeros(3);
no_stars = length(vBody);
% Creating for loop to find B and z of the Davenport's q-method
for i =1:no_stars
% Extracting the vectors
b = vBody(:, i);
r = vRef(:, i);
% Creating the B matrix
B_mat = B_mat + b * r';
% Creating the z vector
z = z + cross(b, r);
end
% Calculating the K matrix
K11= B_mat + B_mat' - trace(B_mat)* eye(3);
K_mat =[K11 z ; z' trace(B_mat)];
% Finding the eigenvalues and q vectors
[q_vec, eigs]= eig(K_mat ,'vector');
% Finding the biggest eigenvalue and its q vector
[lambda , i]= max(eigs);
q = q_vec(:, i);
q_hat = q / norm(q);
% Calculating the DCM using the function
A_davenport = q_DCM(q);
% Confirm that lambda_dave is close to lambda0
lambda_init = no_stars;
diff_lambda = lambda_init - lambda;
function skew_mat = Skew(v)
% This function creates the skew symmetric matrix of a vector
skew_mat =[0-v(3) v(2); v(3)0-v(1); -v(2) v(1)0];
end
function A = q_DCM(q)
% This function creates the rotation matrix by multiplying two matrices
% Calculating psi and (don't know what it is called)
psi =[(q(4)* eye(3)- Skew(q(1:3))); -q(1:3)'];
greek_letter =[(q(4)* eye(3)+ Skew(q(1:3))); -q(1:3)'];
% DCM
A = greek_letter' * psi;
end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!