Computing question 1st year Engineering-MATLAB

In summary, the question is asking for a code that will calculate all of the different possible combinations of adjacent and opposite side lengths when using Pythagoras theorem. The code only calculates element by element, so this would require a loop that runs through the values of opposite and adjacent and calculates the hypotenuse.
  • #1
Bostonpancake0
42
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
  • #2
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?
 
  • #3
Do you mean you want a 31x31 matrix of the lengths? Use a nested loop
 
  • #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 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.
 
  • #5
Hint: "inner product"
 
  • #6
Well, I tried to be cute with the short reply, and I blew it!

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

1. What is MATLAB?

MATLAB is a high-level programming language and interactive computing environment used for data analysis, visualization, and numerical computation. It is commonly used in engineering and scientific fields for tasks such as data analysis, signal processing, and image processing.

2. How is MATLAB different from other programming languages?

MATLAB is specifically designed for numerical computation and has built-in functions and tools for data analysis and visualization. It also has a user-friendly interface that allows for easy manipulation of data and algorithms. Other programming languages may require more coding and knowledge of syntax for similar tasks.

3. Can I use MATLAB for machine learning and artificial intelligence?

Yes, MATLAB has a dedicated toolbox called the "Deep Learning Toolbox" that allows for the implementation of deep learning algorithms and artificial neural networks. It also has other useful toolboxes such as the "Statistics and Machine Learning Toolbox" that can be used for data analysis and machine learning tasks.

4. Is MATLAB free to use?

No, MATLAB is a commercial product and requires a license to use. However, there is a free alternative called "GNU Octave" that has similar capabilities and syntax to MATLAB. It is open-source and can be used for academic and personal purposes.

5. What are some resources for learning MATLAB?

There are many online tutorials, courses, and forums dedicated to learning MATLAB. The official MathWorks website offers documentation, tutorials, and examples for beginners. Other popular resources include MATLAB Central, YouTube channels, and online courses on platforms such as Coursera and Udemy.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
951
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top