MATLAB HW

MATLAB HW

The input function is a MATLAB built-in function that allows a program to accept user input. For example, if one wanted to ask the user to enter a number between 1 and 5, the MATLAB code would look like:

 

            some_num = input(‘Enter a number between 1 and 5’)

 

This code would ask the user to enter a number between 1 and 5, and store the input number in the some_num variable. You may be wondering what the single quotes are. In MATLAB, single quotes denote a string. A string is a sequence of characters, such as  ‘dog’, and ‘ate my homework!’. For the purposes of this lab, you can consider a string to simply represent text.

 

1) Based on the information above, write the MATLAB code to ask the user to enter a velocity in m/s. Be sure to store the input in a variable that has a fitting name.

 

 

 

 

 

 

 

 

One can also do element-wise multiplication with vectors and matrices in MATLAB (Hadamard product). It is, however, different syntax than multiplying scalar values. For example, if we wanted to multiply a vector x by the scalar 4 (in other words, scale the vector by 4), we would simply write the code 4 * x. This syntax does not work if we want to multiply x by another vector, say y. In order to do element-wise multiplication for vectors and matrices, we need to add a dot to our mathematical operator. Instead of x * y, we would write the code x .* y. The need for the . next to the operator is only necessary for multiplication and division. Addition and subtraction follows the normal, conventional syntax.

 

2) You have two Matrices, A and B. Assume that they are the correct dimensions (i.e. Both rows and columns are equal). Write the MATLAB code to do the following.

 

            a) Add A and B

 

 

            b) Subtract B from A

 

 

            c) Multiply A and B

 

 

            d) Divide B by A

 

 

e) A Cubed (Hint: remember that A raised to the third power is the same as A times A times A)

 

3) Scripts and functions (also known by their extension as .m files) are ways of organizing code in units or modules. Use the links below to answer the following questions:

 

MATLAB scripts and functions: http://www.mathworks.com/help/matlab/learn_matlab/scripts-and-functions.html

 

MATLAB creating and running scripts

http://www.mathworks.com/help/matlab/matlab_prog/create-scripts.html

 

 

 

            a) What is the difference between a script and function?

 

 

 

 

 

            b) How do you create a new script or function in MATLAB?

 

 

            c) List two ways to run a script in MATLAB

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon
"FIRST15"

Order Now