Due Date: Monday Oct 2, 2017 9:00 AM. This homework aims at giving you some experience with MATLAB variables.





1.  Type the following in the command window and submit the results. Briefy explain what each assignment does.

>> a = 1
>> b = 'x'
>> c = true
>> whos a b c
>> a == c
>> a + c
>> d = [1 2 3 4]
>> e = ['a' 'b' 'c' 'd']
>> f = ['abcd']
>> g = {‘a’ ‘b’ ‘c’ ‘d’}
>> h = { a b c d}
>> whos d e f g h






2.  What would happen if you go beyond the range for a particular type? For example, the largest integer that can be stored in int8 is 127, and the smallest integer is -128, so what would happen if we type cast a larger integer to the type int8? Smaller integer? Use the built-in functions intmin and intmax to find the largest and smallest integers that can be stored in int16 and int32.





3.  Think about what the results would be for the following expressions, and then type them in to the terminal to verify your answers. Briefly explain the results for each one.

>> 1\2 
>> 1/2
>> int8(1/2)
>> int8(1/3)
>> -5^2 
>> (-5) ^ 2 
>> 10-6/2 
>> 5*4/2*3






4.(a)  Define the following variables:

>> a
a =
     1     0
     2     1
>> b
b =
    -1     2
     0     1
>> c
c =
     3
     2
>> d
d =
     5


4.(b)  What is the result of each of the following expressions? Briefly explain what MATLAB is doing for each operation.

  1. a + b
  2. a .* b
  3. a * b
  4. a * c
  5. a + c
  6. a + d
  7. a .* d
  8. a * d





5.  Provide three different methods of generating the matrix a, one method should use the diag() function, one should use the eye function, and one should use the zeros function.

>> a
a =
     2     0     0
     0     2     0
     0     0     2






6.  Download this code. This code is full syntax errors. Fix the errors and submit the corrected code with name script_full_of_errors_fixed.m in your folder for this HW. Explain in front of each corrected MATLAB statement, why the error occurred. Modify the last two variables so that they display,

>> Persian
Persian =
Persian is a human language
>> Spanish
Spanish = 
    'Spanish '    'is '    ' another'    'language'


Modify the last line such that for the last line the code displays,

Persian is not the same as Spanish


Explain these results.





7.  Use MATLAB help to find out how you can create a new directory named mynewdir from MATLAB command line. Then change the working directory the newly created directory. Then create a MATLAB script in this directory named myscript.m with the following code in it,

% First create an array from -2*pi to 2:pi
x = -2*pi:pi/20:2*pi;

% Calculate |sin(x)|
y = abs(sin(x));

plot(x,y);


Now on MATLAB command line, run the script by calling its name. What do you get? Save the output as a figure and submit it with your homework.





8.  Now change your working directory to the original directory before you created mynewdir directory. Try to run the script myscript you had created again, from MATLAB command line. What do you get? and why?



Comments