Basic MATLAB Intro III

Similar documents
A very brief Matlab introduction

PERI INSTITUTE OF TECHNOLOGY DEPARTMENT OF ECE TWO DAYS NATIONAL LEVEL WORKSHOP ON COMMUNICATIONS & IMAGE PROCESSING "CIPM 2017" Matlab Fun - 2

How to learn MATLAB? Some predefined variables

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

Image Processing CS 6640 : An Introduction to MATLAB Basics Bo Wang and Avantika Vardhan

Introduction to Matlab

Basic Graphs. Dmitry Adamskiy 16 November 2011

MATLAB Functions and Graphics

Basic plotting commands Types of plots Customizing plots graphically Specifying color Customizing plots programmatically Exporting figures

GRAPHICS AND VISUALISATION WITH MATLAB

A quick Matlab tutorial

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

INTERNATIONAL EDITION. MATLAB for Engineers. Third Edition. Holly Moore

Stokes Modelling Workshop

APPM 2460 PLOTTING IN MATLAB

Mechanical Engineering Department Second Year (2015)

APPM 2360 Project 2 Due Nov. 3 at 5:00 PM in D2L

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

An Introduction to MATLAB II

1 Introduction to Matlab

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

Homework 1 Description CmpE 362 Spring Instructor : Fatih Alagoz Teaching Assistant : Yekta Said Can Due: 3 March, 23:59, sharp

MATLAB SUMMARY FOR MATH2070/2970

AMS 27L LAB #2 Winter 2009

What is Matlab? A software environment for interactive numerical computations

CS129: Introduction to Matlab (Code)

The Singular Value Decomposition: Let A be any m n matrix. orthogonal matrices U, V and a diagonal matrix Σ such that A = UΣV T.

Logical Subscripting: This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression.

Introduction to Matlab

Introduction to image processing in Matlab

MAT 275 Laboratory 1 Introduction to MATLAB

Lab of COMP 406 Introduction of Matlab (II) Graphics and Visualization

The value of f(t) at t = 0 is the first element of the vector and is obtained by

Introduction to Matlab

Introduction to Matlab

INC151 Electrical Engineering Software Practice. MATLAB Graphics. Dr.Wanchak Lenwari :Control System and Instrumentation Engineering, KMUTT 1

matlab_intro.html Page 1 of 5 Date: Tuesday, September 6, 2005

Computer Vision 2 Exercise 0. Introduction to MATLAB ( )

CS1114 Assignment 5, Part 1

Image Manipulation in MATLAB Due Monday, July 17 at 5:00 PM

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

MATLAB for Image Processing

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

Image Processing Matlab tutorial 2 MATLAB PROGRAMMING

Matlab Introduction. Scalar Variables and Arithmetic Operators

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Octave Tutorial Machine Learning WS 12/13 Umer Khan Information Systems and Machine Learning Lab (ISMLL) University of Hildesheim, Germany

Introduction to Programming in MATLAB

CSE 123. Plots in MATLAB

MAT 343 Laboratory 4 Plotting and computer animation in MATLAB

2. Plotting in MATLAB

Scientific Functions Complex Numbers

Introduction to Finite Element Modelling in Geosciences: Introduction to MATLAB

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Introduction to Matlab

Basic Simulation Lab with MATLAB

Question 2. fprintf('\nenter elements of Matrix 1\n'); for i=1:m, for j=1:n; A(i,j)=input('Value='); end end

Desktop Command window

Introduction to MATLAB LAB 1

Introduction to Digital Image Processing

A Brief Introduction to MATLAB

Graphics Example a final product:

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Introduction to MATLAB

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

MATLAB Laboratory 09/23/10 Lecture. Chapters 5 and 9: Plotting

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

Introduction to MATLAB. CS534 Fall 2016

Matlab Handout Nancy Chen Math 19 Fall 2004

Lab 1 Intro to MATLAB and FreeMat

Computational Foundations of Cognitive Science. Inverse. Inverse. Inverse Determinant

Introduction to GNU-Octave

Basic Plotting. All plotting commands have similar interface: Most commonly used plotting commands include the following.

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

W1005 Intro to CS and Programming in MATLAB. Plo9ng & Visualiza?on. Fall 2014 Instructor: Ilia Vovsha. hgp://

CSE/Math 485 Matlab Tutorial and Demo

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

Computer Programming in MATLAB

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

Getting started with MATLAB

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

PC-MATLAB PRIMER. This is intended as a guided tour through PCMATLAB. Type as you go and watch what happens.

Objectives. 1 Basic Calculations. 2 Matrix Algebra. Physical Sciences 12a Lab 0 Spring 2016

2D LINE PLOTS... 1 The plot() Command... 1 Labeling and Annotating Figures... 5 The subplot() Command... 7 The polarplot() Command...

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

3 Introduction to MATLAB

TOPIC 6 Computer application for drawing 2D Graph

A Guide to Using Some Basic MATLAB Functions

Matlab Tutorial. Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can click Start MATLAB Demos to start the help screen.

Introduction to MATLAB for CSE390

Laboratory 1 Introduction to MATLAB for Signals and Systems

Introduction to MATLAB

Graphics in MATLAB. Responsible teacher: Anatoliy Malyarenko. November 10, Abstract. Basic Plotting Commands

HERIOT-WATT UNIVERSITY DEPARTMENT OF COMPUTING AND ELECTRICAL ENGINEERING. B35SD2 Matlab tutorial 1 MATLAB BASICS

Matlab Primer. Lecture 02a Optical Sciences 330 Physical Optics II William J. Dallas January 12, 2005

50 Basic Examples for Matlab

Math 7 Elementary Linear Algebra PLOTS and ROTATIONS

