MATLAB: Help writing a function

function

Can someone please help on how to write the following function please.
I have 3 3×3 Rotation Matrices.
2 of which are Identity Matrices.
R1 = eye(3,3)
R2 = eye(3,3)
% R3 Rotation Value Kappa Pre-Cal
R3(3,3) = zeros
R3(1,1) = cos(xHat_BM(3,1))
R3(1,2) = sin(xHat_BM(3,1))
R3(2,1) = -sin(xHat_BM(3,1))
R3(2,2) = cos(xHat_BM(3,1))
R3(3,3) = 1
xHat_BM will be updated for each iteration (separate section of code)
How do I create a function so that said function grabs the xHat_BM value from the script, uses it for the rotation angle calculations (R3 elements in the function), then outputs the updated R3 matrix (M_BM)back into the script for the next iteration?
Thanks in advance.

Best Answer

function R3 = update_R3(xHat_BM)
% R3 Rotation Value Kappa Pre-Cal
R3 = zeros(3,3);
R3(1,1) = cos(xHat_BM(3,1));
R3(1,2) = sin(xHat_BM(3,1));
R3(2,1) = -sin(xHat_BM(3,1));
R3(2,2) = cos(xHat_BM(3,1));
R3(3,3) = 1;
To use: have your script call
M_BM = update_R3(xHat_BM);