A Tutorial on Matlab Ch. 3 Programming in Matlab

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

AMS 27L LAB #2 Winter 2009

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

Introduction to MATLAB LAB 1

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Introduction to MATLAB Practical 1

INTRODUCTION TO NUMERICAL ANALYSIS

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Mechanical Engineering Department Second Year (2015)

ME 121 MATLAB Lesson 01 Introduction to MATLAB

PART 1 PROGRAMMING WITH MATHLAB

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

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Matlab Tutorial 1: Working with variables, arrays, and plotting

Graphics and plotting techniques

MATLAB INTRODUCTION. Risk analysis lab Ceffer Attila. PhD student BUTE Department Of Networked Systems and Services

Getting started with MATLAB

MATLAB TUTORIAL WORKSHEET

Introduction to MATLAB

A Brief Introduction to MATLAB

Overview. Lecture 13: Graphics and Visualisation. Graphics & Visualisation 2D plotting. Graphics and visualisation of data in Matlab

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

An Introduction to MATLAB II

Introduction to Python Practical 1

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

Computer Programming in MATLAB

Introduction to Matlab

TOPIC 6 Computer application for drawing 2D Graph

Introduction to MATLAB

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

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Introduction to MATLAB

Getting started with MATLAB

Introduction to Matlab

Lecturer: Keyvan Dehmamy

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Plotting using Matlab. Vytautas Astromskas

Matlab 1: Get Started

MATLAB Guide to Fibonacci Numbers

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

2. Plotting in MATLAB

Basic Graphs. Dmitry Adamskiy 16 November 2011

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 GNU-Octave

1-- Pre-Lab The Pre-Lab this first week is short and straightforward. Make sure that you read through the information below prior to coming to lab.

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

Math 375 Natalia Vladimirova (many ideas, examples, and excersises are borrowed from Profs. Monika Nitsche, Richard Allen, and Stephen Lau)

Introduction to 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:

This module aims to introduce Precalculus high school students to the basic capabilities of Matlab by using functions. Matlab will be used in

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

EE 301 Signals & Systems I MATLAB Tutorial with Questions

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

Chapter 1 Introduction to MATLAB

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Basic MATLAB Tutorial

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

Programming in Mathematics. Mili I. Shah

Computational lab on complex numbers

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

GEORGIA INSTITUTE OF TECHNOLOGY. SCHOOL of ELECTRICAL and COMPUTER ENGINEERING. ECE 2026 Summer 2018 Lab #0: Introduction to MATLAB

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Mini-Project System Simulation over AWGN Using BPSK Modulation

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations

Desktop Command window

Basic exercises. Array exercises. 1. x = 32:2: x = [ ] a = x + 16 b = x(1:2:end) + 3 c = sqrt(x) or c = x.^(0.5) d = x.^2 or d = x.

A Quick Tutorial on MATLAB. Zeeshan Ali

Unix Computer To open MATLAB on a Unix computer, click on K-Menu >> Caedm Local Apps >> MATLAB.

DSP First. Laboratory Exercise #1. Introduction to MATLAB

Programming 1. Script files. help cd Example:

Grace days can not be used for this assignment

ECE 102 Engineering Computation

What is Matlab? A software environment for interactive numerical computations

You can change the line style by adding some information in the plot command within single quotation marks.

Finding MATLAB on CAEDM Computers

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

1. Register an account on: using your Oxford address

MATLAB Modul 3. Introduction

LabVIEW MathScript Quick Reference

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

MATLAB Tutorial. Primary Author: Shoumik Chatterjee Secondary Author: Dr. Chuan Li

Introduction to MATLAB

LAB 1 General MATLAB Information 1

What is Matlab? The command line Variables Operators Functions

FOR LOOP. for <indexmin:indexstep:indexmax> {statements} end

Dr. Iyad Jafar. Adapted from the publisher slides

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

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

Matlab Tutorial and Exercises for COMP61021

PHYS 210: Introduction to Computational Physics MATLAB Exercises 2

Introduction to PartSim and Matlab

Matlab Tutorial for COMP24111 (includes exercise 1)

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

MATLAB SUMMARY FOR MATH2070/2970

Matlab Programming Introduction 1 2

An Introductory Tutorial on Matlab

MATLAB/Octave Tutorial

Lecture 6: Plotting in MATLAB

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

Introduction to Matlab. High-Level Computer Vision Summer Semester 2015

Transcription:

Department of Electrical Engineering University of Arkansas A Tutorial on Matlab Ch. 3 Programming in Matlab Dr. Jingxian Wu wuj@uark.edu

OUTLINE 2 Plotting M-file Scripts Functions Control Flows Exercises

PLOTTING Simple plotting >> x = [1 2 3 4 5]; >> y = [3-1 2-3 -4]; % vector x and y must be of the same length >> plot(x, y) >> x = linspace(0, 2*pi, 100); >> y = cos(x + pi/3); >> plot(x, y) >> xlabel('x'); % add a label to the x-axis >> ylabel('y'); % add a label to the y-axis >> title('cos(x + \pi/3)'); % add a title to the figure >> grid on; % turn on the grid >> print djpeg fig.jpg; % save the figure to a file.

