Welcome To MATLAB!: With Alyssa
Welcome To MATLAB!: With Alyssa
With Alyssa
About STS
Online
What is MATLAB?
M Code
M code syntax:
Addition (+) Subtraction (-) Division (/) Multiplication (*) Exponential (^) Sind(x) Cos(x) Pi
Examples
Basic Functions
exp() sqrt() Log( fix() mod() round() clc clear help
What is a script?
a file where you can save your m code Why write a script?
Example: Write a script that calculates age of a dog in human years (hint: every dog year corresponds to 7 human years)
$0
$ 10,750 $ 21,490 $ 100,000 $ 236,600 or over
$ 10,750
$ 21,490 $ 100,000 $ 236,600
Arrays
a structure that allows you to store multiple values
under one variable name
Example 1:
Matrices
A multidimensional array Syntax: matrix = [1 2 3; 4 5 6]
Manipulating Matrices
To Retrieve data :
Enter the row and column of the position in the matrix you
would like to access Example: matrix(2,3)
To Edit data:
Assign a value to a certain position in a matrix
Example: matrix(2,3) = 100
To Delete a row :
Matrix(9,:) = []; Example: delete Column 4 of your matrix
Matrix Multiplication
In Matlab, you can multiply
Individual numbers in a matrix Multiply 2 matrices together
1. Multiplying individual numbers in a matrix Example 1: square each component in randomData, using the . operator
Matrix Multiplication
For Loops
Syntax: for i = startVal : endVal
[is it your birthday?]
end
For Loops
Example: In a script, write a for loop that starts at 1, ends at 20 and prints the current state of the loop (ie. on the 20th loop, the code should print 20)
If Statements
Syntax: if (x > 5) x = x + 5 elseif (x < 5) x = x + 2 else x = x 3 end
does the following: If a number in the loop is divisible by 2, display the number Otherwise, do not display the number Hint: you will need to use mod(X,Y)
Challenge: Take this script, and turn it into a function with an input parameter y = the number to divide x by
Calculating Income
Example: modify the function calcIncome, taking an input parameter of hourly wage calculate the annual wage, and use this amount to determine the base tax and tax percentage
Plotting
Important plotting commands:
plot(x) plot(x,y) title(title) xlabel(x) ylabel(y) hold on legend
EXCEL
Smaller learning curve (Most of you already know it!) Easy to make simple line and bar graphs
Better means to make more complex graphs (i.e.3-D graphs) Can edit colors, plot shape with only one line of code
Plotting
Example: Plot the following data and label the axes
and add a title X = [1 2 3 4 5 6 7 8 9 10] Y = [5 10 15 20 25 30 35 40 45 50]
y = sin(x) y = cos(x)
3D Plotting
Example: 3D plot
Use the function plot3 to make a 3D line with the following data:
X = [1 2 3 4 5 6 7 8 9 10] Y = [5 10 15 20 25 30 35 40 45 50] Z = [0,7,14,21,28,35,42,49,56,63,70]
Dont forget a title and to label your axes!
Complex Numbers
Example: a = 1 + 3i Generating a Complex Number: (-1)^0.5 log(-1) Functions for Complex Numbers: isreal() imag() real()
Evaluations
Matlab 1 with Alyssa Morrow Password: 18818
https://docs.google.com/a/wisc.edu/forms/d/1H2Pv-t1jTjC1lRBMIXIW_eT66UaYOFCMxM9A-a6HVA/viewform