Introduction to Scientific Programming in MATLAB Michael McLennan Software Architect HUBzero™ Platform for Scientific Collaboration.

Slides:



Advertisements
Similar presentations
Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Advertisements

Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Introduction to MATLAB The language of Technical Computing.
Plotting Selim Aksoy Bilkent University Department of Computer Engineering
Introduction to Graphing Using MATLAB. Line Graphs  Useful for graphing functions  Useful for displaying data trends over time  Useful for showing.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 4.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/27/2003.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/27/2003.
MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered:
Vectors and Plotting Selim Aksoy Bilkent University Department of Computer Engineering
Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.
Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.
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.
EPSII 59:006 Spring Outline Managing Your Session File Usage  Saving Workspace  Loading Data Files  Creating M-files More on Matrices Review.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
Lecture 13: M/O/F/ for Engineering Applications - Part 1 BJ Furman 26NOV2012.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
PLOTS AND FIGURES DAVID COOPER SUMMER Plots One of the primary uses for MATLAB is to be able to create publication quality figures from you data.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
Scientific Computing Introduction to Matlab Programming.
Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots.
Plot (x-values, y-values) >> x = linspace (-3, 3, 20); >> x = linspace (-3, 3, 20); >> y = 2*x – 1; >> y = 2*x – 1; >> plot (x, y) >> plot (x, y)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
EGR 115 Introduction to Computing for Engineers 2D Plotting – Part I Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
CMPS 1371 Introduction to Computing for Engineers VECTORS.
Introduction to MATLAB ENGR 1181 MATLAB 1. MATLAB Built-in Math Functions  Pre-defined in MATLAB ready for use exp(x) – exponential (e x ) log(x) – natural.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Lecture 12: M/O/F/ for Engineering Applications B Burlingame 27 April 2016.
PRELIMINARII Un semnal discret este definit pe mulţimea numerelor întregi Z → R Mulţimea Z are semnificaţia de timp (discret) Not ă m cu x[n] valoarea.
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Prof. N. P. Jadhav Presented by. Overview of MATLAB environment MATLAB is an interactive, matrix-based system for scientific and engineering numeric computation.
Introduction to Matlab
L – Modeling and Simulating Social Systems with MATLAB
Introduction to MATLAB
Introduction to MATLAB
Course Information Do not rely on color to distinguish curves (due to black-white printout). Use different line styles to distinguish curves (solid, dashed,
Lecture 25: Exploring data
Matrices and Arrays.
MATLAB How to use (using M-files) Double click this icon
MATH 493 Introduction to MATLAB
MATLAB How to use (using M-files) Double click this icon

MATLAB How to use (using M-files)
Introduction to MATLAB Plotting LAB 3
Communication and Coding Theory Lab(CS491)
Introduction to MATLAB
Introduction to MATLAB
Plotting Signals in MATLAB
Matlab tutorial course
Introduction to MATLAB
Introduction to MATLAB
How to Use MATLAB A Brief Introduction.
Introduction to Matlab
Matlab Basics Tutorial
Presentation transcript:

Introduction to Scientific Programming in MATLAB Michael McLennan Software Architect HUBzero™ Platform for Scientific Collaboration

Accessing MATLAB Start a workspace and type matlab NOTE: MATLAB requires a license from Purdue, so this works only when accessing from Purdue’s campus. Try some simple commands: >> x=1 >> y=2 >> z=2*x + y >> exit

Free Clone: GNU Octave In your workspace, type octave  Runs almost all MATLAB scripts  Creates plots  Missing fancy debugger  Missing “simulink” toolbox

Using Vectors … xaxis xaxis = 1:0.1:5 start with this increment by this end with this Create a series of number like this: xaxis = linspace(1,5,41) start with this end with this number of points Try this: >> xaxis = 1:0.1:5; >> length(xaxis) >> xaxis Try it with/without the semicolon. What does the semicolon do?

More Vectors xaxis Suppose you have an irregular spacing: xaxis = [1.0, 1.1, 3.0, 4.9, 5.0] square brackets comma-separated list of values Try this: >> y = 2*xaxis – 2 >> plot(xaxis,y) >> y = 3*xaxis*xaxis + 1 >> y = 3*xaxis.*xaxis + 1 Multiply element by element: [ …]

Matrices Suppose you want to define a matrix, like this: a = in MATLAB, that’s >> a = [ ; ; 1 1 1] a T = in MATLAB, that’s >> a’ Try these built-in functions: >> zeros(3) >> ones(4) >> eye(2) >> a * a’ + eye(3) Try this: >> a * a’ >> a’ * a >> a’ * a + 1

Image Processing Try this: >> im = imread(‘/apps/matlab/7.5/toolbox/simulink/simulink/simulinkteam.jpg’); >> figure; >> imshow(im); >> a = [ ; ; 1 1 1] >> im2 = imfilter(im, a, ‘conv’); >> imshow(im2); >> im2 = imfilter(im, a*a’, ‘conv’); >> imshow(im2); ‘conv’ operation Edge detection! charcoal effect Octave Users: Works in Octave 3.0 For earlier versions, download imread from

Matrix Indexing a = in MATLAB, that’s >> a = [ ; ; 1 1 1] Try this: >> a(1,1) >> a(2,1) >> a(1,2) row index column index Try this: >> a(1,1:3) >> a(1,:) whole row Try this: >> a(2,2) = 3 >> a Pick apart image pixel by pixel… >> im(1,5) >> im(3, :) >> im(1:100, 1:100)

Back to Plotting Try this: >> rv = im(3, :, 1) >> plot(1:length(rv), rv, ‘r’) >> title(‘row 3 from image’) >> xlabel(‘pixel index’) >> ylabel(‘color value’) >> hold >> gv = im(3, :, 2) >> plot(1:length(gv), gv, ‘g’) >> legend(‘red’,’green’) all column elements first component: r g b row index

Plotting Options Try this: >> clf >> plot(1:length(rv), rv, ‘r:o’) red, dotted line, circles r Red g Green b Blue c Cyan m Magenta y Yellow k Black w White - Solid line (default) -- Dashed line : Dotted line -. Dash-dot line + Plus sign o Circle * Asterisk. Point x Cross s Square d Diamond ^ Upward-pointing triangle v Downward-pointing triangle > Right-pointing triangle < Left-pointing triangle p Five-pointed star (pentagram) h Six-pointed star (hexagram)

Functions % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix(n,orient) half = floor(n/2); n = 2*half + 1; for i=1:n for j=1:n if strcmp(orient,'horz') m(i,j) = j-half-1; else m(i,j) = i-half-1; end % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix(n,orient) half = floor(n/2); n = 2*half + 1; for i=1:n for j=1:n if strcmp(orient,'horz') m(i,j) = j-half-1; else m(i,j) = i-half-1; end file: edgematrix.m im2 = imfilter(im, edgematrix(5,’horz’), ‘conv’);

Loops % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix(n,orient) half = floor(n/2); n = 2*half + 1; for i=1:n for j=1:n if strcmp(orient,'horz') m(i,j) = j-half-1; else m(i,j) = i-half-1; end % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix(n,orient) half = floor(n/2); n = 2*half + 1; for i=1:n for j=1:n if strcmp(orient,'horz') m(i,j) = j-half-1; else m(i,j) = i-half-1; end file: edgematrix.m i j

Conditionals % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix(n,orient) half = floor(n/2); n = 2*half + 1; for i=1:n for j=1:n if strcmp(orient,'horz') m(i,j) = j-half-1; else m(i,j) = i-half-1; end % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix(n,orient) half = floor(n/2); n = 2*half + 1; for i=1:n for j=1:n if strcmp(orient,'horz') m(i,j) = j-half-1; else m(i,j) = i-half-1; end file: edgematrix.m m = m = ‘horz' anything else

Programming the MATLAB Way % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix(n,orient) half = floor(n/2); n = 2*half + 1; for i=1:n for j=1:n if strcmp(orient,'horz') m(i,j) = j-half-1; else m(i,j) = i-half-1; end % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix(n,orient) half = floor(n/2); n = 2*half + 1; for i=1:n for j=1:n if strcmp(orient,'horz') m(i,j) = j-half-1; else m(i,j) = i-half-1; end file: edgematrix.m % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix2(n,orient) half = floor(n/2); o = ones(2*half + 1); m = [-half:half]' * o(1,:) if strcmp(orient,'horz') m = m' end % returns edge detection matrix of size n % orient is 'horz' for horizontal edges, and 'vert' for vertical function m = edgematrix2(n,orient) half = floor(n/2); o = ones(2*half + 1); m = [-half:half]' * o(1,:) if strcmp(orient,'horz') m = m' end =

Simple Input/Output % prompt for a phrase and say "hello" disp('Who are you?'); name = input('Enter your name: ','s'); age = input('Enter your age: '); mesg = sprintf('Hello, %s!', name); disp(mesg); file = input('Enter a file name: ','s'); fid = fopen(file,'w'); fprintf(fid, '%s is %d years old\n', name, age); fclose(fid); % prompt for a phrase and say "hello" disp('Who are you?'); name = input('Enter your name: ','s'); age = input('Enter your age: '); mesg = sprintf('Hello, %s!', name); disp(mesg); file = input('Enter a file name: ','s'); fid = fopen(file,'w'); fprintf(fid, '%s is %d years old\n', name, age); fclose(fid); file: hello.m >> hello Who are you? Enter your name: Michael Enter your age: 43 Hello, Michael! Enter a file name: info.txt String input Numbers and other stuff Use script name as a command to invoke the script

Other Resources MATLAB DOs and DONTs Tutorials at MathWorks