Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MATLAB program to compute the sum of two 2-D vectors. You will prompt th

ID: 3802505 • Letter: W

Question

Write a MATLAB program to compute the sum of two 2-D vectors. You will prompt the user to input the x and y components of two vectors (four inputs). Then your program will compute the x and y components of the resultant and then the magnitude and the direction of the resultant. Last, your program should display the x and y components, plus the magnitude and direction for the polar coordinates. The flowchart for the program is shown below. The information displayed back to the user should be in the following format where your program will fill in the blanks with the computed information: The x and y components of the resultant vector are _____ and _____, respectively The magnitude and direction of the resultant vector are _____ and _____, respectively Once you have completed your m-file, name it using the following naming convention: "LASTNAME_FIRSTNAME_SprmgBreakAssignment.m". Where you will replace LASTNAME and FERSTNAME with your information.

Explanation / Answer

Let C = dot(A,B) returns the scalar dot product of A and B.
-If A and B are vectors, then they must have the same length.
-If A and B are matrices or multidimensional arrays, then they must have the same size.
In this case, the dot function treats A and B as collections of vectors. The function calculates the dot product
of corresponding vectors along the first array dimension whose size does not equal 1.

Create two simple, three-element vectors.
A = [4 -1 2];
B = [2 -2 -1];

Calculate the dot product of A and B.
C = dot(A,B)
C = 8
  
The result is 8 since
C = A(1)*B(1) + A(2)*B(2) + A(3)*B(3)
  
In order to do the same operations over complex vectors,
-Create two complex vectors.
- A = [1+i 1-i -1+i -1-i];
B = [3-4i 6-2i 1+2i 4+3i];

Calculate the dot product of A and B.
C = dot(A,B)
C = 1.0000 - 5.0000i
  
The result is a complex scalar since A and B are complex. In general, the dot product of two complex vectors is also complex.
An exception is when you take the dot product of a complex vector with itself.
  
  
you can tally the following code-
variable = [value1-1 value1-2 value1-3 ... ; value2-1 value2-2 ...]

where each value may be rational or complex numbers. Within the square brackets that are used to form vectors and matrices, you can use a semicolon to end a row. For example:

>> x = [1 2 3 4; 0 9 3 8]

x =

1 2 3 4

0 9 3 8

>>

(Note: You can also use the semicolon after an expression or statement to suppress printing or to separate statements.)

You can also initialize matrices with complex numbers:

>> x = [4+5i 2 4; 1 3+i 7]

x =

4.0000 + 5.0000i 2.0000 4.0000

1.0000 3.0000 + 1.0000i 7.0000

When entering complex numbers as matrix elements within brackets, it is important to not use blanks. In the example above, the expression 4 + 5i, with the + sign surrounded by blanks, would represent two separate numbers.

(In MATLAB 4, the * operator is not required in complex numbers, as it was in previous versions.)

Vectors and scalars are initialized the same way as matrices. It is important to note that MATLAB indexes matrices in the following manner:

(1,1) (1,2) (1,3) (1,4)

(2,1) (2,2) (2,3) (2,4)

This means that the first element always has index (1,1), not (0,0).

Indexing:

If x is already defined as a vector of the form [val1 val2 val3 val4...] then you can define a new variable as a subset of x by using the index of the specific value in vector x. For example, if x is defined as [2 4 1 7], then:

>> z = x(3)

z =

1

You can specify a value in matrix y the same way:

>> y = [ 1 2 3 4 5; 3 4 5 6 7]

y =

1 2 3 4 5

3 4 5 6 7

>> z = y(2,1)

z =

3

You can also specify a range of numbers in a defined vector or matrix using the colon operator. The colon causes MATLAB to step in sequence through the numbers specified. For example,

>> z = (1:5)

z =

1 2 3 4 5

So, using the y matrix defined above, you could specify a subset of y using the colon:

>> z = y(1:2,2:3)

z =

2 3

4 5

MATLAB has a variety of built-in functions to make it easier for you to construct matrices without having to enumerate all the elements. (The following examples show both vectors and matrices.)

The ones function creates a matrix whose elements are all ones. Typing ones(m,n) creates an m row by n column matrix of ones. To create a ones matrix that is the same size as an existing matric, you can use ones(size(X)). This does not affect the input argument. For example (this definition of x applies to subsequent examples in this section):

>> x = [1 2 3 4; 0 9 3 8]

x =

1 2 3 4

0 9 3 8

>> y = ones(size(x))

y =

1 1 1 1

1 1 1 1

The zeros function is similar to the ones function. Typing zeros(m,n) creates an m-by-n matrix of zeros, and zeros(size(x)) will create a two-by-four matrix of zeros, if x is defined the same way as above.

The max and min functions return the largest and smallest values in a vector. For example (this definition of z applies to the following series of examples):

>> z = [1 2 -9 3 -3 -5]

z =

1 2 -9 3 -3 -5

>> max(z)

ans =

3

If called with a matrix as its argument, max returns a row vector in which each element is the maximum value of each column of the input matrix. The max function can also return a second value: the index of the maximum value in the vector. To get this, assign the result of the call to max to a two element vector instead of just a single variable.

For example:

>> [a b] = max(z)

a =

3

b =

4

where a is the maximum value of the vector and b is the index of that value. The MATLAB function min is exactly parallel to max:

>> min(z)

ans =

-9

sum and prod are two more useful functions for matrices. If z is a vector, sum(z) is the sum of all the elements of the vector z:

>> sum(z)

ans =

-11

For matrices, sum sums the columns. For example:

>> w = magic(3);

>> w

w =

8 1 6

3 5 7

4 9 2

>> sum(w)

ans =

15 15 15

>> sum(sum(w))

ans =

45

Similarly, prod(z) is the product of all the elements of z.

>> prod(z)

ans =

-810

Often, it is useful to define a vector as a subunit of a previously defined vector. To do this, you can use the colon operator. For example, using the z defined above,

>> z

z =

1 2 -9 3 -3 -5

>> y = z(2:5)

y =

2 -9 3 -3

where (2:5) represents the sequence of index values to be taken from the larger vector.

The size function returns a two-element vector giving the dimensions of the matrix with which it was called. For example:

>> x = [1 2 3 4; 0 9 3 8]

x =

1 2 3 4

0 9 3 8

>> y = size(x)

y =

2 4

You can also define the result to be two separate values (as shown in the max example):

>> [m n] = size(x)

m =

2

n =

4

The length operator returns the length of a vector. If z is defined as in the above examples,

>> length(z)

ans =

6

For matrices, length is the length or the width, whichever is greater, i.e., length(z) is equivalent to max(size(z)).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote