Question: I need help with my MATLAB code. I want to create a function of the following code, so if I have multiple S and z

I need help with my MATLAB code. I want to create a function of the following code, so if I have multiple S and z values.
S = B_mat + B_mat';
kappa = trace(adjoint(S));
% Characteristic Equation (Eqn.3.73)
f = @(x)(x^2- trace(B_mat)^2+ kappa)*(x^2- trace(B_mat)^2-...
dot(z, z))-(x - trace(B_mat))*(z'*S*z + det(S))-...
z'*(S'*S)*z;
% Derivative of the characteristic equation
df =@(x)2*x*(x^2- trace(B_mat)^2- dot(z , z))+...
2*x*(x^2- trace(B_mat)^2+ kappa)+(z'*S*z + det(S)) ;
% Calculate the eigenvalue Newton-Raphson iteration
lambdas = zeros(4,1);
lambdas(1)=10;
step = zeros(3,1);
quat = zeros (4,3);
DCM = zeros (3,3,3);
for i =1:3
Xn = f(lambda_init)/ df(lambda_init);
lambda_init = lambda_init - Xn;
% get the corresponding attitude estimates
rho = lambda_init + trace(B_mat);
rho_mat = rho * eye (3)- S;
q_unit =[adjoint(rho_mat)* z ; det(rho_mat)];
q_unit = q_unit / norm(q_unit);
% store the step size and attitude estimate
step(i)= Xn;
lambdas(i +1)= lambda_init;
quat(:, i)= q_unit;
DCM(:, :, i)= q_DCM(q_unit);
% Capture lambda_init and DCM after first and third iterations
if i ==1
lambda_after_first = lambda_init;
DCM_after_first = DCM(:, :, i);
elseif i ==3
lambda_after_third = lambda_init;
DCM_after_third = DCM(:, :, i);
end
end
% Compare lambda_init after the first and third iterations
lambda_difference = abs(lambda_after_third - lambda_after_first);
% Compare DCM after the first and third iterations
DCM_difference = abs(DCM_after_third - DCM_after_first);

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 Mechanical Engineering Questions!