Simple vector functions in matlab

In summary, the assignment for MATLAB involved creating two vectors x and y and finding their sum in three ways: first, by creating an extra variable z and finding the sum; second, by using the dot function; and third, by multiplying x with the transpose of y. The correct results were obtained by using the sum of z, but the other two methods gave a different result due to the difference in the scalar product of two vectors. After correcting the code, the correct result was obtained.
  • #1
ineedhelpnow
651
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
  • #2
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.
 
  • #3
Thank you. I just realized it should have been z=x*y
 

1. What is a simple vector function in matlab?

A simple vector function in matlab is a mathematical operation that acts on vectors and produces a new vector as a result. This can include operations such as addition, subtraction, multiplication, and division.

2. How do I create a simple vector function in matlab?

To create a simple vector function in matlab, you can use the built-in functions provided by the software, such as "zeros" or "ones". You can also define your own custom function using the "function" keyword and specifying the input and output variables.

3. Can I apply a simple vector function to multiple vectors at once?

Yes, you can apply a simple vector function to multiple vectors at once by using element-wise operations. This means that the function will be applied to each element of the vectors separately, producing a new vector with the same dimensions as the original ones.

4. How can I plot the results of a simple vector function in matlab?

You can plot the results of a simple vector function in matlab by creating a plot using the "plot" function. You will need to specify the x and y values for the plot, which can be obtained by applying the function to a range of input values. You can also add labels, titles, and other formatting options to the plot.

5. Are there any limitations to using simple vector functions in matlab?

There are some limitations to using simple vector functions in matlab, such as the size and type of the input vectors. Matlab has a maximum array size limit, so if your vectors are too large, the function may not work. Additionally, the vectors must be of the same data type for the function to be applied successfully.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
575
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
940
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
16
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top