Example Code (some of which we cover today)

• Go to https://gitlab.com/opensourcedave/matlabcourse (you can get here through my website too)
• On the right, click the drop down menu with the arrow pointing down
• Unzip (on windows click and drag contents to a new directory)
• In matlab cd to unzipped directory (On windows you can click on an empty space of the address bar to get the address. Highlight it, then copy it. Then in matlab paste it after the cd command)

# Exercises

## Operators & functions

1. Assign variables a,b,c to different integer values
2. Create an anonymous function of the quadratic equation:

$$\frac{-b \pm \sqrt{b^2-4ac}}{2a}$$

1. Consider a,b,c as coefficients in a quadratic equation ax2+bx+c

Challenge: Integrate the exponent of cosine of x between 0 and half pi

## Matrices 1

1. Create a column vector A with elements 1000 2000 3000
2. Create a row vector B with elements 4000 5000 6000
3. Combine the two vectors to create a 2x3 matrix C
4. Multiply all elements by 2

Challenge: See if you can identify the differences between functions repelem() and repmat().

## Strings

1. Assign variables to the following strings: 'Strings', ' ', 'are ', 'nice' '.'
2. Concatenate the strings in order with a new line between 'Strings' and ' '
3. Create a second string with your name.

Challenge:

1. Use strrep() to replace your name with my name
2. Use strsplit() and join() to rearrange the sentence order

HINT: Cells group arrays of different sizes. Indexing rules for cells are the same, but use {} instead of () in particular cases.

## Plotting Basics

1. Create values of y, where y = tangent(x), with 1000 x values between 80 and 100 HINT: use the function tan()
2. Display this plot with the following properties:
• Triangle points with magenta outline and blue filling (HINT: use help)
• y limited to -20 and 20
• Label the x and y axis

Challenge "For" loops in matlab are written as for i = r; <command(s)>; end where r is any valid range eg (1:1000) See if you can create animate a plot by using a for loop and drawnow()

## Subplots

Use 1000 x values between 1 and 10 for the following

1. plot the functions log(x) and sin(x) on the same subplot with ylim 0-2.5
2. plot the function the square root of ( e to the x ) in a subplot below 1.

After plotting, add a title to the top subplot

Challenge Make the second subplot twice as wide as the top

## Plotting 3D

1. Using surf and imagesc in seperate figures: plot the surface 1/x2 + 1/y2 between -3 & 3 for both x and y
2. Display the colorbar in each
3. Display the plots between -2 & 2 for both x and y
4. change the colormap

Challenge "For" loops in matlab are written as for i = r; <command(s)>; end where r is any valid range eg (1:1000) See if you can create animate a 3d plot by using a for loop and drawnow()

## Index & Subscripts

1. Create a random matrix between -10 and 90, with 10 rows and 9 columns. HINT: create a matrix with integers values between 0 & 100 first
2. Select values between the 3rd and 4th column and the 2nd and 6th and set these values equal to 3
3. Remove the 4th column.
4. Check the number of columns in the matrix.
5. Remove the 10th element from the matrix.
6. Check the size of the matrix again.

Thought for food: A third type of indexing exists in matlab - logical indexing. We used this secretly when using basic logic and the find function. Try this: A=randi(10,10) i=A==10 sum(sum(i)) ind=find(i) numel(ind) %Compare with the sum step above A(i) A(i)=3 i=A==10 sum(sum(i)) %Compare with other sum above

## Matrices 2

1. Create a 2x5 matrix where with elements are equal to a phone number (800 555 1234)
2. Use the function size to verify the size

Challenge Use reshape() to reshape this matrix into a square matrix, then use diag() on the output

## Matrices 3

1. Create a random matrix of size 3x2,
2. Matrix Multiply it by an identity matrix
3. Element wise multiply by 10
4. Sum all the Elements (both vertical and horizontal)

Challenge:

1. Use normrnd() to draw values from a normal distribution of mean 5 and standard deviation 3
2. Use histogram() to visualize your results

## EXTRA

### 1. Matrix manipulation

• Create a column vector with values 3 through 1502, incrementing by 1.5
• Resize the vector to be a matrix of size 20x50
• Duplicate all elements in place by 3 columns and 2 row
• Remove the fourth column

### 6. Logical indexing

• Create a matrix of size 10x12 with random integers between 5 & 15
• Set all values of 10 equal to 4
• Add 1 to all values greater than 10
• Get subscripts of all values equal to 8

## Operators & functions

a=1; b=3; c=5; p = -b + sqrt(b2-4*a*c)/(2*a); % use up arrow n = -b - sqrt(b2-4*a*c)/(2*a);

## Matrices 1

A = [1000; 2000; 3000]; B = [4000, 5000, 6000]; C = [A' B].*2;

## Strings

A='Strings' B=' ' C='are ' D='nice' E='.' string1=[A newline B C D E]; string2='David White' cellA={string1, string2} %OR cellA{1}=string1; cellA{2}=string2;

## Plotting Basics

x=linspace(80,100,1000) y=tan(x) plot(x,y,'m<','MarkerFaceColor','b')

## Subplots

x=linspace(1,10,1000) figure(1) subplot(2,1,1) plot(x,log(x));hold on plot(x,sin(x));hold off subplot(2,1,2) plot(x,sqrt(exp(x))) subplot(2,1,1) title('title')

## Plotting 3D

l=linspace(-3,3,100); [x,y]=meshgrid(l); z = 1./x2 + 1./y2; a=subplot(2,1,1) surf(x,y,z) b=subplot(2,1,2); imagesc(z) colormap(b,winter);

## Index & Subscripts

A=randi(100,10,9)-10 A(2:6,3:4)=3 A(:,4)=[] size(A,2) %should be 8 A(1)=[]; size(A); should be 1x79

## Matrices 3

sum(sum(rand(3,2).*eye(3,2)*10)) %single digits are treated as scalars, which are always element wise

## EXTRA

### Matrix Manipulation

A=[5:1.5:3202]'; A=resize(A,4,533); A=repelem(A,2,3): A(:,4)=[];

### Logical indexing

A=randi(10,10,12)+5; A(A==4)=4; i=(A>10); A(i)=A(i)+1; [n,m]=find(A==8)