MATLAB Simple vector functions in matlab

AI Thread Summary
The discussion revolves around a MATLAB assignment involving the creation of two vectors, x and y, and calculating their sum using three different methods: creating an additional variable z, using the dot function, and multiplying x with the transpose of y. The user initially encounters a discrepancy in results, obtaining 148.2851 from the sum of z but only 129.5077 from the dot product and matrix multiplication. The confusion arises from misunderstanding the operations; the dot function computes the scalar product, which yields a different result than simply adding the vectors. The user later realizes that the correct approach for z should be z = x * y, aligning the operations with the intended calculations.
ineedhelpnow
Messages
649
Reaction score
0
I have an assignment for MATLAB where I am required to create two vectors x and y and then to find the sum in three ways.
first, create an extra variable z and find the sum
second, use the dot function
third, multiply x with the transpose of y

here's my code
Code:
x=linspace(0,1,5);
y=logspace(0,2,5);
z=x+y;
disp(x)
disp(y)
disp(sum(z))
disp(x)
disp(y)
disp(dot(x,y))
disp(x)
disp(y)
disp(x*y')

here's what MATLAB gives me
Code:
         0    0.2500    0.5000    0.7500    1.0000

    1.0000    3.1623   10.0000   31.6228  100.0000

  148.2851

         0    0.2500    0.5000    0.7500    1.0000

    1.0000    3.1623   10.0000   31.6228  100.0000

  129.5077

         0    0.2500    0.5000    0.7500    1.0000

    1.0000    3.1623   10.0000   31.6228  100.0000

  129.5077

using the sum of z gave the correct sum but with the other two functions for some reason its giving me 129.5077 instead of 148.2851 and i can't seem to find what i did wrong. I'd really appreciate it if someone could help me spot it out
 
Physics news on Phys.org
Hi,

There is nothing wrong in your code. The results are different because they are different in general, the scalar product of two vectors is the sum of the product of its components.
 
Thank you. I just realized it should have been z=x*y
 

Similar threads

Replies
10
Views
3K
Replies
4
Views
1K
Replies
1
Views
2K
Replies
16
Views
6K
Replies
5
Views
5K
Back
Top