Docsity

MATLAB Intro for CSSE 120 Students: Prep for ME323 at Rose Hulman, Study notes of Software Engineering

Partial preview of the text

Download MATLAB Intro for CSSE 120 Students: Prep for ME323 at Rose Hulman and more Study notes Software Engineering in PDF only on Docsity!

MATLAB INTRO:PREPARATION FOR ME323 CSSE 120—Rose Hulman Institute of Technology

What is MATLAB? ^

Numerical Computing Environment & ^

Programming Language ^

Made by The MathWorks Inc.

How is MATLAB different from Python? ^

MATLAB = "

mat

rix

lab

oratory“

^

MATLAB defaults to use a 2D matrix of numbers (oftype double) for as many things as possible ^

Many built in functions without loading libraries ^

Arrays are 1 based not 0 based ^

MATLAB actually has good help docs

☺

^

MATLAB is pricey! Ballpark $5000 the day you stopgoing to Rose to have a personal copy of MATLAB. ^

Used heavily in industry. Very common.

Sample comparison code ^

The first program we looked at in C was a print roottable function. Let’s see the syntax in Matlab.^ 

Review see code in C and Python first  See how MATLAB would code the root table problem

rootTable in MATLAB

Comments ^

will comment a line in MATLAB

^

Similar to # in Python^ 

Similar to // in C ^

First comment in a file is used as the help message.

elif statement if (a<0)

x= elseif(a>0)

x= else

x= end elif is done as elseif (one word)

while loops while (k>0)

k=k- end Similar changes to the if statement, can still use the

“break” statement to exit early if needed

Functions in MATLAB Easy to return multiple values, no “return” statementAutoruns first function which should have same name

as .m file

Inputs and outputs are optional ^

function

testFunction

^

No inputs or outputs

^

function

[x] = testFunction

^

Only 1 output called x

^

function

testFunction3(n)

^

Only 1 input called n

^

function

[y] = testFunction4(a,b,c)

^

3 inputs a, b, c and 1 output y

^

If the primary function has inputs call from commandline, if no inputs needed you can hit Run (or F5)

Hands on MATLAB script .m files ^

Make a .m script file the does the following ^

Save your script as practiceScript.m clc x = 3+4for i = 1:

i end

Things to try with that script file ^

View the output ^

Add a ; to the end of the i;

line

^

After running the code type in

x

or

i^

in the

interactive mode command line ^

View the variable in the Workspace ^

Notice how the value is stored as a 2D matrix oftype double that has 1 row and 1 column.Remember MATLAB!

Debugger ^

In this case MATLAB is more like Eclipse than IDLE ^

MATLAB has an easy to use debugger ^

Add a breakpoint to the start of your factorialTablecode (first line in the factorialTable function) ^

Step into the code

Fun quick keys/Shortcuts ^

Comment line

-^

Ctrl k

^

Uncomment line

-^

Ctrl t

^

Select All/Auto Indent -

Ctrl a

Ctrl I

^

Run .m file

-^

F

^

Autocomplete

-^

Tab

^

Save

-^

Ctrl s

^

Standard copy, cut, paste