PLOTTING Multiple curves in one figure Use the hold on command >> x = [0:0.01:5]; >> y1 = cos(x); >> y2 = exp(-x); >> y3 = sinc(x); >> plot(x, y1); >> hold on; >> plot(x, y2, 'k--'); % black, dashed line >> plot(x, y3, 'r-.'); % red, dash-dot line >> legend('cos(x)', 'exp(-x)', 'sinc(x)'); % adding legends >> set(gca, 'fontsize', 14); % change the fontsize Use help plot to find out more line styles and colors

PLOTTING Rescale a figure Use the axis command >> x = [-20:0.01:20]; >> plot(x, sinc(x)) >> axis([-5, 5-0.3 1]); % x range: [-0.5, 0.5], y-range: [-0.3, 1] Close one or more figures figure(1); % make figure 1 the current figure close; % close the current figure close all; % close all figures

OUTLINE 6 Plotting M-file Scripts Functions Control Flows Exercises

M-FILE SCRIPTS Script file: We can write a sequence of Matlab commands in an external file with extension *.m The file is usually called an m-file Execute the m-file file will execute all commands in the file

M-FILE SCRIPTS Create a script file 1. In Matlab, click on File New Script, to load the default editor 2. In the editor, type the commands: x = [0:0.01:10]; y1 = cos(x); y2 = exp(-x); plot(x, y1); hold on; plot(x, y1+y2, 'r--'); 3. Save the file. In the editor window, click on File Save test.m 4. Execute the file. In the editor window, click on Debug Run test.m

M-FILE SCRIPTS Execute the file Switch to the command window At the command prompt, type >> test % note that.m is not included in the command Important: the m-file to be executed must be in the current working directory >> pwd % display the current working directory ans = c:\skydrive\teaching\eleg3124\matlab >> ls *.m % list all m-files in the current working directory test.m

OUTLINE 10 Plotting M-file Scripts Functions Control Flows Exercises

FUNCTIONS Function A function can be defined and saved in a separate m-file Example function y = average(x) % function y = average(x) % compute the average of a vector x, and return the value to y N_element = length(x); y = sum(x)/n_element; Must start with the keyword: function Save to an m-file: in the editor window, File Save average.m. The name of the m-file must be the function name. The comments after the function title will be displayed when you type help average in the command line

FUNCTIONS Call the function In the command window >> x = 1:10; >> y = average(x) >> z = sqrt(x); >> average(z)

FUNCTION Function with multiple inputs and/or outputs function [addition, difference] = total_diff(x, y) % function [total, difference] = total_diff(x, y) % find the sum and difference between two vectors addition = x + y; difference = x - y;

OUTLINE 14 Plotting M-file Scripts Functions Control Flows Exercises

CONTROL FLOWS Matlab control flows: 1. if elseif else end 2. for end 3. while end

CONTROL FLOWS if end x = 10; y = sqrt(x)-x/3; if y < 0 end 'y is less than 0' y = y + 1;

CONTROL FLOWS if else end x = 10; y = sqrt(x)-x/3+1; if y < 0 'y is less than 0' y = y + 1; else 'y is greater than or equal to 0' y = y - 1; end

CONTROL FLOWS if elseif else end y = 0; if y < 0 'y is less than 0' y = y + 1; elseif y == 0 'y is 0' else 'y is greater than 0' y = y - 1; end

CONTROL FLOWS for end for mm = 1:2:10 end y(mm) = mm^2; % or equivalently: % this is more efficient y = [1:2:10].^2;

CONTROL FLOWS Double loops A = [1 3 2; 4-1 0]; [n_row, n_col] = size(a); for mm = 1:n_row row_avg(mm) = mean(a(mm, :)); for nn = 1:n_col B(mm, nn) = A(mm, nn).^2; end end % Alternatively and more efficiently row_avg = mean(a, 2); % calculate the row average B = A.^2;

CONTROL FLOWS while end x = 1; while x <= 20 end x = 3*x+2;

OUTLINE 22 Plotting M-file Scripts Functions Control Flows Exercises

EXERCISES 1. Plot the real part, imaginary part, and amplitude of the following function in the same figure. Use xlabel, ylable, legend, grid, and different color and line styles for different curves. Use a font size of 14 s t = e t e j2πt, 0 t 10 2. rescale the above curve for x between 0 and 5, and y between -2 and 2 3. Write a function to calculate the standard deviation of a vector, where is the i-th element of the vector, x is the average of the vector x i y 1 n n i 1 ( x i x) 2 Use the above function, evaluate the standard deviation of the following vector x = [0, 3, 1, 5, -2, -4, 3, 2, -5]

EXERCISES 4. Write a Matlab function to generate the first n Fibonacci number, where n is the function input. The Fibonacci number is defined as F F F Generate the first 10 Fibonacci number 5. Write a function, which takes a vector x as input. The output y is a vector of the same size, defined as follows Test your function with x = -3:3 6. Consider a sequnce y n 1 2 n 1 1 F n 1 xn, 0, 2 xn, x F Use while loop to find out what is the smallest n such that n 2 x x n x n n 0 0 0 0, x n 2x n 3/ 2 1 1 1 x n 100