Computing question 1st year Engineering-MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter Bostonpancake0
  • Start date Start date
  • Tags Tags
    Computing Matlab Year
Click For Summary

Discussion Overview

The discussion revolves around a programming question related to calculating the hypotenuse of right triangles using MATLAB, specifically focusing on generating all possible combinations of two vectors representing the lengths of the opposite and adjacent sides, rather than performing element-wise operations.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks to calculate combinations of two vectors in relation to the Pythagorean theorem, expressing confusion over the element-wise operation of their current code.
  • Another participant questions the meaning of "all of the different possible combinations" and suggests clarifying whether the vectors should only contain integer lengths.
  • A suggestion is made to use a nested loop to create a matrix of hypotenuse lengths from the combinations of opposite and adjacent side lengths.
  • Further clarification is provided by a participant who explains that the current code only computes hypotenuse values for corresponding elements of the vectors, rather than all combinations.
  • Hints are provided regarding the use of inner and outer products in MATLAB, with a suggestion that these operations might relate to the problem at hand.
  • Another participant elaborates on the inner and outer products, providing examples and noting that while these are common operations, they may not be what the original poster intends to use for their calculations.

Areas of Agreement / Disagreement

Participants express varying degrees of understanding regarding the original poster's intent, with some suggesting methods to achieve the desired outcome while others question the clarity of the problem statement. There is no consensus on the best approach to solve the issue presented.

Contextual Notes

There are limitations in the clarity of the original question, particularly regarding the definition of "all combinations" and the specific requirements for the vectors involved. Additionally, the discussion does not resolve whether the inner or outer product is the appropriate method for the calculations.

Bostonpancake0
Messages
42
Reaction score
0
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 occurring and consider all combinations of adjacent and opposite side lengths?
 
Physics news on Phys.org
Bostonpancake0 said:
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 occurring and consider all combinations of adjacent and opposite side lengths?

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?
 
Do you mean you want a 31x31 matrix of the lengths? Use a nested loop
 
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 let's 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.
 
Hint: "inner product"
 
Well, I tried to be cute with the short reply, and I blew it!

New hint: "outer product"
 
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.
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K