introduction to matlab - university of oxfordgallas/lectures/2010_karen... · 2020. 10. 19. ·...

49
Introduction to Programming in Matlab Dr Karen Hampson 1 12.10.20

Upload: others

Post on 04-Feb-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

  • Introduction to Programming in MatlabDr Karen Hampson

    1

    12.10.20

  • Session Overview

    1. What is Matlab?

    2. Why Matlab?

    3. Navigating the Matlab Environment

    4. M-Files

    5. Loading data files and plotting

    6. Loops

    7. Functions

    8. Matlab coursework

    2

  • 1. What is Matlab?Matrix Laboratory

    3

  • 2. Why Matlab?

    • Friendly user interface

    • You don’t need to worry about initialising memory forstoring variables or data

    • You don’t need to declare the type of a variable

    • Widely used in research

    • It has many useful toolboxes

    4

  • 5

    3. Navigating the Matlab Environment

  • Opening Matlab

    6

  • 7

  • Working Folder – Where to Put Files

    8

  • Working Folder – List of Files

    9

  • Command Window

    10

  • M-Files: Files Containing Code

    11

  • Variables

    12

  • 13

    Demo:Command line as calculatorMatrices in command lineSay can type code here but ‘slow’

  • 14

    4. M-Files

  • Simple Program Example

    15

    Adding two numbers together

    Variable Name Assigned Value

    Lack of ; will cause theoutput to be displayed inthe command window

    clear all

    NumberOne = 4;

    NumberTwo = 3;

    TheSum = NumberOne + NumberTwo

    Clear any stored variables

  • Simple Program Example

    16

    Adding two numbers together

    Matlab Output

    M-File Name

  • Creating a New M-File

    17

  • Executing an M-File

    18

    • Editor tab• Press Run (or F5)• Check you are on

    the correct file

  • 19

    Demo:Open new M-fileShow example program

  • Program Example

    20

    Adding two numbers together

    What do you think would happen in this case?

    clear all

    TheSum = NumberOne + NumberTwo

    NumberOne = 4;

    NumberTwo = 3;

  • Errors

    21

    These display in the command window

    Error

    Location of Error

  • 22

    5. Loading Data Files and Plotting

  • Loading Data Files

    23

    This will form part of the exercises

    Txt Files: MyData = load('TheTextFileName.txt');

    Image Files: MyImage = imread('TheImageFileName.bmp');

    Make sure your files are in the correct directory (i.e. in the Matlab folder)!

    (CO01)

    (CO01)

  • Program Example

    24

    Loading data, plotting the data (CO01)

    All rows, column 2

    Plot the data

    clear all

    MyData = load('TheTextFileName.txt');

    xData = MyData(:,1);

    yData = MyData(:,2);

    plot(xData,yData)

    xlabel('x Variable')

    ylabel('y Variable')Label Axis

  • Program Example

    25

    Loading data, plotting the data (CO01)

  • 26

    Demo:Open new M-FileLoad and plot data

  • 27

    6. Loops

  • For Loop

    28

    Used throughout the coursework exercises

    If you only learn one loop – learn this one!

    for i = 1:3

    DO SOMETHING

    end

    Do Something 3 times

    i = 1;

    DO SOMETHING

    i = 2;

    DO SOMETHING

    i = 3;

    DO SOMETHING

  • Program Example

    29

    Summing numbers – SPOT THE ERROR

    Notice the indentation.This makes the codeeasier to read.

    Print the final value in the command line

    VectorOfNumbers = [1 4 6 7 9];

    for i = 1:5

    Sum = Sum + VectorOfNumbers(i);

    end

    Sum

    Column Vector clear all

  • 30

    Demo:Open new M-FileRun code with bug

  • Program Example

    31

    What if the length of the vector changes?

    VectorOfNumbers = [1 4 6 7 9];

    Sum = 0;

    for i = 1:length(VectorOfNumbers)

    Sum = Sum + VectorOfNumbers(i);

    end

    Sum

    We don’t want to change the code each time

    clear all

  • 32

    7. Functions

  • Functions

    33

    Have an input and outputThey take data and perform an action on it

    Output = FunctionName(Input)

  • Creating a New Function

    34

  • Creating a New Function

    35

    function [ output_args ] = Untitled( input_args )

    end

    So Matlabknows it is a function

    Code goes here

    Name of function

  • Function Example

    36

    Returning the sum of a set of numbers

    function [ TheSum ] = SumTheNumbers( VectorOfNumbers )

    Sum = 0;

    for i = 1:length(VectorOfNumbers)

    Sum = Sum + VectorOfNumbers(i);

    end

    TheSum = Sum;

    end

    The output must bedefined somewherein the function

  • Calling a Function

    37

    Example of calling TheSum function

    Create New M-File

    VectorOfNumbers = [1 4 6 7 9];

    [ TheSum ] = SumTheNumbers( VectorNumbers )

    Can you spot the error?

    clear all

    Function call

  • 38

    Demo:Create and call function

  • Calling a Function

    39

    Example of calling TheSum function

    Create New M-File

    VectorOfNumbers = [ 1 4 6 7 9];

    [ TheSum ] = SumTheNumbers( VectorOfNumbers )

    Must be the same

  • Tips/TricksIf you don’t know how to do something, type it in google

    40

  • 41

    8. Matlab Coursework

  • The Matlab Coursework

    42

    http://www-teaching.physics.ox.ac.uk/CO-MATLAB/html/course_text.html

    REMEMBER TO STORE YOURCODE ON YOUR HOMEDRIVE. NOT ON THE LOCALDRIVE OF THE COMPUTER.

  • What to Expect During Marking

    43

    CO01 - Grader

    Instructions

  • What to Expect During Marking

    44

    CO01 - Grader

    Enter Code

    Test Code

    Submit

  • What to Expect During Marking

    • You will have to run your code

    • You will have to explain your code

    • You will need to have a lab book with your programming work inside

    45

    CO02 – In Matlab Environment

  • Good Coding Practice

    46

    You will be assessed on this

    Aim

    AuthorDate

    What the lines do

  • What to Expect During Lab Sessions

    Demonstrators will online to help you

    Thursday 10-1 & 2-5

    Friday 10-1 & 2-5

    47

    Each session will have 3-4 demonstrators

  • What to do if you need more help?

    My lab session: Thursday Morning

    One-one-one or small group tutorials outside my session or outside Thursday and Friday

    [email protected]

    48

  • 49

    [email protected]