1 Introduction. 2 Useful linear algebra (reprise) Introduction to MATLAB Reading. Spencer and Ware (2008), secs. 1-7, 9-9.3,

Transcription:

Basic MATLAB Intro III Plotting Here is a short example to carry out: >x=[0:.1:pi] >y1=sin(x); y2=sqrt(x); y3 = sin(x).*sqrt(x) >plot(x,y1); At this point, you should see a graph of sine. (If not, go to the Window menu.) >plot(x,y2); overwrites the sine graph with x. Let s be a little more careful with managing our plots. First, erase what we ve just done with clf: >clf; We ll try to get the sine graph again: >h1=plot(x,y1); In order to overlay plots, we ll write >hold on; >h2=plot(x,y2); >plot(x,y3); Try this again, but with >x=[0 : pi-.1 : 30*pi] >y4=sin(x); >plot(x,y3); This is one of the dangers of line plots you are simply interpolating a list of points with line segments what you get may not resemble the function you inted to plot there is no simple way to avoid this, other than to sample your function densely enough that you don t see the line segments. Axes and formatting Let s add some axis information: >xlabel( x ); ylabel( y ); >leg( sin(x), sqrt(x), sin(x)*sqrt(x) ) You can control the horizontal and vertical dimensions of the graph: >axis([0 30-7 7]) Add a title: >title( Plotting test ) The three curves are too similar, so we can change some of the attributes to the first (which you may recall were labeled as h1 and h2): >set(h1, linewidth,5, color,[0.5 0 0.5], linestyle, -- ); >set(h2, linewidth,3, color,red, linestyle, -. ); 1

Saving plots At this point, we can save the plot as a pdf: >print -dpdf PlotTest.pdf or as an image: >print -dpng PlotTest.png Sub-plots Read about the subplot command and try the following: >clf; >t=[0:0.01:2*pi]; >x1=sin(t); >x2=sin(2*t); >x3=sin(3*t); >x4=sin(4*t); >subplot(2,2,1) >plot(t,x1); >title( sin(t) ) >subplot(2,2,2) >plot(t,x2); >title( sin(2t) ) >subplot(2,2,3) >plot(t,x3); >title( sin(3t) ) >subplot(2,2,4) >plot(t,x4); >title( sin(4t) ) Programming Inline functions You can program simple functions like x 2 sin x at the command line: f = @(x) x 2 sin(x) gives a function that can be evaluated. Be sure to use * when necessary. Matlab won t recognize 2x, but it will 2*x. In this case, you ll note that the function f can take square matrix arguments as well. Is it clear what f does to square matrices? Question: What is sin(x) when x is a matrix? What is 2 x when x is a matrix? 2

Loops You can program a for loop by writing for j = 1:m (Body of loop).m files More complex functions should be stored in your working directory in a.m file. For instance, a function that adds two matrices called myfunction, should be kept in a file called myfunction.m. You could write: function sum = myfunction(v, w) sum = v + w; The word function at the beginning of the file indicates that it s a function. It returns a variable called sum and it takes two arguments v and w. In order to call the function (at the command line, or within another function) we write: >myfunction(a,b) Here is another example: create a file called sinc.m containing the code: function y=sinc(x,a) % %sinc(a*x): to avoid 0 if(nargin<2) a = 1.0; epsilon = 0.000001; if abs(x)<epsilon y=1; else y=sin(a*x)./(a*x); 3

Create a Matlab function that takes 3 parameters, a, b and n that generates an n by n tridiagonal matrix: 1 b 0... 0 a 1 b... 0 A :=......... 0... a 1 b 0... 0 a 1 (Can you make this without using any loops?) Use this function to solve the system Ax = b = 1/2. 1 2... n for n = 10, a = 1/2, Managing variables Saving variables You can save the variable A in a.mat file by typing >save( A ) This stores the variable A in a file called A.mat. You can do the same thing with multiple variables and by naming the variables by typing >save( filename, var1, var2,...) Clearing variables You can clear the variable A by typing > clear( A ) Loading variables You can load the variables you saved by typing >load( filename ) Images Display a matrix as an image >x = peaks(256); >imagesc(x); >colormap(gray) Create and write an image >a=rand(512); creates a 512 512 matrix with random entries between 0 and 1. >imwrite(a, random.png, png ); 4

stores this as a grayscale image. A more complicated image >x=[0:0.004:1]; >y=[0:0.004:1]; >[X,Y]=meshgrid(x,y); This makes a uniform grid >Z1=(((X 0.5). 2 + (Y 0.5). 2) < 0.125); This is a disk! >imwrite(z1, circle.tif, tif ); >Z2=imread( circle.tif ); >Z2=imagesc(Z2); >colormap( Gray ); >axis equal; axis off; >axis([1 251 1 251]) An alternative to using imagesc is imshow. Exercise 10: Let s use the SVD as a naive image compression algorithm. Investigate the svd command. Run [U,S,V] = svd(z2); Observe that Z2 = USV, Rewrite this as a series of rank one matrices: Z2 = N j=1 A j, where each A j is a matrix having the same dimensions as Z2, and corresponding to one singular value. To compress the matrix, try to use the biggest N 0 singular values: N0 j=1 A j, starting with N 0 = 1, 2,... How do the corresponding compressions match up visually? Observe that compressing with N 0 terms means you are actually storing N 0 *(1 +length(u) +length(v)) numbers. You can try this now on a more natural image. To do so, you need to store your image in your Matlab directory. Load it using the imread command. For instance, by running IM = imread( thomas.jpg ) at the command line (if your image file is called thomas.jpg ). You ll notice that if the image is in color, IM is a 3-d array (a matrix of triples), and that the entries are stored as uint8. You can convert to double precision numbers by using the im2double command. You can covert from color format to grayscale by using rgb2gray. 5