Simple vector functions in matlab

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
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