Writing a function to calculate the cross product of two vectors???

11 vues (au cours des 30 derniers jours)
Big
Big le 28 Avr 2011
Commenté : rana le 22 Nov 2023
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...

Réponses (2)

Walter Roberson
Walter Roberson le 28 Avr 2011
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])

bym
bym le 28 Avr 2011
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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by