Scientific Functions Complex Numbers

Similar documents
A General Introduction to Matlab

Some elements for Matlab programming

Finding, Starting and Using Matlab

Chapter 1 MATLAB Preliminaries

Matlab and Psychophysics Toolbox Seminar Part 1. Introduction to Matlab

MATLAB Functions and Graphics

A. Matrix-wise and element-wise operations

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

CNBC Matlab Mini-Course

More on Plots. Dmitry Adamskiy 30 Nov 2011

Lecturer: Keyvan Dehmamy

Dr Richard Greenaway

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

Introduction to GNU-Octave

LAB 1 General MATLAB Information 1

Matlab course at. P. Ciuciu 1,2. 1: CEA/NeuroSpin/LNAO 2: IFR49

CNBC Matlab Mini-Course

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Information for Candidates. Test Format

Matlab Practice Sessions

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Chapter 1 Introduction to MATLAB

Script started on Thu 25 Aug :00:40 PM CDT

QUICK INTRODUCTION TO MATLAB PART I

Fundamentals of MATLAB Usage

short-reference.mht 1/ /WK/

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

A BRIEF INTRODUCTION TO MATLAB Fotios Kasolis Introduction

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

General MATLAB Information 1

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

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Mechanical Engineering Department Second Year (2015)

A very brief Matlab introduction

EE168 Handout #6 Winter Useful MATLAB Tips

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

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

What is Matlab? The command line Variables Operators Functions

MATLAB Workshop Dr. M. T. Mustafa Department of Mathematical Sciences. Introductory remarks

MATLAB Introductory Course Computer Exercise Session

MATLAB and Numerical Analysis

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

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

University of Alberta

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

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

SGN Introduction to Matlab

GRAPHICS AND VISUALISATION WITH MATLAB

Stokes Modelling Workshop

Introduction to Programming in MATLAB

Consider this m file that creates a file that you can load data into called rain.txt

Lecture 1: What is MATLAB?

Getting started with MATLAB

Image Processing Matlab tutorial 2 MATLAB PROGRAMMING

Math 2250 MATLAB TUTORIAL Fall 2005

Introduction to MATLAB for CSE390

Scientific Computing Lecture 1: MATLAB fundamentals

ENGR Fall Exam 1

PREPARED FOR: ME 352: MACHINE DESIGN I Fall Semester School of Mechanical Engineering Purdue University West Lafayette, Indiana

Basic MATLAB Intro III

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

A QUICK INTRODUCTION TO MATLAB

Programming in MATLAB

MATH 3511 Basics of MATLAB

MATH 5520 Basics of MATLAB

Introduction to MATLAB

Matlab Guide #4 (created on April 9, 1998) 1

PROGRAMMING WITH MATLAB WEEK 6

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

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Introduction to MATLAB Practical 1

CS129: Introduction to Matlab (Code)

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

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

Built-in Types of Data

Laboratory 1 Octave Tutorial

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

MATLAB QUICK START TUTORIAL

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

Chapter 2 (Part 2) MATLAB Basics. dr.dcd.h CS 101 /SJC 5th Edition 1

INTRODUCTION TO MATLAB. Padmanabhan Seshaiyer

Excel R Tips. is used for multiplication. + is used for addition. is used for subtraction. / is used for division

Programming in Mathematics. Mili I. Shah

Teaching Manual Math 2131

Arithmetic and Logic Blocks

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

Matlab Tutorial, CDS

Introduction to Matlab

Python. Olmo Zavala R. Python Exercises. Center of Atmospheric Sciences, UNAM. August 24, 2016

Matlab Workshop I. Niloufer Mackey and Lixin Shen

MATLAB Constants, Variables & Expression. 9/12/2015 By: Nafees Ahmed

Computer Programming in MATLAB

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

PSY8219 : Week 6. Homework 5 Due Today. Homework 6 Due October 8. Readings for Today Attaway Chapter 6, 10, and 12

Lab 1 Intro to MATLAB and FreeMat

Getting Started with MATLAB

Introduction to MATLAB

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

INTRODUCTION TO MATLAB PROGRAMMING Lec 1.1: MATLAB Basics

