In: Advanced Math
1. Magnitude & Direction of Vector
Write a function m-file to find the magnitude and the direction of cosine of vector F which expresses 3 dimension - x, y, and z.
Here are requirements for the function m-file.
- Parameter-list (input(s)): · 3 dimensional vector · row vector or column vector
- undetermined - Return Value (outputs):
· Magnitude of a given vector, |F| = √ F2 x + F2 y + F2 z
· Angles(degree) from direction of cosine cos(α) = Fx |F| cos(β) = Fy |F| cos(γ) = Fz |F|
· Order of outputs : [Magnitude, α, β, γ]
- Function name : ‘Mag_Vec_yourEmailAccount’ Using a script m-file which will call the Mag_Vec_yourEmailAccount function, save outputs with following given inputs. - F1 = 3ˆi + 2ˆj − 6ˆk. Save it on EX1_1.dat.
- F2 = 4ˆi − 2ˆk. Save it on EX1_2.dat.
- F3 = 4ˆi + 3ˆj. Save it on EX1_3.dat.
- F4 = 5ˆj − 12ˆk. Save it on EX1_4.dat.
- F5 =ˆi +ˆj + ˆk. Save it on EX1_5.dat.
using mathlab functions, thank you!
function [Magnitude, alp, bit, gamma] = Mag_Vec(F)
Magnitude=sqrt(F(1)^2+F(2)^2+F(3)^2);
alp = acos(F(1)/Magnitude); % acos = invers cos(th)
bit = acos(F(2)/Magnitude); % alp, bit ,gamma calculating as
radian
gamma = acos(F(3)/Magnitude);
alp = 180*alp/pi; % alp, bit ,gamma calculating as degree
bit = 180*bit/pi;
gamma = 180*gamma/pi;
end
clc
clear
fid = fopen('EX1_1.dat','w');
F1=[3, 2, -6]; % F1 = 3i+2j-6k;
[Magnitude, alp, bit, gamma] = Mag_Vec(F1);
Sol = [Magnitude, alp, bit, gamma];
fprintf(fid,'%f \n',Sol);
fclose(fid);
fid = fopen('EX1_2.dat','w');
F2=[4, 0, -2]; % F1 = 4i-2k;
[Magnitude, alp, bit, gamma] = Mag_Vec(F2);
Sol = [Magnitude, alp, bit, gamma];
fprintf(fid,'%f \n',Sol);
fclose(fid);
fid = fopen('EX1_3.dat','w');
F3=[4, 3, 0]; % F1 = 4i+3j;
[Magnitude, alp, bit, gamma] = Mag_Vec(F3);
Sol = [Magnitude, alp, bit, gamma];
fprintf(fid,'%f \n',Sol);
fclose(fid);
fid = fopen('EX1_4.dat','w');
F4=[0, 5, -12]; % F1 = 4j+12k;
[Magnitude, alp, bit, gamma] = Mag_Vec(F4);
Sol = [Magnitude, alp, bit, gamma];
fprintf(fid,'%f \n',Sol);
fclose(fid);
fid = fopen('EX1_5.dat','w');
F5=[1, 1, 1]; % F1 = i+j+k;
[Magnitude, alp, bit, gamma] = Mag_Vec(F5);
Sol = [Magnitude, alp, bit, gamma];
fprintf(fid,'%f \n',Sol);
fclose(fid);
%to see the EX1_1.dat file enter in command window- type EX1_1.dat