Writing a function to calculate the cross product of two vectors???
조회 수: 12 (최근 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!