> For the complete documentation index, see [llms.txt](https://meirian.gitbook.io/matlab/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://meirian.gitbook.io/matlab/getting-started/challenges.md).

# Challenges

During the workshop, activities are provided so that you can engage and test out the content shown for yourself. These are referred to as challenges.

## 1 Getting Started

### 1.1 Using MATLAB as a Calculator

Use the MATLAB command prompt to perform the following operations:

* [ ] 15 - 21
* [ ] 42 x 6
* [ ] 7 ÷ 2
* [ ] 8 ÷ 2(2 + 2)

{% hint style="success" %}
Answer the following questions:

* [ ] What happens in the Workspace each time you enter a new command?
* [ ] How can you 'recall' a previous command if you want to edit it?
* [ ] Which order of operations does MATLAB use: BODMAS (Brackets, Order, Division, Multiplication, Addition, Subtraction) or PEMDAS (Parentheses, Exponents, Multiplication, Division, Addition, Subtraction)?
  {% endhint %}

### 1.2 Saving your work

* [ ] Create a folder called "Workshop" and set it as the current working directory.
* [ ] Inside your new folder, create a script called "workshop\_script". You can use this script to take notes and work through the rest of the challenges.

{% hint style="info" %}
Hint: for information on Live scripts, visit <https://au.mathworks.com/help/matlab/matlab_prog/what-is-a-live-script-or-function.html>
{% endhint %}

## 2 Variables

### 2.1 The magic number

* [ ] Assign any integer number value to a variable called `x`
* [ ] Create a new variable `y` which is the sum of `x` and the number one larger than `x`
* [ ] Now create `z` starting with `y` add 9 and divide by 2.
* [ ] Then subtract `x` and multiply by 20.
* [ ] What did you get as the final value of `z`?

### 2.2 The Workspace

* [ ] Save your Workspace: `save filename.mat`, where `filename` is any name you choose.&#x20;
* [ ] Clear your Workspace: `clear`
* [ ] Load a previous Workspace: `load filename.mat`

{% hint style="info" %}
Hint: To save only one variable, `x`, use: `>> save filename.mat x`\
and to clear only one variable, `x`, use: `>> clear x`

When you load a Workspace, this won't clear existing variables in your Workspace; it will add to your existing Workspace.
{% endhint %}

### 2.3 Extension: What's in a name

* [ ] Enter your name as a string: `name1`.
* [ ] Create another variable, `name2`, and enter someone else's name.
* [ ] Try adding the strings together like we did with numbers earlier. What happens?

## 3 Vectors and matrices

### 3.1 Vectors and matrices

* [ ] Define the matrix `M` and row vector `p` shown below:![](https://firebasestorage.googleapis.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfG9RzQCZQ-JfoVLlzk%2Fuploads%2Fz4oH5tZB8KZlagahr4Pq%2Ffile.png?alt=media)
* [ ] Create a *column* vector called `ages` which contains the ages of three people: `age1`, `age2` and `age3`.
* [ ] What is the size of each of these arrays?

{% hint style="info" %}
Hint: You can check the size of a variable `x`, on MATLAB using the function `size(x)`
{% endhint %}

### 3.2 Random arrays

* [ ] Create a row vector which starts at 7 and ends at 19, counting up by 3.
* [ ] Create a row vector which starts at 10 and ends at 0, counting *down* by 2.
* [ ] Create a column vector of five 1s.
* [ ] Create a size 3x3 matrix array of random numbers.

{% hint style="info" %}
Hint: To get random numbers in MATLAB you will need to use the `rand()` function. Use 'Search Documentation' in the top right corner to learn about`rand`
{% endhint %}

### 3.3 Indexing

* [ ] Define the matrix `M` and row vector `p` shown below:![](https://firebasestorage.googleapis.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfG9RzQCZQ-JfoVLlzk%2Fuploads%2Ffo5XjkZMEDGzPUHBbTxb%2Ffile.png?alt=media)
* [ ] Extract the second column of `M` as a column vector called `col2`.
* [ ] Using indexing on `p`, let `q` be another row vector with the elements of `p` in reverse (that is, you should get `q = [2 14 5 9 2 17]`).
* [ ] Use assignment (`=`) to edit `p` to replace the second and last elements with 3 (you should get: `[17 3 9 5 14 3]`).
* [ ] Use the keyword `end` to get the *second-last* element of `p`.

## 4 Array Operations

### 4.1 Operation matrix

* [ ] The following array contains the average highest (column 1) and average lowest (column 2) temperatures (°F) in Los Angeles for each month in the calendar year: \
  `F = [67, 51; 67, 51; 67, 51; 69, 53; 70, 56; 73, 58;`\
  &#x20;    `77, 62; 79, 62; 78, 62; 75, 59; 71, 55; 67, 51]`.\
  Convert these temperatures to °C using this formula:  °C = (°F - 32) x 5 / 9
* [ ] Define the column vector `a = [3; 1; 2]` and the matrix `A = [6, 2, 6; 10, 4, 8]`. \
  Multiply both of these arrays together, noting that there is only one way to do this. What is the size of the result?

{% hint style="info" %}
Hint: Recall you can check the size of a variable using the function `size()`
{% endhint %}

### 4.2 Extension

* [ ] Transpose the vector `a = [3; 1; 2]` from the previous challenge to create `a_transpose`. Now try again to multiply the result with `A`&#x20;
* [ ] Find the determinant and inverse of `G = [1, 3; 4, -1]`.
