Error using cross and not calculating correctly
8 views (last 30 days)
Show older comments
I am trying to write the code to calculate the area of a trianlge given three vectors. I keep getting an answer that is wrong. Here's the code that I wrote:
input a=['Enter vector a:'];
input b=['Enter vector b:'];
input c=['Enter vector c:'];
A=norm(cross('c-a','b-a'));
Area=A/2
This is what I get when I plug in the following vectors:
>> TAREA
a=['Enter vector a:'][1 2 2]
b=['Enter vector b:'][3 1 4]
c=['Enter vector c:'][5 2 1]
Area =
53.4649
The area should be 5.4083. Why is the code not working properly?
0 Comments
Answers (2)
Walter Roberson
on 14 Mar 2023
CA = 'c-a'
BA = 'b-a'
dCA = double(CA)
dBA = double(BA)
cross(CA,BA)
cross(dCA,dBA)
From this you can see that cross('c-a','b-a') is the same as cross([99 45 97],[98 45 97]) . You are taking the cross-product of the characters ['c' '-' 'a'] and ['b' '-' 'a'] rather than the cross-product of the difference in values of variables c and a and b.
Dyuman Joshi
on 14 Mar 2023
Edited: Dyuman Joshi
on 14 Mar 2023
You are using character scalars instead of using variables. The result you get are corresponding to the ascii values of the characters
a=[1 2 2];
b=[3 1 4];
c=[5 2 1];
%Using variables
Area1=norm(cross(c-a,b-a))/2
Area2=norm(cross('c-a','b-a'))/2
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!