Introduction to Matlab PHY September Why Matlab? No need to compile code Huge amount of built-in functions Highly optimized and fast, in general.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Programming with MATLAB
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Introduction to MATLAB
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
MATLAB Fundamentals.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Part 1 Chapter 3 Programming with MATLAB PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University.
Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
MATLAB Programming COMM2M Harry R. Erwin, PhD University of Sunderland.
Introduction to Matlab
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Introduction to Literate Programming in Matlab 2WN50 – Week programming-in-matlab.pptx?dl=0.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Tutorial on Matlab Basics
EEE 161 Applied Electromagnetics
응용 전산 및 실습 MATLAB – Chapter 3 행렬연산
L – Modeling and Simulating Social Systems with MATLAB
Programming with MATLAB
Scripts & Functions Scripts and functions are contained in .m-files
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
MATLAB: Structures and File I/O
StatLab Matlab Workshop
Matlab review Matlab is a numerical analysis system
MATH 493 Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
MATLAB Tutorial Dr. David W. Graham.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
T. Jumana Abu Shmais – AOU - Riyadh
INTRODUCTION TO MATLAB
244-2: MATLAB/Simulink FUNDAMENTALS
Announcements P3 due today
CS 111 Introduction to Computing in Engineering and Science
Presentation transcript:

Introduction to Matlab PHY September 2016

Why Matlab? No need to compile code Huge amount of built-in functions Highly optimized and fast, in general Environment combines programs/functions and an interpreter M-files can also be compiled for more speed Extensive documentation Large user base and robust online community are other resources Ease of use

The Matlab Environment Editor: for writing scripts and m-files Workspace: variables and objects File explorer Command window: for running functions, scripts, defining variables, and calculating

Defining variables - scalars Every variable in Matlab is a matrix CASE SENSITIVE Definition will be printed unless suppressed with a ; at the end of the line Scalar: >> x = 5; c = 3e8; Complex number: >> z = 3 + 9*i; or >> z = 3 + 9*j; Predefined variables: pi, ans, eps, Inf

Defining variables - scalars Format output using the format type command Possible types:

Defining Variables – vectors and matrices Row vector: r = [ ], or r = [1, 3, 6, 0, 4] Column vector: c = [2; 5; 6; 3] or c = [ ]’ Transpose operator: ’ Matrices: M = [3 4 5; 6 5 3; 9 0 7], or M = [[3 6 9]’ [4 5 0]’ [5 3 7]’] Elements: M(3,2) = 0, r(4) = 0 ones(m,n), zeros(m,n), eye(m,n), rand(m, n)

The colon operator Defining vectors a = start:interval:end a = 3:10 = [ ] a = 4:.2:5 = [ ] Negative increments also allowed Element selection a(3:6) = [ ] Row/column selection from a matrix M(2,:) = second row, M(:,3) = third column linspace() and logspace() operators

Strings and multi-line entry Strings Single, not double, quotes s = ‘the five thousand pound man’ Concatenation: cat = [s ‘by rahsaan roland kirk’] = ‘the five thousand pound man by rahsaan roland kirk’ Multiline entry >> u = [ … ]

Mathematical operators Work on numbers and variables, including vectors and matrices Row vector r, column vector c r *c = scalar, c*r = matrix (outer product) Matrix/vector dimensions must make sense Left division is crazy: A x = b -> x = b \ A Element-by-element operations: add a dot before the operator

Built-in functions Basic info func(args) Described by the help, use help funcname Usually operate on matrices and vectors element-by-element Some have matrix versions, found by appending ‘m,’ ie sqrtm(M) Examples sin(x) log(x) sqrt(x) mean(v)

Plotting Array vs array For example, t = 1:30; v = 11:40; plot(t, v.^2, ‘p--m’) xlabel(‘t’) ylabel(‘v^2’) fplot() to plot functions plot3() for 3d plots hold on, then plot() again to plot multiple data sets subplot() for sub-figures Axes/titles rendered using TeX, you can specify LaTeX using 'Interpreter','Latex'

Using the help demo for tutorials and videos for new users help function_name for documentation about a specific function lookfor search_string to search the help Matlab Answers:

M-files – script files Two types of m-files: script files, and function files Script files Executed using run script_name Consist of a series of commands that would otherwise be typed into the command window Variables defined in script files persist in the workspace, that is, they are global

M-files – function files Function files Take arguments Do stuff (calculate, manipulate input) Return result(s) Like functions defined in normal programming languages Help comments printed in response to help funcname First line of help comments indexed for searching by lookfor Run by typing funcname(args) Variables are local

input() and disp() input(prompt_string) takes input from the user Useful for making interactive functions that take input from input() instead of as arguments disp(value) will display its argument to the command window Can take variables or constants Like the Python print() function

fprintf() Finer grained control over output Displays text and variables, controlled with format codes and control codes fprintf(‘text’, var1, …)

File I/O Saving Default behavior: save a.mat file that contains part or all of the workspace.mat files are binary, only readable by Matlab save filename var1 … varN ASCII files can be saved using the -- ascii option Useful for saving data or results save filename --ascii Loading Can load an entire workspace or select specific variables load filename var1 … varN Loaded files can be assigned to variables grades = load grades.txt

Control Structures – if statements if statements Execute when their condition is true Can be a single line for one statement if condition statement end And ( & ), Or ( | ), and Not ( ~ ) Order of operations: Not, And, Or If-else for two possibilities If-elseif-else for more possibilities

Control Structures – switch statement Like if-elseif-else, but it tests a single expression Tests expression and takes action depending on its value Optional otherwise case for input that doesn’t match any of the cases

Loops – for loop Repeats statements inside the loop a set number of times Uses colon notation Possible to use negative step sizes

Loops – while loop Run until some condition is met Careful not to make a condition that will never be met Ctrl-C to stop execution, if you find yourself in an infinite loop

Recursive functions Functions that call themselves Can implement some things much more cleanly For example, the factorial function here doesn’t require a loop Exercise: write an m-file to calculate the Ackermann function function f = factorial(n) % Calculate the factorial n! if n == 0 out = 1; else out = n*factorial(n – 1); end f = out;

Anonymous Functions Functions defined in the command window, not in an m- file Can include variables from the workspace Function doesn’t change if the variables do fhandle expression

Functionals (aka function functions…) Written like normal functions, but one of the arguments is a function Use varargin to pass arguments to functions inside