In: Advanced Math
A Cartesian vector can be thought of as representing magnitudes along the x-, y-, and z-axes multiplied by a unit vector (i, j, k). For such cases, the dot product of two of these fectors {a} and {b} corresponds to the product of their magnitudes and the cosine of the angle between their tails as in {a}⋅ {b} = abcos(theta)
The cross product yields another vector, {c} = {a} × {b} , which is perpendicular to the plane defined by {a} and {b} such that its direction is specified by the right-hand rule. Develop and M-file function that is passed two such vectors and returns Theta, {c} and the magnitude of {c}, and generates a three-dimensional plot of the three vectors {a}, {b}, and {c} with their origins at zero. Use dashed lines for {a} and {b} and a solid line for {c}. Test your function using the following cases:
A. a = [ 6 4 2 ]; b = [ 2 6 4 ];
B. a = [ 3 2 -6 ]; b = [ 4 -3 1];
C. a = [ 2 -2 1 ]; b = [ 4 2 -4 ];
D. a = [ -1 0 0 ]; b = [ 0 -1 0 ];
I know how to find theta, {c}, and the magnitude of {c}, I just don't know how to plot a 3-dimensional graph so if someone could help me with that part of the code for MATLAB
MATLAB Script:
close all
clear
clc
fprintf('Part
A\n-------------------------------------------\n')
a = [6 4 2]; b = [2 6 4];
[Theta, Magnitude] = cross_prod(a, b);
fprintf('Theta (in radians): %.4f\n', Theta)
fprintf('Magnitude: %.4f\n', Magnitude)
fprintf('\nPart
B\n-------------------------------------------\n')
a = [3 2 -6]; b = [4 -3 1];
[Theta, Magnitude] = cross_prod(a, b);
fprintf('Theta (in radians): %.4f\n', Theta)
fprintf('Magnitude: %.4f\n', Magnitude)
fprintf('\nPart
C\n-------------------------------------------\n')
a = [2 -2 1]; b = [4 2 -4];
[Theta, Magnitude] = cross_prod(a, b);
fprintf('Theta (in radians): %.4f\n', Theta)
fprintf('Magnitude: %.4f\n', Magnitude)
fprintf('\nPart
D\n-------------------------------------------\n')
a = [-1 0 0]; b = [0 -1 0];
[Theta, Magnitude] = cross_prod(a, b);
fprintf('Theta (in radians): %.4f\n', Theta)
fprintf('Magnitude: %.4f\n', Magnitude)
function [Theta, Magnitude] = cross_prod(a, b)
c = cross(a, b);
Theta = acos(dot(a, b)/sqrt(dot(a, a)*dot(b, b)));
Magnitude = sqrt(dot(c, c));
% Plotting
origin = [0 0 0];
figure
x = [origin(1) a(1)]; y = [origin(2) a(2)]; z = [origin(3)
a(3)];
plot3(x, y, z, '--'), hold on % Plot vector a
x = [origin(1) b(1)]; y = [origin(2) b(2)]; z = [origin(3)
b(3)];
plot3(x, y, z, '--') % Plot vector b
x = [origin(1) c(1)]; y = [origin(2) c(2)]; z = [origin(3)
c(3)];
plot3(x, y, z), hold off % Plot vector c
grid on
xlabel('x'), ylabel('y'), zlabel('z')
title('Cross Product - 3D Plot')
legend('a', 'b', 'c')
end
Output:
Part A
-------------------------------------------
Theta (in radians): 0.6669
Magnitude: 34.6410
Part B
-------------------------------------------
Theta (in radians): 1.5708
Magnitude: 35.6931
Part C
-------------------------------------------
Theta (in radians): 1.5708
Magnitude: 18.0000
Part D
-------------------------------------------
Theta (in radians): 1.5708
Magnitude: 1.0000
Plots: