Write two functions called mysross and mydot that take two arrays as inputs and
ID: 3727840 • Letter: W
Question
Write two functions called mysross and mydot that take two arrays as inputs and compute the cross and dot products respectively. Incorporate conditionals into your functions that will identify errors that could arise given bad inputs. If the inputs are such that the operation cannot be performed, the function should output the number 1e-10 (there are ways for you to actually print out error messages like built-in Matlab functions would do, but these are not part of the course material). In order to do implement the conditions correctly, it is necessary to recall under which circumstances a dot and cross product are defined. (Hint: it has to do with the length of the vectors)Explanation / Answer
function [] = mydot ( a,b)
if (length(a)==3 && length(b)==3)
f= a(1)*b(1)+ a(2)*b(2) + a(3)*b(3);
display('dot product of vector is ');
f
else
display('Wrong input vector ');
end
function []= mycross(a,b)
if (length(a)==3 && length(b)==3)
f1= a(2)*b(3)- a(3)*b(2);
f2= a(1)*b(3)- a(3)*b(1);
f3= a(1)*b(2)- a(2)*b(1);
f=[f1 -f2 f3];
display('cross product of vector is ');
f
else
display('Wrong input vector ');
end
%%% main programm %%%
clc;
clear all;
close all;
a=[ 1 3 2];
b= [ 1 1 3];
mydot(a,b);
mycross(a,b);
output:
dot product of vector is
f =
10
cross product of vector is
f =
7 -1 -2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.