INTRO TO MATLAB

34
Introduction To Matlab Class 1 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD Double click the matlab icon When prompted click “Skip”, then type: mkdir C:/MatlabClass/YourLastName cd C:/MatlabClass/YourLastName

description

MATLAB

Transcript of INTRO TO MATLAB

  • Introduction To MatlabClass 1Instructors: Hristiyan (Chris) Kourtevand Xiaotao Su, PhD

    Double click the matlab iconWhen prompted click Skip, then type:

    mkdir C:/MatlabClass/YourLastNamecd C:/MatlabClass/YourLastName

    Optional Presentation Title Unit Name

    IntroductionAsk Questions!Slides, notes, assignments, and other info will be at:http://ruccs.rutgers.edu/matlab_course/

    Optional Presentation Title Unit Name

    SetupClick Matlab mkdir C:/MatlabClass/YourLastNamecd C:/MatlabClass/YourLastName

    Optional Presentation Title Unit Name

    ProgrammingMatlab has its own programming languageWhat is a program?A series of instructions that the computer followsAlso referred to as Code or a Script

    Optional Presentation Title Unit Name

    Two Options For Where To Issue Commands/InstructionsIn the command windowIn a script (program) executed by the command window.

    Optional Presentation Title Unit Name

    Hello WorldAt the command prompt type:my_var = hello world;

    And press ENTER.Note: The _ character is the shifted dash SHIFT + -

    Optional Presentation Title Unit Name

    Hello World my_var = hello world;

    Variable NameSingle quotes denote character stringSemi-colon is so you dont output the result to the screen.

    Optional Presentation Title Unit Name

    Variables my_var = hello world;

    Variable Namemy_varhello worldnum_apples = 7num_apples7num_oranges = 3.53.5num_oranges

    Optional Presentation Title Unit Name

    MemoryEach of the values are stored in memory associated with the variable nameThe who command will list the current variables in memoryTyping my_var will return the value of that variableYour computers memory

    Var NameValuemy_varhello worldnum_apples7num_oranges3.5subject_namejohn smith

    Optional Presentation Title Unit Name

    Setting valuesAs you have seen you set the value of a variable with the = signYou can also set a variable equal to another variable or the output of an operation on a variable.

    Optional Presentation Title Unit Name

    Setting valuesnew_var = my_varfruit = num_apples + num_orangesthird_character = my_var(3)this is 7this is 3.5this is now 10.5num_apples = 6what is the value of fruit now?

    Optional Presentation Title Unit Name

    Changing around strings

    Typing my_var(3) will return l

    my_var(3) = my_var(1) Now what is the value of my_var?

    my_var is currently hello world

    Optional Presentation Title Unit Name

    Calling FunctionsA function is a program that you call on to do some sort of actionlength(hello world)length(my_var)disp(my_var)

    Optional Presentation Title Unit Name

    Nesting Functionsdisp(length(my_var))What happens here?disp( length(hello world) )disp(11)11

    Optional Presentation Title Unit Name

    Your first programWhat is pseudo-code and why we need itTyping commands one at a time isnt very efficient, especially when you want to do the same ones repeatedlyso we create a program to do itThis program will be called mix_strings

    Optional Presentation Title Unit Name

    CommentsComments are descriptions of what is happening in your code. They are need to follow what is happening for debugging purposesAt the beginning of a script you put in a type of comment called a header

    Optional Presentation Title Unit Name

    % mix_strings.m % % Displays the string hello world % Then scrambles letters in that string and% displays the scrambled string% % written by XYZ 3/6/2005 % symbol makes this line commented out This means that the computer will ignore these linesName of fileDescription: What does this file do?Who wrote it and when?These lines arent for the computer, they are for you

    Optional Presentation Title Unit Name

    So what is the header good for?Save the file At the command line type:help mix_strings

    All matlab programs have headers. Find out information on disp, pause, and length

    Optional Presentation Title Unit Name

    What else?lookfor can be used to search for programs that have certain words in their headers.lookfor stringlookfor random

    Optional Presentation Title Unit Name

    Back to the programclear all;my_var = hello world;mixstr = my_var;mixstr(3) = my_var(1);mixstr(1) = my_var(3);disp(mixstr);

    Optional Presentation Title Unit Name

    Now run itIn the command window:mix_strings

    Optional Presentation Title Unit Name

    loopsUsed to repeat the same chunk of code many times

    for i=1:5disp(i)end;

    Optional Presentation Title Unit Name

    mix_strings2.mclear all;my_var = hello world; mixstr = xxxxxxxxxx;for i=1:length(my_var)mixstr(i) = my_var(i);end;

    disp(mixstr)

    Optional Presentation Title Unit Name

    rand and ceilrand will create a random number from 0 to 1ceil will round up any number to the next integerTogether you can create random integers ceil(rand*7)

    Optional Presentation Title Unit Name

    task 1Replace each x in mixstr with a random character from my_var.Start with the first x, then the second x

    hello worldxxxxxxxxxxxxxResult could be -> wdolhrdlhlwe

    Optional Presentation Title Unit Name

    % mix_strings3.m%%Displays the string 'hello world, then scrambles letters in that string%and displays the scrambled string%% written by Chris Kourtev 7/12/2010 clear all;my_var = 'hello world'; mixstr = 'xxxxxxxxxxxxx'; disp(my_var);for i = 1:length(mixstr) mixstr(i) = my_var(ceil(rand*length(my_var)));end;

    disp(mixstr);

    Optional Presentation Title Unit Name

    Vectors & MatrixesA string is a list of charactersYou can also have lists of numbers using vectors and matrixesvect = [1, 2, 5, 6, 3] mat = [1, 5, 3; 2, 1, 5; 7, 9, 0]mat = [1, 5, 3; 2, 1, 5; 7, 9, 0]

    Optional Presentation Title Unit Name

    Vectors & Matrixesvect = [1, 2, 5, 6, 3] mat = [1, 5, 3; 2, 1, 5; 7, 9, 0]Vectmatmat = [1, 5, 3; 2, 1, 5; 7, 9, 0]note: vectors are basically 1 dimensional matrixes

    12563

    153215790

    Optional Presentation Title Unit Name

    Getting Values out of Vectors & MatrixesVectmatvect(3) returns 5mat(3,1) returns 7Note: to get a cell you use (ROW, COLUMN)

    12563

    153215790

    Optional Presentation Title Unit Name

    vector as a 1D matrixvect = [1; 2; 3]vect2 = vect

    123

    123

    Optional Presentation Title Unit Name

    transposing matmat =mat = if you set t_mat = matt_mat(3, 1) is equal to mat(1,3)

    Rows and columns are swapped when you transpose a matrix

    153215790

    127519350

    Optional Presentation Title Unit Name

    performing operationsvect + 3vect + vectvect - 3vect + [5, 4, 3]mat+mat

    Optional Presentation Title Unit Name

    multiply and divideDont forget to put a dot before multiplication and division symbolsvect.*3mat.*0.5vect./2vect2 = [2; 2; 4]vect.*vect2vect./vect2

    Optional Presentation Title Unit Name

    task 2Create a 6(rows)x4(columns) matrix filled with random integer values between 1 and 20Create a zeroes matrix of the same dimensionsReplace each zero with a random number from the other matrix (as we did in the mix_strings program)Display each value in the new mixed matrix

    Hint 1: You will need to use nested loopsHint 2: Look up how to use the zeros command

    ** Advanced task: Design your program so that it can work with matrices of any dimension just by changing the number of rows and column. Hint: Look up the size command