function that calculates the angle between two input vectors. inputs are two vectors, and output the angle between the two vectors in degrees. function displays the type of angle you have calculated in matlab

main.m
function angle = calculate_angle(v1, v2)
    dot_product = dot(v1, v2);
    magnitude_v1 = norm(v1);
    magnitude_v2 = norm(v2);
    
    angle = rad2deg(acos(dot_product / (magnitude_v1 * magnitude_v2)));
    
    if angle == 0
        disp('The vectors are parallel');
    elseif angle == 180
        disp('The vectors are antiparallel');
    elseif angle > 0 && angle < 90
        disp('The vectors form acute angle');
    elseif angle == 90
        disp('The vectors are orthogonal');
    elseif angle > 90 && angle < 180
        disp('The vectors form obtuse angle');
    end
end

% Example Usage:
v1 = [1, 2, 3];
v2 = [4, 5, 6];
angle = calculate_angle(v1, v2);
disp(['Angle between v1 and v2: ', num2str(angle)]);
722 chars
26 lines

related categories

gistlibby LogSnag