Question

In: Computer Science

MATLAB NEEDED. This lab will use functions and arrays.   The task within is to Add, Sub,...

MATLAB NEEDED.

This lab will use functions and arrays.   The task within is to Add, Sub, or Multiply 2 Matrix. See Manipulate Matrix doc for the methods. My reference to matrix below is because we are doing matrix math. The matrix are arrays.

Matlab built-in functions not allowed

You will write 3 functions (call those functions): 1. ADD two matrix,   2. SUBTRACT two matrix, and 3. MULTIPLY two matrix.

Requirements:

  1. Write the script to cycle until user chooses to stop.
  2. Using any input format, you pick um,
    1. Have the user choose to ADD, SUB, or MULTIPLY matrix
    2. Using a switch case
      1. ADD matrix for this task you have the user enter the size of the matrix. That is enter row and column.   (NOTE row and col must apply to both matrix so you only ask once.)

Using random fill create two different matrix.

NOTE in code example used: b = int16(rand(r,c)*n);      int allows signed integers.

% digits 0 to n integers

For this lab we care about signed number so please use int not uint.

Call a function to add these 2 matrix and return the SUM

  1. SUB matrix for this task you have the user enter the size of the matrix. Same rule apples for SUM as applied for ADD.

Using random fill create two different matrix

Call a function to subtract these two matrix and return the DIFFERENCE

  1. MULTIPLY matrix for this task you have to ask the user for 3 dimension rows of A, columns of A,

and columns of B. (From theory Columns of A are the rows of B)

Using random fill create two different matrix

Call a function to multiply these two matrix and return the PRODUCT

  1. Write 3 functions ADD, SUB, and MULTIPLY (Use your own names)
    1. Please do not pass the rows and columns thru the parameter list. Each parameter list should have only 2 array names, to be processed (added, subtracted, or multiplied ).
    2. Instead use the size function in Matlab.  For ADD and SUB only need size of one array. For MULTIPLY need size of both arrays because you need 3 of the 4 row, column numbers for the 3 for loops i, j, k.
    3. OUTPUT from each function is the resultant array.

ADD (SUB) MATRIX

C = A + B   Requires 2 for loops to move throught the arrays

Equation   C(i,j) = A(i,j) + B(i,j)   Fundamentally you are adding each element of A to its corresponding element of B and putting the sum into the corresponding element of C

MULTIPLYING MATRICES

There are some strick rules that must be applied.

C = A * B     requires that the columns of A must equal the rows of B.   C’s dimensions are the Rows of A by the Columns of B.

A(Ar,Ac)    B(Br,Bc)   C(Cr,Cc) given these dimensions for the three matrices

Rules above require Ac = Br    thus the # of columns of A equal the rows of B.

Then Cr = Ar   The rows of C must equal the rows of A

Then Cc = Bc The columns of C must equal the columns of B

The equation for filling the Matrix C is:

C(i,j) = ∑Ai,k) *B(k,j)

This notation uses i, j , k:

  1. i runs from 1 to Ar
  2. j runs from 1 to Bc

These 2 for loops fill Matrix C

  1. k controls the summation of a sum of products of each row element in Matrix A times each respective column element in Matrix B
  2. (   Note that ∑ demands a third for loop using k from 1 to Ac)

This is similar to Add and Sub the Answer array must be filled by 2 for loop.   This only adds a third for loop to manage the summation. use equation for summation. (∑).  

Solutions

Expert Solution

This example shows basic techniques and functions for working with matrices in the MATLAB® language.

First, let's create a simple vector with 9 elements called a.

a = [1 2 3 4 6 4 3 4 5]
a = 1×9

     1     2     3     4     6     4     3     4     5

Now let's add 2 to each element of our vector, a, and store the result in a new vector.

Notice how MATLAB requires no special handling of vector or matrix math.

b = a + 2
b = 1×9

     3     4     5     6     8     6     5     6     7

Creating graphs in MATLAB is as easy as one command. Let's plot the result of our vector addition with grid lines.

plot(b)
grid on

MATLAB can make other graph types as well, with axis labels.

bar(b)
xlabel('Sample #')
ylabel('Pounds')

MATLAB can use symbols in plots as well. Here is an example using stars to mark the points. MATLAB offers a variety of other symbols and line types.

plot(b,'*')
axis([0 10 0 10])

One area in which MATLAB excels is matrix computation.

Creating a matrix is as easy as making a vector, using semicolons (;) to separate the rows of a matrix.

A = [1 2 0; 2 5 -1; 4 10 -1]
A = 3×3

     1     2     0
     2     5    -1
     4    10    -1

We can easily find the transpose of the matrix A.

