Finding the Dot Product of Two Vectors

The dot product (or scalar product) of two vectors is used, among other things, as a way of finding the angle theta between two vectors. Recall that, given vectors a and b in space, the dot product is defined as
a . b = | a | | b | cos( theta )
We will use this formula later to find the angle theta. For now, we want to focus on the computation formula for the dot product: given the components of the vectors a = < a1, a2, a3 > and b = < b1, b2, b3 >, the dot product is given by
a . b = a1*b1 + a2*b2 + a3*b3
Remember that in both cases, the result is NOT a vector, but a scalar (or number-hence the alternate name "scalar product").
Use this formula to write a function file which computes the dot product of two 3-dimensional vectors a and b. The function name is DotProd, which has two inputs: the vectors a and b (which should contain 3 elements each). The output is the single value y, which is a . b
NOTE: Though not assessed, it may be more efficient if you use element by element multiplication along with MATLAB's built-in command sum (which adds the components of a vector).
Your Function
function y = DotProd(a,b) % NOTE: DO NOT CHANGE THE CODE ON THIS LINE!
y = ;
end
so please can i have the Code to call my function ?

댓글 수: 1

Guillaume
Guillaume 2019년 5월 30일
so please can i have the Code to call my function ?
No, we don't you your homework for you (particularly ones as easy as this).

댓글을 달려면 로그인하십시오.

답변 (2개)

Piyush kant
Piyush kant 2019년 5월 30일

0 개 추천

I hope this works.
function y = DotProd(a,b) % NOTE: DO NOT CHANGE THE CODE ON THIS LINE!
y = a.*b ;
end

댓글 수: 5

Guillaume
Guillaume 2019년 5월 30일
No, it doesn't (in that it doesn't calculate a dot product at all)
In any case, don't do their homework when they have shown no effort. You're not helping them, just showing them that it's easy to cheat.
Stephen23
Stephen23 2019년 5월 30일
편집: Stephen23 2019년 5월 30일
"I hope this works"
y = a.*b ;
Rather than hoping you could just read the times documentation and know that it doesn't calculate the dot product.
Guillaume
Guillaume 2019년 5월 30일
Or read the definition of the dot product which is given in the question (hence why this homework is so easy. The exact equation is given in the question)
Walter Roberson
Walter Roberson 2019년 5월 30일
maryam alhajji comments to Guillaume:
please respect , as the it is practice not homework , and if I know the answer I will not ask someone to help me , I am just asking for the knowledge .
Walter Roberson
Walter Roberson 2019년 5월 30일
You could read the documentation: the code for the simple case is given there.

댓글을 달려면 로그인하십시오.

maryam alhajji
maryam alhajji 2019년 5월 30일
편집: Walter Roberson 2019년 5월 30일

0 개 추천

syms a1 a2 a3 b1 b2 b3
a=[ a1 , a2 , a3];
b= [ b1 , b2 , b3];
d= dot(a,b)
c=cross (a,b)
c =
[ a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1]
# the answer shoe me by (-) and i donot know how to keep it as
function y = DotProd(a,b) % NOTE: DO NOT CHANGE THE CODE ON THIS LINE!
y = ;
end
# if any one can help me I am thankful .

댓글 수: 1

Walter Roberson
Walter Roberson 2019년 5월 30일
Read the documentation for dot(), especially starting from "More About"

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2019년 5월 30일

댓글:

2019년 5월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by