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
SUMMARY

The discussion focuses on calculating the hypotenuse of all possible combinations of two vectors using Pythagoras' theorem in MATLAB. The original code provided by the user only performs element-wise operations, leading to a limited output. To achieve the desired result, participants suggest using nested loops to iterate through all combinations of the 'opposite' and 'adjacent' vectors, which range from 0 to 30. The concepts of inner and outer products are also introduced as relevant mathematical operations that can be utilized in MATLAB for this purpose.

PREREQUISITES
  • Understanding of MATLAB syntax and operations
  • Familiarity with Pythagorean theorem
  • Knowledge of inner and outer products in linear algebra
  • Basic programming concepts, particularly loops
NEXT STEPS
  • Implement nested loops in MATLAB to calculate all combinations of two vectors
  • Explore MATLAB functions for matrix operations, specifically focusing on inner and outer products
  • Research efficient ways to optimize nested loops in MATLAB for performance
  • Learn about vectorization techniques in MATLAB to replace loops for better efficiency
USEFUL FOR

This discussion is beneficial for engineering students, MATLAB users, and anyone interested in numerical analysis and programming techniques for vector operations.

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
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · 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