Transcription:

CNBC Matlab Mini-Course Inf and NaN 3/0 returns Inf David S. Touretzky October 2017 Day 2: More Stuff 0/0 returns NaN 3+Inf Inf/Inf 1 -Inf, -NaN 4 Scientific Functions Complex Numbers Trig: Rounding: Modular: Exponential: Primes: sin, cos, tan, asin, acos, atan sinh, cosh, tanh, asinh, acosh,... floor, ceil, round, fix rem, mod exp, log, log2, log10, sqrt factor, primes sqrt(-16) 3.5i 2-3.5i (2+3i) * (4+5i) Polynomials: roots, polyfit, polyval 2 5 Matrix Functions Determinant: det Inverse: inv, pinv Eigenvalues: eig, svd Fourrier: fft And many, many more... isreal(3) isprime(1 : 13) isnumeric([2 3 5]) isempty([ ]) isinf(inf) isnan(nan) islogical(1 == 1) ischar('a') isequal('foo', 'aardvark') Predicates 3 What percentage of the first 1000 integers is prime? mean(isprime(1:1000)) 6

Return Values Functions can return multiple values: hist(randn(2000,1)) Variable In and Out A = rand(5, 3); hist(randn(2000,1), 50) s = size(a) counts = hist(randn(2000,1), 5) [rows, cols] = size(a) [counts, centers] = hist(randn(2000,1), 5) 7 10 Optional Return Values Functions can choose whether to return values, deping on if the user is asking for values. nargin and nargout Inside a function, nargin is the number of input arguments supplied with the call. plot([1 2 3], [3 1 2]) h = plot([1 2 3], [3 1 2]) set(h, 'LineStyle', '--') set(h, 'LineWidth', 8) no return value single return value nargout is the number of output arguments requested with the call. 8 11 Variable Number of Arguments Testing nargin/nargout Some functions accept a variable number of arguments: peaks peaks(10) 9 function [x,y,z] = nargtest(p,q,r,s,t) if nargout >= 1 x = 50; if nargout >= 2 y = 'foo'; if nargout >= 3 z = 3:7; whos Try: a = nargtest(5,6,7) [a, b] = nargtest(3) [a, ~, c] = nargtest(9,8) % show the local workspace 12

Name Spaces Base workspace: variables created outside of any function exist in the base workspace. Local workspaces: each function executes in a separate local workspace holding the arguments, return variables, and any local variables created by the function. Functions cannot access variables of the base workspace. Scripts Called By Functions Scripts do not have their own workspaces. A script called from the keyboard executes in the base workspace. A script called form within a function executes in the function's local workspace. 13 16 Name Spaces (cont.) Global workspace: variables declared global by a function are accessed in the global workspace. It's a good idea to also declare the variable global in the base workspace. clear x Resetting Variables removes variable x and undoes any global declaration You can also click on a variable in the workspace pane and hit the Delete key, or right-click on the variable and choose from the menu. clear all clears everything clear global clears global declarations 14 whos global shows all global variables 17 global pts pts = 0 : pi/20 : 2*pi ; Global Variables function h = circ(x,y) % draws a circle centered on (x,y) global pts hh = plot(x+cos(pts), y+sin(pts)); if nargout > 0 h = hh; % return h only if requested Handle Graphics Root = 0 Figure = 1, 2,... Axes Line Text Image Surface 15 18

