Writing a function to calculate the cross product of two vectors???
11 次查看(过去 30 天)
显示 更早的评论
Write a function to calculate the cross product of two vectors V1 and V2;
V1 x V2 = (Vy1Vz2 – Vy2Vz1)I + (Vz1Vx2 – Vz2Vx1)j + (Vx1Vy2 – Vx2Vy1)k
Where V1 = Vx1i + Vy1j + Vz1k and V2 = Vx2i + Vy2j + Vz2k. Note that this function will return a real array as its result. Use the function to calculate the cross product of the two vectors V1 = [-2, 4, 0.5] and V2 = [0.5, 3, 2].
Write your user-defined function, e.g., mycross(v1, v2), and can not use MATLAB built-in function cross.
The function statement is
function w = mycross(v1, v2) V1 is vector with values [vx1, vy1, vz1], V2 is vector [vx2, vy2, vz2], returned value w is a vector, where w(1) = v1(2)*v2(3) – v2(2)*v1(3). (originally vy1 vz2 - vy2 vz1)
Here's what I have so far, but I'm getting these errors, and not sure why...
The name of my m file is mycross.m
function W=mycross(v1,v2)
W(1)=( v1(2)* v2(3)- v2(2)* v1(3));
W(2)=-( v1(1)* v2(3)- v2(1)* v1(3));
W(3)=( v1(1)* v2(2)- v2(1)* v1(2));
??? Input argument "v1" is undefined.
Error in ==> mycross at 3
W(1)=( v1(2)* v2(3)- v2(2)* v1(3));
I'm lost...
0 个评论
回答(2 个)
Walter Roberson
2011-4-28
How are you invoking the function? It has to be called from the command line like
YourOutput = mycross([-2, 4, 0.5], [0.5, 3, 2])
0 个评论
bym
2011-4-28
You can also create a run configuration in the editor to specify a default input to your function. It can be found in the debug menu
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!