Simple vector functions in matlab

Click For Summary
SUMMARY

The discussion revolves around creating two vectors in MATLAB, named x and y, and calculating their sum using three different methods: creating an extra variable z, using the dot function, and multiplying x with the transpose of y. The user initially encounters discrepancies in the results, specifically receiving 129.5077 instead of the expected 148.2851 when using the dot function and matrix multiplication. The resolution highlights that the correct approach for the sum should be z = x * y, clarifying the difference between the operations performed.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with vector operations in MATLAB
  • Knowledge of the dot product and matrix multiplication
  • Basic understanding of MATLAB's linspace and logspace functions
NEXT STEPS
  • Explore MATLAB's documentation on vector operations
  • Learn about the differences between dot product and matrix multiplication in MATLAB
  • Investigate the use of MATLAB's sum function for different data types
  • Practice creating and manipulating vectors using linspace and logspace functions
USEFUL FOR

This discussion is beneficial for MATLAB users, students working on assignments involving vector calculations, and anyone looking to deepen their understanding of vector operations and functions in MATLAB.

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 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
6K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 6 ·
Replies
6
Views
4K