This is the solution to Homework 2: Problems - Variables, Values, and Types.

The following figure illustrates the grade distribution for this homework.

♣ **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
```

**Answer:**

```
>> a = 1
a =
1
>> b = 'x'
b =
x
>> c = true
c =
logical
1
>> whos abc
>> whos a b c
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x1 2 char
c 1x1 1 logical
>> a == c
ans =
logical
1
>> a + c
ans =
2
```

`a == c`

because, although they are different types, `a`

logical type of true is also represented by the integer 1, so `a`

and `c`

are equivalent, and can be added together.

```
>> d = [1 2 3 4]
d =
1 2 3 4
>> e = ['a' 'b' 'c' 'd']
e =
abcd
>> f = ['abcd']
f =
abcd
>> g = {'a' 'b' 'c' 'd'}
g =
1×4 cell array
'a' 'b' 'c' 'd'
>> h = { a b c d}
h =
1×4 cell array
[1] 'x' [1] [1×4 double]
>> whos d e f g h
Name Size Bytes Class Attributes
d 1x4 32 double
e 1x4 8 char
f 1x4 8 char
g 1x4 456 cell
h 1x4 491 cell
```

`e`

and `f`

are equivalent, because they are both just character arrays, `e`

just concatenated all the individual single characters into one array, while `f`

was already concatenated together. g is a cell array of characters ‘a’, ‘b’, ‘c’, and ‘d’, and h is a cell array of the variables `a`

, `b`

, `c`

, and `d`

.

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

**Answer:**

```
>> int8(200)
ans =
int8
127
>> int8(-150)
ans =
int8
-128
>> intmax('int16')
ans =
int16
32767
>> intmin('int16')
ans =
int16
-32768
>> intmax('int32')
ans =
int32
2147483647
>> intmin('int32')
ans =
int32
-2147483648
```

**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
```

**Answer:** :

```
>> 1\2
ans =
2
>> 1/2
ans =
0.5000
>> int8(1/2)
ans =
int8
1
>> int8(1/3)
ans =
int8
0
>> -5^2
ans =
-25
>> (-5)^2
ans =
25
>> 10-6/2
ans =
7
>> 5*4/2*3
ans =
30
```

**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
```

**Answer:**

```
>> a = [1 0; 2 1]
a =
1 0
2 1
>> b = [-1 2; 0 1]
b =
-1 2
0 1
>> c = [3; 2]
c =
3
2
>> d = 5
d =
5
```

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

`a + b`

`a .* b`

`a * b`

`a * c`

`a + c`

`a + d`

`a .* d`

`a * d`

**Answer:**

```
>> a + b
ans =
0 2
2 2
>> a .* b
ans =
-1 0
0 1
>> a * b
ans =
-1 2
-2 5
>> a * c
ans =
3
8
>> a + c
ans =
4 3
4 3
>> a + d
ans =
6 5
7 6
>> a .* d
ans =
5 0
10 5
>> a * d
ans =
5 0
10 5
```

**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
```

**Answer:**

```
>> a = eye(3,3) * 2
a =
2 0 0
0 2 0
0 0 2
```

```
>> d = [2 2 2]
d =
2 2 2
>> a = diag(d)
a =
2 0 0
0 2 0
0 0 2
```

```
>> a = zeros(3,3);
>> a(1,1) = 2;
>> a(2,2) = 2;
>> a(3,3) = 2;
>> 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.

**Answer:**

The corrected script can be found here

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

**Answer:**

You can create a new directory and switch the current directory to it using the following commands,

```
>> mkdir mynewdir
>> cd mynewdir
```

The script `myscript`

generates a plot of $y$ versus $x$ where $y = |sin(x)|$. In doing so, MATLAB opens a new window called **plot window** that contains the plot of y as a function of x. Here is the resulting figure:

**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?

**Answer:**

You get an error message like the following,

```
>> cd mynewdir\
>> myscript
>> cd ..
>> myscript
Undefined function or variable 'myscript'.
```

And this happens because the script we are trying to run is neither in MATLAB’s working directory, nor in any of MATLAB’s search paths. Therefore, MATLAB gives an error, as it cannot find the requested file.