1 Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix.

Slides:



Advertisements
Similar presentations
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Advertisements

Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
MATLAB’s extensive, device-independent plotting capabilities are one of its most powerful features. They make it very easy to plot any data at any time.
Introduction to Matlab. I use Matlab for: Data analysis Data plotting Image Analysis Also – Simulations (solving odes/pdes/finite element methods) – Minimisations,
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
1 Introduction to MatLab: Image Processing - MatLab stands for Matrix Laboratory. - Most of the programming operations have as input or output a matrix.
Introduction to MATLAB MECH 300H Spring Starting of 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.
Introduction to Matlab Jianguo Wang CSSCR September 2009.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
259 Lecture 15 Introduction to MATLAB. 2 What is MATLAB?  MATLAB, which stands for “MATrix LABoratory” is a high- performance language for technical.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
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 Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
INTRODUCTION TO MATLAB LAB# 01
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.
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.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2010.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Scientific Computing Introduction to Matlab Programming.
Introduction MATLAB stands for MATrix LABoratory.  Basics  Matrix Manipulations  MATLAB Programming  Graphics  Image types  Image Processing  Useful.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Introduction to MATLAB 7 MATLAB Programming for Engineer Hassan Migdadi Spring 2013.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MATLAB – PT1 The purpose of this workshop is to get you started and to have fun with MATLAB! Let’s talk a little and decide on what we will be covering.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2012.
Introduction to Matlab
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Matlab for Engineers Matlab Environment Chapter 2.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
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,
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
EEE 242 Computer Tools for Electrical Engineering
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
ECE 1304 Introduction to Electrical and Computer Engineering
Computer Application in Engineering Design
L – Modeling and Simulating Social Systems with MATLAB
Ch3 Graphics Overview of Plotting Editing Plots
MATLAB DENC 2533 ECADD LAB 9.
StatLab Matlab Workshop
Communication and Coding Theory Lab(CS491)
Matlab Basics Tutorial
Presentation transcript:

1 Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix or a vector. Prepared by Fred Annexstein University of Cincinnati Some Rights Reserved CS100: Into to Computer Science

2 Overview Like Scratch, MatLab is an interpreted programming language. Unlike Scratch, Matlab is a language in which you type commands at the prompt. Try these MATRIX AND VECTOR OPERATIONS This is how we can define a vector >> v=[1, 2, 3] Matlab prints out the following v = Similarly we can define a matrix >> M= [ 1 2 3; 4 5 6; 7 8 9] The result is: M = If you want to suppress the MatLab output then you need to finish the line with semicolon as follows. >>M= [ 1 2 3; 4 5 6; 7 8 9];

3 To find the sum of the columns of M we can use the 'sum' function. >> sum(M) ans = To find the sum of all elements use >> sum(sum(M)) ans = 45 Note that the 'sum' function takes both matrices and vectors as parameters. The inner 'sum' takes a matrix and outputs a vector which is the input for the outer 'sum'. By default the output is assigned to the 'ans' variable unless we assign the output explicitly as follows. >> s=sum(M) s =

4 >> result= s+v result = Note that some operations can produce long output. Typing 'more on' will switch to paging mode which controls the output dump. To perform a component-by-component operation on a vector or matrix you need to prepend the arithmetic operator by a '.' >> M.* M ans = Note that this squares each element of the matrix. The result is different from Usual squaring the matrix as shown below. >> M*M ans =

5 Projection Say you want to extract some rows and columns of a matrix. This is called a projection. We simply give the subset of rows and columns as parameters, as follows >> M11=M(2:3, 2:3) M11 = To specify all elements in a given dimension one can use ':‘ So to get all rows but just columns 1 and 2, we type >> A= M( :, 1:2) A =

