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

Similar documents
Introduction to MATLAB

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

CDA5530: Performance Models of Computers and Networks. Chapter 8: Using Matlab for Performance Analysis and Simulation

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

Introduction to MatLab. Introduction to MatLab K. Craig 1

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

Lecture 2: Variables, Vectors and Matrices in MATLAB

Summer 2009 REU: Introduction to Matlab

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

Classes 7-8 (4 hours). Graphics in Matlab.

Introduction to Matlab

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial Vb

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50

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

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

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

AMS 27L LAB #2 Winter 2009

MATLAB Lecture 1. Introduction to MATLAB

Introduction to MATLAB

CS129: Introduction to Matlab (Code)

A Tour of Matlab for Math 496, Section 6

Computational Foundations of Cognitive Science

APPM 2360 Lab #2: Facial Recognition

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

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

LabVIEW MathScript Quick Reference

MATLAB Premier. Asst. Prof. Dr. Melik DÖLEN. Middle East Technical University Department of Mechanical Engineering 10/30/04 ME 304 1

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

Identity Matrix: >> eye(3) ans = Matrix of Ones: >> ones(2,3) ans =

Dr Richard Greenaway

Matlab and Octave: Quick Introduction and Examples 1 Basics

MATLAB BEGINNER S GUIDE

1.1 ABOUT MATLAB and MATLAB GUI (Graphical User Interface)

Grace days can not be used for this assignment

Introduction to MATLAB

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

CS1114 Assignment 5, Part 1

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

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

PART 1 PROGRAMMING WITH MATHLAB

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

3D Geometry and Camera Calibration

Mechanical Engineering Department Second Year (2015)

Table of Contents. Basis CEMTool 7 Tutorial

BSM510 Numerical Analysis

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as

Computational Photonics, Seminar 01 on Introduction into MATLAB, Page 1

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Introduction to Matlab

Introduction to Matlab

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

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.

Lab # 2 - ACS I Part I - DATA COMPRESSION in IMAGE PROCESSING using SVD

Basics. Bilkent University. CS554 Computer Vision Pinar Duygulu

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

Graphics Example a final product:

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

1. Register an account on: using your Oxford address

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

How to learn MATLAB? Some predefined variables

1 Introduction to Matlab

Introduction to Matlab

Introduction To MATLAB Introduction to Programming GENG 200

Teaching Manual Math 2131

Introduction to MATLAB

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Basic Graphs. Dmitry Adamskiy 16 November 2011

Matrix Inverse 2 ( 2) 1 = 2 1 2

Contents. Implementing the QR factorization The algebraic eigenvalue problem. Applied Linear Algebra in Geoscience Using MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

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

A very brief Matlab introduction

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as

x = 12 x = 12 1x = 16

Programming in Mathematics. Mili I. Shah

1 Week 1: Basics of scientific programming I

A Guide to Using Some Basic MATLAB Functions

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

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

EE 301 Signals & Systems I MATLAB Tutorial with Questions

GRAPHICS AND VISUALISATION WITH MATLAB

Introduction to GNU-Octave

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

ENGR Fall Exam 1

MATLAB and Numerical Analysis

MATLAB Vocabulary. Gerald Recktenwald. Version 0.965, 25 February 2017

Computational Finance

Computer Programming in MATLAB

TUTORIAL 1 Introduction to Matrix Calculation using MATLAB TUTORIAL 1 INTRODUCTION TO MATRIX CALCULATION USING MATLAB

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

2 Second Derivatives. As we have seen, a function f (x, y) of two variables has four different partial derivatives: f xx. f yx. f x y.

Signals and Systems Profs. Byron Yu and Pulkit Grover Fall Homework 1

ARRAY VARIABLES (ROW VECTORS)

Finite Math - J-term Homework. Section Inverse of a Square Matrix

Matlab Tutorial, CDS

Getting Started with MATLAB

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

MATLAB TUTORIAL FOR MATH/CHEG 305

Transcription:

Computational Foundations of Cognitive Science Lecture 14: s and in Matlab; Plotting and Graphics Frank Keller School of Informatics University of Edinburgh keller@inf.ed.ac.uk February 23, 21 1 2 3 Reading: McMahon, Ch. 3 Frank Keller Computational Foundations of Cognitive Science 1 Frank Keller Computational Foundations of Cognitive Science 2 The command inv(a) computes the inverse of A. Matlab complains if the matrix is singular: > A = [1 2 3; 2 5 3; 1 8]; B = [1 6 4; 2 4-1; -1 2 5]; > disp(inv(a)); -4 9 13-5 -3 5-2 -1 > disp(inv(b)); warning: inverse: matrix singular to machine precision We can test the property AA 1 = I : > disp(inv(a) * A); 1 1 1 Frank Keller Computational Foundations of Cognitive Science 3 We can test a few more properties of the inverse, such as (AB) 1 = B 1 A 1 and (A T ) 1 = (A 1 ) T : > format rat; > B = [1 6 4; 2 4 1; 1 2 5]; > disp(inv(a * B)); 88/3-29/18-119/18-34/3 3/36 91/36-1/3 1/9 1/9 > disp(inv(b) * inv(a)); 88/3-29/18-119/18-34/3 3/36 91/36-1/3 1/9 1/9 > disp(inv(a )); -4 13 5-5 -2 9-3 -1 Frank Keller Computational Foundations of Cognitive Science 4

The command det(a) computes the determinant of A: > A = [1 2 3; 2 5 3; 1 8]; B = [1 6 4; 2 4-1; -1 2 5]; > disp(det(a)); -1 > disp(det(b)); Recall that det(b) = indicates that B is singular (not invertible). To compute the inverse based on the determinant: > A = [1 2; 2 5]; disp(inv(a)); 5-2 -2 1 > Ai = 1/det(A) * [A(2, 2) -A(2, 1); -A(1, 2) A(1, 1)]; > disp(ai); 5-2 -2 1 Frank Keller Computational Foundations of Cognitive Science 5 Use eig(a) to obtain the eigenvalues of A: > A = [1 3; 4 2]; > disp(eig(a)); -2 5 Let s check this against the characteristic equation of A: > disp(-2 * eye(2) - A); -3-3 -4-4 > disp(det(-2 * eye(2) - A)); Recall that the determinant of the characteristic equation of A has to be zero. Frank Keller Computational Foundations of Cognitive Science 6 [X, L] = eig(a) returns a matrix X that contains the eigenvectors, and a matrix L that contains the eigenvalues of A: > [X, L] = eig(a); > disp(x); -.771 -.6.771 -.8 > disp(l); -2 5 Note that Matlab scales the eigenvectors so that the norm of each vector is one. To avoid that, use the nobalance option: > [X, L] = eig(a, nobalance ); disp(x); -1. -.75 1. -1. Frank Keller Computational Foundations of Cognitive Science 7 Let s check if these vectors are really eigenvectors. They have to have the property Ax = λx: > disp(a * X(:,1)); 2-2 > disp(l(1,1) * X(:,1)); 2-2 Note [ ] that the [ eigenvectors for A actually involve a scaling factor: t 3 and 4 t ], but Matlab instantiates t. t t Frank Keller Computational Foundations of Cognitive Science 8

Matlab s eigshow is a good way of getting an intuition for how eigenvectors work: For a matrix A, assume that X is a matrix that contains the eigenvectors of A, and Λ is a matrix containing the eigenvalues of A on the diagonal. Use Matlab to show that: A = X ΛX 1 What is this decomposition useful for? Frank Keller Computational Foundations of Cognitive Science 9 Frank Keller Computational Foundations of Cognitive Science 1 The plot(x, y) command in Matlab plots two vectors x and y against each other, with x representing the values on the x-axis and y representing the corresponding values on the y-axis. The x-values can be generated with x = [start:interval:end], which generates a vector with values ranging from start to end, spaced using interval. We can then apply a function to x and call plot: 1.8.6.4.2 > x = [:.1:1]; > disp(x);..1.2.3... 1. > y = cos(x); > disp(y); 1..99.98.95... -.83 > plot(x, y);.2.4.6.8 1 1 2 3 4 5 6 7 8 9 1 Frank Keller Computational Foundations of Cognitive Science 11 Frank Keller Computational Foundations of Cognitive Science 12

The command plot(x, y) plots the content of arbitrary vectors. Functions can also be plotted using fplot(function string, [start end]). This automatically chooses an optimal interval. Compare: > x = [:.25:4]; > y = exp(-x).* sin(x); > plot(x, y); with: > fplot( exp(-x) * sin(x), [, 4]);.35.3.25.2.15.1.5.5.5 1 1.5 2 2.5 3 3.5 4 Frank Keller Computational Foundations of Cognitive Science 13 Frank Keller Computational Foundations of Cognitive Science 14 Plotting Options A range of options can be appended to plot or fplot: exponential dampening xlabel label of x-axis ylabel label of y-axis title graph title string legend graph legend string grid switch grid on or off axis axis spacing can be square or equal or [xmin xmax ymin ymax] Example: > fplot( exp(-x) * sin(x), [, 4]), xlabel( x ), ylabel( f(x) ), title( exponential dampening ), grid on, axis equal; f(x) 1.5 1.5.5 1.5 1 1.5 2 2.5 3 3.5 4 x Frank Keller Computational Foundations of Cognitive Science 15 Frank Keller Computational Foundations of Cognitive Science

