Você está na página 1de 32

Welcome to MATLAB!

With Alyssa

About STS

Free Technology Classes

Public classes Custom Technology Workshops

Ask a Trainer (AAT)

Go to wisc.edu/sts to learn more!

One-on-one appointment Online chat email

Online

Lynda.com Manuals and class files

What is MATLAB?

Used for statistical analysis, mathematics, signal


processing, etc. Today, we will be covering: The Matlab Interface M Code Arrays and Matrices Writing Scripts and Functions If , while and for loops Loading data Plotting

The MATLAB Interface



Command Window Workspace Command History Current Directory Creating a new script or function Help Function

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)

More Complex Scripts


Create your income after state taxes given the table
taxes = baseTax + percentage * (excessCutOff - annualWage);
At Least But Less Than Tax Computation

$0
$ 10,750 $ 21,490 $ 100,000 $ 236,600 or over

$ 10,750
$ 21,490 $ 100,000 $ 236,600

4.40% of excess over $0


$473.00 plus 5.84% of excess over $10,750 $1,100.216 plus 6.27% of excess over $21,490 $6,022.793 plus 6.27% of excess over $100,000 $14,587.613 plus 7.65% of excess over $236,600

Arrays
a structure that allows you to store multiple values
under one variable name

Example 1:

Create an array of length 10 that begins at one and increments by one


Example 2: Using the linspace function, create an array of

length 10 that begins at 5 and increments by 5 to 50


Linspace(START,END,NUMBER OF ELEMENTS)

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

Matlab uses the . operator to operate on individual


numbers in the matrix

1. Multiplying individual numbers in a matrix Example 1: square each component in randomData, using the . operator

Matrix Multiplication

Example: Multiply the following matrices


together: A. [1,2,3; 2,3,4] * [2,2;5,8]
B. [1,2,3; 2,3,4] * [2,3;3,10;9,4]

Your First Function

Function: a set of commands that performs


given calculations given a specified input
Example: Create a function that converts degrees from Celsius to Fahrenheit Hint: Fahrenheit = 9/5*Celsius + 32;

On your Own: Calculating income


Rewrite the script calculating annual income by turning it into a script, accepting an input hourlyWage

What is a for loop?

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

For and If Loops

Write a for loop that starts at 1, ends at 20 and

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

Putting it together: Importing Data, For Loops and If statements


Example: Using the excel file given, import the data as a matrix Make a function that takes in a matrix called
grades In a for loop evaluate each students final grade: If the student got a 92 or higher on the final, raise
their final grade by 5 percentage points If the student got a 90 or higher on the final, raise their final grade by 2 percentage points Make sure no student gets higher than a %100

Plotting
Important plotting commands:

plot(x) plot(x,y) title(title) xlabel(x) ylabel(y) hold on legend

Plotting MATLAB vs Excel


MATLAB
Steeper learning curve

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

Difficult to get around default styles and colors

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]

Example 2: Plot the following curves on the same


graph, provide a legend, against x data from 0 to 2pi, with 100 data points
Hint: use the hold on function to plot 2 different equations on
the same graph

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()

Example: Graphing Complex Numbers


Make a vector from 0 to 7 Make a row vector a consisting of complex numbers : a = 1i.^(0:7) Graph the real and imaginary parts of this number Add a legend, title and axis labels

On Your Own: Graphing


Example: Write a script that calls calcIncome, and use the function to create a graph of incomes from hourly wages of $5 to $500 an hour
Hint: you will need to use a for loop

Evaluations
Matlab 1 with Alyssa Morrow Password: 18818
https://docs.google.com/a/wisc.edu/forms/d/1H2Pv-t1jTjC1lRBMIXIW_eT66UaYOFCMxM9A-a6HVA/viewform

Você também pode gostar