6 Each function you create should be separated in a *.m file and should have the same name as the file. Create an m-file by going to the menu option File>New>M-file The syntax for defining a function is: function result= myfunction(A,b) where A and b are parameters and result is the variable whose value is returned as a result from the function. myfunction is an arbitrary function name. Let's write and save a function to calculate a simple polynomial. (put the following two line in a file named mypoly.m) mypoly.m function y=mypoly(x) y= 3*(x.^2)+ 4*x + 5; At the MatLab prompt type the following which gives us 800 points on the real line interval from -4 to +4, with each pair of points separated by 0.01 >> x= -4:0.01:4; Building FUNCTIONS

7 Plots Let us evaluate our polynomial along the interval we just defined >> y=mypoly(x); Now to plot the function we simply type at the prompt >> plot(x,y) This will pop up a graphical window with the plot of y=3x^2 +4x +5 in the interval [-4; 4]. If for some reason we need to overlay two plots on the same graph the command 'hold on' can be used to hold the current display and show all subsequent plots on top of the first one. 'hold off' turns that capability off. >>hold on >>plot(x, 50*sin(x))

8 Let us first draw a 2-D circle: >>cosx=cos(x); >>sinx=sin(x); >>plot(cosx,sinx) We can use the same idea in 3D to build a helix: >> plot3(cosx,sinx,x); Exercise: Usually the third dimension is called z, so create a z-axis from 0 to 100 and plot a helix along this axis. 3D PLOTS and SURFACES

9 SURFACES >> x=1:10; >> y=1:10; Create a 10x10 matrix using outer product operation (here we are using the transpose of a vector x) >> Z=x'*y; Note that here Z is a matrix holding the z value for every combination of x and y. Look at the matrix values as a surface. >> surf(x,y,Z); How about the surface of the sine function >>S=sin(Z); >> surf(x,y,S); % Add some labels and other fancy stuff xlabel('X'); ylabel('Y'); zlabel('Z'); title('My Plot'); 3D PLOTS and SURFACES

10 MatLab has all the control we have seen in Excel and Scratch, including if then-else statements, repeat loops and for loops. If possible avoid using 'for' loops. Try to rewrite the computation in matrix form. Because MatLab is interpreted 'for' loops take a lot of time. Try these examples: x=0; for i=1:31 y(i)= sin(x); x=x+0.1; end Can also be rewritten as: x2=0:0.1:3; y2=sin(x2); Loops in MatLab

11 Matrices and vectors can have only elements of the same type. They also have fixed dimensions. To overcome this limitation cell arrays are introduced. C= cell(3,2) C{1,1}=[1,2]; C{1,2}=[3,4]; C{2,1}=[5 6; 7 8]; C{2,2}=[ ; ]; C{3,1}= ones(3); C{3,2}='Hello'; To access an element type: >> C{3,1} ans = >> C{2,2}(1,3) ans = 11 Note the use of the two different kinds of parentheses. The first one '{}' accesses the cell array elements. The second one '()' accesses the elements of the object contained in the cell array. CELL ARRAYS in MatLab

12 Let’s talk about image files and their formats….. Color vs GrayScale Basic functions: img=imread('image1.jpg'); imwrite(img, 'image2.jpg'); IMGJPG=imread(‘face.jpg’); IMGTIFF=imread('face1.tiff'); imshow(IMGJPG); Example Program: Inverting an image %To invert or to add two images we need to convert to double and % then rescale the result back so that it looks like an image %This does not work: RES= IMG1; RES= 1 - double(IMG1)/255; figure imshow ( uint8(round(RES*255)) ) Exercise: Find 2 images online and add them together. WORKING WITH IMAGES in MatLab

13 RESIZE >> img=imread('ric.jpg'); >> size(img) ans = >> rowV=linspace(1,435,312); >> newrows=round(rowV); >> newIMG=img(newrows,:,:) >> image(newIMG); Exercise: Finally when you are done, save your workspace and turn in on Bb. Next time: More on Images, including filtering and Edge detection WORKING WITH IMAGES in MatLab