Plotting Options We can plot more than one function in the same graph, simply by giving plot multiple arguments: > x = [ :.1 : 5]; > y = exp(-x); > z = exp(-2*x); > plot(x, y, --, x, z, : ), legend ( y, z ), axis([ 2 1]); Here, the third argument specifies the line type: - for straight line, -- for dashed line line, : for dotted line. Note also the use of the legend option to introduce a legend, and the axis option to specify axis spacing. 1.9.8.7.6.5.4.3.2.1.2.4.6.8 1 1.2 1.4 1.6 1.8 2 y z Frank Keller Computational Foundations of Cognitive Science 17 Frank Keller Computational Foundations of Cognitive Science 18 The plot command can be used to plot discrete data as well, but Matlab also offers a number of special graph types for this. Assume we have the following probability distribution (probability P(x) of obtaining x head when tossing a coin four times): x 1 2 3 5 P(x) 1 4 6 4 1 Plot this distribution as a bar chart: > x = [ : 4]; > y = [1/ 4/ 6/ 4/ 1/]; > bar(x, y), xlabel( x ), ylabel( P(x) ); P(x).4.35.3.25.2.15.1.5 1 2 3 4 x Frank Keller Computational Foundations of Cognitive Science 19 Frank Keller Computational Foundations of Cognitive Science 2

Plot the same data as a stem plot or as a scatter plot: > stem(x, y), xlabel( x ), ylabel( P(x) ), axis([-.5 4.5.4]); > plot(x, y, o ), xlabel( x ), ylabel( P(x) ), axis([-.5 4.5.4]); We can superimpose multiple graphs by saying hold. For example, we can use this connect the dots in the scatter plot: > plot(x, y, o ), xlabel( x ), ylabel( P(x) ); > hold; > plot(x, y); P(x).4.35.3.25.2.15.1.5.5.5 1 1.5 2 2.5 3 3.5 4 4.5 x Frank Keller Computational Foundations of Cognitive Science 21 Frank Keller Computational Foundations of Cognitive Science 22 P(x).4.35.3.25.2.15.1.5.5.5 1 1.5 2 2.5 3 3.5 4 4.5 x In Matlab, images can be processed as matrices. For example, a greyscale image of size 2 2 pixels is a matrix of integers ranging from (black) to 255 (white). Images can be read from a file using imread, saved to a file using imwrite, and displayed using imshow. > A = imread( baboon_grey.jpg ); > A = double(a); > imshow(uint8(2 * A)); > imshow(uint8(a )); > imwrite( baboon_rotated.jpg, uint8(a )); Note that we need to convert the image matrix to format double for matrix operations (such as transpose). For input and output, the matrix need to be in format uint8. Frank Keller Computational Foundations of Cognitive Science 23 Frank Keller Computational Foundations of Cognitive Science 24

Images Processing A = 2*A = We can also multiply an image matrix with a matrix we have generated using Matlab: > B = [eye(1) eye(1); eye(1) eye(1)]; > B = B - eye(2); > imshow(uint8(255 * B)); > imshow(uint8(a * B)); We can convolute an image with a kernel using the conv2 command (see next lecture for details): > K = [1/9 1/9 1/9; 1/9 1/9 1/9; 1/9 1/9 1/9]; > C = conv2(k, A); > imshow(uint8(c)); > K = [1-1; 2-2; 1-1]; > D = conv2(k, A); > imshow(uint8(d)); Frank Keller Computational Foundations of Cognitive Science 25 Frank Keller Computational Foundations of Cognitive Science 26 Images Processing Example: Image Processing B = A*B = C = D = Frank Keller Computational Foundations of Cognitive Science 27 Frank Keller Computational Foundations of Cognitive Science 28

Summary : inv(a); determinant: det(a); eigenvalues and eigenvectors: eig(a); plotting functions: plot, fplot; plotting discrete data: bar, stem; processing images: imread, imwrite, imshow; convolution: conv2. Frank Keller Computational Foundations of Cognitive Science 29