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

Write a MATLAB script that plots the even and odd components of the functions in

ID: 3884778 • Letter: W

Question

Write a MATLAB script that plots the even and odd components of the functions in problem parts(a)(c)(d)(f)(h)(i). This script will result in three figures, each containing two columns, where each column will display three figure axes: top axis – original function; middle axis – the function’s even component; bottom axis – the function’s odd component. Use the MATLABsubplot() function to create these axis layouts,and add descriptive titles and labels to your plots. For example, the left column of the first figure should contain plots of g(t),ge(t), and go(t), from top to bottom, for problem (a). Use the title of the top axis to state the problem number and the mathematical expression for g(t). Then, use the titles of the remaining axes to identify the even and odd signal components. The right column of the first figure should contain the corresponding plots for problem(c). The second figure should address (d)(f), and the third figure should address (h)(i).

The functions to be plotted are in the image.

10 sin(2 d) x(+) = 1+t Xe (t 1 Yo(t): t 4r t j) g(t)-(3+7t)cos(3201 t)

Explanation / Answer

The most straightforward way to initialize a matrix is to type a command of the form:

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)).

Basic Arithmetic

MATLAB uses a straight-forward notation for basic scalar arithmetic. The following table summarizes simple MATLAB notation:

+ addition

- subtraction

* multiplication

/ division

^ exponentiation

All of these work for two scalars, including complex scalars. You can also add, subtract, multiply or divide all the elements of a vector or matrix by a scalar. For example, if x is a matrix or vector, then x+1 adds one to each element x, and x/2 divides each element of x by 2. x^2 does not square each element of x, but x.^2 does. Matrix and vector exponentiation are discussed later.

Another useful operator is the colon (:), which you use to specify a range of numbers (as a vector). For example:

>> x = 1:4

x =

1 2 3 4

You can optionally give the range indicator a step size as the middle element in a series of colons. For example:

>> x = 8:-1:5

x =

8 7 6 5

>> x = 0:0.25:1.25

x =

0 0.25 0.5 0.75 1.0 1.25

The special operator ' (prime or apostrophe) denotes the transposition of a matrix. For example:

>> a = [1 2 3]

a =

1 2 3

>> a'

ans =

1

2

3

Element-Wise Operations

You often may want to perform an operation on each element of a vector while doing a computation. For example, you may want to add two vectors by adding all of the corresponding elements. The addition (+) and subtraction (-) operators are defined to work on matrices as well as scalars. For example, if x = [1 2 3] and y = [5 6 2], then

>> w = x+y

w =

6 8 5

Multiplying two matrices element by element is a little different. The * symbol is defined as matrix multiplication when used on two matrices. Use .* to specify element-wise multiplication. So, using the x and y from above,

>> w = x .* y

w =

5 12 6

You can perform exponentiation on a vector similarly. Typing x .^ 2 squares each element of x.

>> w = x .^ 2

w =

1 4 9

Finally, you cannot use / to divide two matrices element-wise, since / and are reserved for left and right matrix ``division.'' Instead, you must use the ./ function. For example:

>> w = y ./ x

w =

5.0000 3.0000 0.6667

All of these operations work for complex numbers as well (the * operator is no longer required in complex numbers).

The abs operator returns the magnitude of its argument. If applied to a vector, it returns a vector of the magnitudes of the elements. For example, if x = [2 -4 3-4i -3i]:

>> y = abs(x)

y =

2 4 5 3

The angle operator returns the phase angle (i.e., the "argument") of its operand in radians. The angle operator can also work element-wise across a vector. For example:

>> phase = angle(x)

phase=

0 3.1416 -0.9273 -1.5708

The sqrt function computes the square root of its argument. If its argument is a matrix or vector, it computes the square root of each element. For example:

>> x

x =

4 -9 i 2-2i

>> y = sqrt(x)

y =

2.0000 0 + 3.0000i 0.7071 + 0.7071i 1.5538 - 0.6436i

MATLAB also has operators for taking the real part, imaginary part, or complex conjugate of a complex number. These functions are real, imag and conj, respectively. They are defined to work element-wise on any matrix or vector.

MATLAB has several operators that round fractional numbers to integers. The round function rounds its argument to the nearest integer. The fix function rounds its argument to the nearest integer towards zero, e.g. rounds ``down'' for positive numbers, and ``up'' for negative numbers. The floor function rounds its argument to the nearest integer towards negative infinity, e.g. ``down.'' The ceil (short for ceiling) function rounds its argument to the nearest integer towards positive infinity, e.g. ``up.''

round rounds to nearest integer

fix rounds to nearest integer towards zero

floor rounds down (towards negative infinity)

ceil rounds up (towards positive infinity)

All of these commands are defined to work element-wise on matrices and vectors. If you apply one of them to a complex number, it will round both the real and imaginary part in the manner indicated. For example:

>> ceil(3.1+2.4i)

ans=

4.0000 + 3.0000i

MATLAB can also calculate the remainder of an integer division operation. If x = y * n + r, where n is an integer and r is less than n but is not negative, then rem(x,y) is r. For example:

>> x

x=

8 5 11

>> y

y=

6 5 3

>> r = rem(x,y)

r=

2 0 2

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