Taking Apart A Figure clf, plot(rand(5, 3)) ax = get(gcf, 'Children') get(ax) lines = get(gca, 'Children') get(lines(1)) peaks rotate3d on 3D Graphics or click on the rotation arrow in the toolbar set(gca, 'CameraViewAngleMode', 'manual') or right-click in the figure, select Rotate Options, then select Fixed Aspect Ratio Axes 19 22 Multiple Axes: Subplot Plotting Surfaces clf subplot(2,2,1), plot(rand(5, 5)) subplot(2,2,2), bar3(rand(5, 3)) subplot(2,2,3), a=rand(15, 1); pie(a, a > 0.7) subplot(2,2,4), polar(cos(0:150)) set(gca, 'Position', [0.32 0.1 0.4 0.4]) [x, y, z] = peaks; surf(x, y, z, z) surf(x, y, z, x) surf(x, y, z, rand(length(x))) 20 23 Exploring Graphics Objects set(gca,'units') set(gca) propedit(gca) click on More Properties Matlab online documentation: Help pulldown menu or '?' icon: > Documentation > MATLAB > Graphics > Graphics Objects 21 Plotting in 3D Don't type all this in! Download this file: www.cs.cmu.edu/~dst/tutorials/matlab/helix.m or cd /afs/andrew/usr/dst/matlab function helix pts = 0 : pi/20 : 4*pi; x1 = cos(pts); y1 = sin(pts); x2 = cos(pts+pi); y2 = sin(pts+pi); z = pts/(2*pi); clf, whitebg(gcf, [0 0 0]), hold on plot3(x1, y1, z, 'y') plot3(x2, y2, z, 'w') axis([ -3 3-3 3 0 2]) view(95, 9) 24

colors = 'rgbm'; Helix (cont.) for i = 4 : 4 : length(pts)-4 plot3([x1(i) x2(i)], [y1(i) y2(i)], z([i i]),... colors(ceil(rand(1)*length(colors))), 'LineWidth', 3) axis off set(gcf, 'Color', 'k') set(gca, 'CameraViewAngleMode', 'manual') az = -180 ; while true view(az, 9), pause(0.05) az = az + 5 ; 25 sphere Surface Objects [x,y,z] = sphere(20); x(1 : 5 : 21*21) = NaN; surf(x, y, z) alpha(0.7) Use the rotate tool to rotate the sphere; set Fixed Aspect Ratio Axes first. surf(x, y, z, rand(size(x))) shading interp, grid off, axis off set(gcf, 'Color', 'w') 28 Color Maps Data From Files clf reset, peaks, colorbar m = colormap; whos m colormap(spring) brighten(0.5) colormap(jet) colormap(bone) colormap(hot) 26 Create a file temps.txt: Use the New Script button. Enter this data: 38 50 42 53 33 57 45 56 44 46 41 40 Save the file as temps.txt load temps.txt plot(temps) 29 2D Data Importing Data From Files [x, y] = meshgrid(-2 : 0.05 : 2) ; z = sin(x).* cos(y); contour(z, 20) imagesc(z) colormap(hot) imagesc(x(:), y(:), z) surf(z), colormap(jet) surfc(z) You can import data from Excel (and many other file formats) using the Import Data button. Select the file you want to import; the wizard will guide you through the rest. There are also built-in functions specifically for dealing with Excel files: doc xlsread doc xlswrite 27 30

Curve Fitting for Extrapolation x = randn(1, 2000); y = sin(x) + 0.2 * randn(1, 2000) ; clf, hold on, plot(x, y, '.') c = polyfit(x, y, 3) Example polynomial representation: c = [ 5-1 4 3] 5x 3 x 2 + 4x + 3 pts = min(x) : range(x)/100 : max(x); plot(pts, polyval(c, pts), 'r', 'LineWidth', 3) 31 Debugging Poor man's debugger: Remove semicolons from assignments. Add 'quoted strings' in appropriate places. Add a call to keyboard. (Use return to return from keyboard input mode.) function y = buggy(vec) p = vec > 5 'got this far' keyboard z = p * vec y = sin(z) ; Try: buggy([4 6]) Type 'return' to exit keboard mode and continue. 34 clear all a = 'aardvark' [x, y, z] = sphere(5); save stuff.mat clear all whos -file stuff.mat load stuff.mat Saving Variables The Matlab Debugger dbtype helix dbstop helix 5 helix dbstep dbstep 7 whos Look at the Stack pulldown menu in the toolbar. dbstep 30 save junk.dat x y -ascii type junk.dat 32 dbquit dbclear helix doc debug 35 General Operating System Stuff Formatted Output pwd cd dir ls *.m for i = 1 : 10 fprintf('the square root of %2d is %f \n',... i, sqrt(i)) delete stuff.mat!ps -a doc fprintf title(sprintf('f(x) over range %g to %g',... -3.5, 5.125)) 33 36