B = A'
B = 3×3

     1     2     4
     2     5    10
     0    -1    -1

Now let's multiply these two matrices together.

Note again that MATLAB doesn't require you to deal with matrices as a collection of numbers. MATLAB knows when you are dealing with matrices and adjusts your calculations accordingly.

C = A * B
C = 3×3

     5    12    24
    12    30    59
    24    59   117

Instead of doing a matrix multiply, we can multiply the corresponding elements of two matrices or vectors using the .* operator.

C = A .* B
C = 3×3

     1     4     0
     4    25   -10
     0   -10     1

Let's use the matrix A to solve the equation, A*x = b. We do this by using the \ (backslash) operator.

b = [1;3;5]
b = 3×1

     1
     3
     5

x = A\b
x = 3×1

     1
     0
    -1

Now we can show that A*x is equal to b.

r = A*x - b
r = 3×1

     0
     0
     0

MATLAB has functions for nearly every type of common matrix calculation.

There are functions to obtain eigenvalues ...

eig(A)
ans = 3×1

    3.7321
    0.2679
    1.0000

... as well as the singular values.

svd(A)
ans = 3×1

   12.3171
    0.5149
    0.1577

The "poly" function generates a vector containing the coefficients of the characteristic polynomial.

The characteristic polynomial of a matrix A is

det(λI−A)

p = round(poly(A))
p = 1×4

     1    -5     5    -1

We can easily find the roots of a polynomial using the roots function.

These are actually the eigenvalues of the original matrix.

roots(p)
ans = 3×1

    3.7321
    1.0000
    0.2679

MATLAB has many applications beyond just matrix computation.

To convolve two vectors ...

q = conv(p,p)
q = 1×7

     1   -10    35   -52    35   -10     1

... or convolve again and plot the result.

r = conv(p,q)
r = 1×10

     1   -15    90  -278   480  -480   278   -90    15    -1

plot(r);

At any time, we can get a listing of the variables we have stored in memory using the who or whos command.

whos
  Name      Size            Bytes  Class     Attributes

  A         3x3                72  double              
  B         3x3                72  double              
  C         3x3                72  double              
  a         1x9                72  double              
  ans       3x1                24  double              
  b         3x1                24  double              
  p         1x4                32  double              
  q         1x7                56  double              
  r         1x10               80  double              
  x         3x1                24  double              

You can get the value of a particular variable by typing its name.

A
A = 3×3

     1     2     0
     2     5    -1
     4    10    -1

You can have more than one statement on a single line by separating each statement with commas or semicolons.

If you don't assign a variable to store the result of an operation, the result is stored in a temporary variable called ans.

sqrt(-1)
ans = 0.0000 + 1.0000i

As you can see, MATLAB easily deals with complex numbers in its calculations


Related Solutions

) Use functions and arrays to complete the following programs. Requirements: • You should use the...
) Use functions and arrays to complete the following programs. Requirements: • You should use the divide-and-conquer strategy and write multiple functions. • You should always use arrays for the data sequences. • You should always use const int to define the sizes of your arrays. a. Write a C++ program. The program first asks the user to enter 4 numbers to form Sequence 1. Then the program asks the user to enter 8 numbers to form Sequence 2. Finally,...
Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the...
Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the "Game of Life". Though not a "game" in any traditional sense, it provides interesting behavior that is specified with only a few rules. This project asks you to write a program that allows you to specify an initial configuration. The program follows the rules of LIFE to show the continuing behavior of the configuration. LIFE is an organism that lives in a discrete, two-dimensional...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices...
PHP Question: Subject: Functions and Arrays. INSTRUCTIONS: Objective: • Write functions. • Use server-side includes. •...
PHP Question: Subject: Functions and Arrays. INSTRUCTIONS: Objective: • Write functions. • Use server-side includes. • Create and utilize a numeric array. • Create and utilize an associative array. Requirements: Create a script file called functions.php, where you will be adding functions. priceCalc() function: • 2 parameters: price and quantity. • Create a numeric array of discounts with the following values: 0,0,.05,.1,.2,.25. • Get the discount percent from the array using the quantity as the index. If the quantity is...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
Create a web calculator with JavaScript. Only four operations (Add, Sub, Multiplication, and Division) Use a...
Create a web calculator with JavaScript. Only four operations (Add, Sub, Multiplication, and Division) Use a CE button to cancel the operation.    Use the following website or any online resource as a reference. Don’t use their CSS or any other code.                 https://freshman.tech/calculator/ I am trying to learn please make comments then I will understand better.
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a...
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 = 3kHz and f2 = 5kHz and then sample it with fs = 10kHz. Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not? If there is aliasing specify which component is casing the aliasing
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT