Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Computing question 1st year Engineering-MATLAB

Tags:
  1. Apr 22, 2014 #1
    Hi,

    the question is as follows.

    I wish to calculate all of the different possible combinations of two vectors in regards to Pythagoras theorem. Not just an element by element operation

    My code is as follows;

    opposite=(0:30)
    adjacent=(0:30)
    hypotenuse=sqrt(x1.^2+y1.^2)

    When I use this code it only gives me distances using element by element operation. How might one prevent this from occuring and consider all combinations of adjacent and opposite side lengths?
     
  2. jcsd
  3. Apr 22, 2014 #2

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    It's not clear what you mean by 'all of the different possible combinations of two vectors in regards to Pythagoras theorem.' Are you talking about two vectors which have only integer lengths?
     
  4. Apr 22, 2014 #3
    Do you mean you want a 31x31 matrix of the lengths? Use a nested loop
     
  5. Apr 22, 2014 #4
    sorry I was not 100% clear in my description.

    When I run the given code above ( x1 and y1 are adjacent and opposite, forgot to change them), the hypotenuse matrix that returns, only considers element by element computations with the two given matrices adjacent and opposite (i.e. only 30 elements in length).

    Thus my code will only produce for example: adjacent=1, opposite=1 thus hypotenuse=sqrt(1^2+1^2)
    then adjacent=2 with opposite=2 thus hypotenuse=sqrt(2^2+2^2)



    But lets say I wanted to compute adjacent =1 with opposite=2?
    then maybe adjacent=1 with opposite=3?
    then maybe adjacent=5 with opposite=2? and so and so forth.

    This isn't of any real importance I was considering the question today whilst doing my programming homework.
     
  6. Apr 24, 2014 #5
    Hint: "inner product"
     
  7. Apr 24, 2014 #6
    Well, I tried to be cute with the short reply, and I blew it!

    New hint: "outer product"
     
  8. May 24, 2014 #7

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Expanding on @mikeph response: Make a loop that runs through the values of opposite and within that loop, make a loop that runs through the values of adjacent. For each combination, calculate the hypotenuse. In your example, where both opposite and adjacent are (1..30), you only have to do half of the inner loop since the answers for swapped opposite/adjacent values are identical and have already been calculated. I don't know if there is a MATLAB shortcut to do that. Maybe someone can assist.
     
  9. May 24, 2014 #8

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You can consider both the inner and outer products as matrix multiplications. You just need to get the matrix dimensions right and the matrices in the right order.
    Inner product: ##\begin{bmatrix}1 & 2\end{bmatrix} \begin{bmatrix}3 \\ 4\end{bmatrix} = \begin{bmatrix} 11 \end{bmatrix}##
    Outer product: ##\begin{bmatrix}1 \\ 2\end{bmatrix} \begin{bmatrix}3 & 4\end{bmatrix} = \begin{bmatrix}
    3 & 4 \\ 6 & 8 \end{bmatrix}##

    They are both common operations in numerical analysis and both should be available in Matlab. The inner product is sometimes called a "scalar" or "dot" product, and the outer product is sometimes called a "rank 1 update" to a matrix.

    Finding them in Matlab is left as a learning exercise for the OP :smile:

    But I don't think that what the OP wants do. Probably calculating the terms in a loop the simplest way.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Computing question 1st year Engineering-MATLAB
Loading...