Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.

Slides:



Advertisements
Similar presentations
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Advertisements

Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
1 Pendahuluan Pertemuan 5 Matakuliah: H0062/Teori Sistem Tahun: 2006.
Introduction to MATLAB Simon O’Keefe Non-Standard Computation Group
Eilon Sharon, Weizmann 2008 © 1 Introduction to Matlab & Data Analysis Tutorial 2: Variables and Matrix Manipulation.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
Matrix Mathematics in MATLAB and Excel
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matlab intro The Environment
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Chapter 10 Review: Matrix Algebra
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Introduction to MATLAB
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
Intro to Matlab 1.Using scalar variables 2.Vectors, matrices, and arithmetic 3.Plotting 4.Solving Systems of Equations Can be found at:
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.
Basics of MATLAB By DR. Wafaa Shabana
Introduction to MATLAB
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
OUTLINE Overview Numbers, variables and similar in Matlab
Introduction to MATLAB Session 4 Simopekka Vänskä, THL 2010.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to MATLAB Session 2 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2010.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Intro to Matlab Yipei Wang & Qihui Li {yipeiw,
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB,
Lecture: MATLAB Chapter 1 Introduction
Introduction to MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Matlab tutorial course
Matlab Intro.
INTRODUCTION TO MATLAB
Matlab Training Session 2: Matrix Operations and Relational Operators
Matlab Intro.
Presentation transcript:

Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010

About this course

Introduction to MATLAB - Session 1 Focus of this course...in learning to use MATLAB software  Not in numerical methods (but some examples) …to get some idea of the possibilities of MATLAB  We will not go through all properties of MATLAB.  To get into a position for learning application specific features.

Introduction to MATLAB - Session 1 Schedule and passing Schedule Five 3-hour sessions Homework Session working style Introduction to the topic + exercises, learning by doing Homework Estimated work load 5-10 hours More information later Passing the course (2 credits, passed/failed) Attendance min 3/5 sessions + passing the homework

Introduction to MATLAB - Session 1 Contents of this course Session 2 Some matrix commands Logical expressions Graphics 1 Session 3 My functions + strings, cells Controlling program flow Session 4 Function functions Session 5 Graphics 2 More linear algebra Starting homework Session 1 Genaral Matrices M-files

MATLAB

Introduction to MATLAB - Session 1 What is MATLAB? Matrix Labratory  Array/Matrix is the basic data element Environment for numerical computing >> exp(sqrt(x.^2+1)), 0, 1) ans =  not for symbolic calculus Library of mathematical functions Programming language Application-specific toolboxes

Introduction to MATLAB - Session 1 MATLAB desktop view

Introduction to MATLAB - Session 1 Getting started Basic idea Workspace  Matrices and other data elements are storaged in the workspace  Commands who and whos for listing the variables Execute commands (functions) to manipulate the matrices in the workspace  Matlab interpretes the commands, no compiler 1. Start MATLAB 2. Create a working directory  Under your personal home directory 3. Set ”Current directory” to your working directory 4. Create a variable v by typing >> v = 5 in a command window and try who and whos. 5. Write >> exp(v) and check the workspace.

MATRICES

Introduction to MATLAB - Session 1 Matrices (and vectors and scalars) Matrix: the basic data element (n,m) array Vector (n,1) matrix = column vector (1,m) matrix = raw vector Scalar (1,1) matrix Two ways to create matrices: 1. List the elements  = is the substitution  Use [ ] brackets 2. Built-in functions (Load from a file) Try the following: >> A = [1 2 3; 5,6,7] >> [1 2 3; 5,6,7] >> B = [1 2 3; 5,6,7]; >> B >> C=[A; B] >> D = ones(2,4) >> E = zeros(3) >> E2 = zeros(1000) >> F = zeros(1,4) >> G = rand(10,1) >> H = randn(10,1) >> I = eye(5) >> J = [ ]

Introduction to MATLAB - Session 1 More matrices Creating vectors with :, the colon command  a:b from a to b with step one  a:d:b from a to b with step d Multi-dimensional matrices Some scalars  i and j : complex number  eps : small number  pi :    Inf, NaN Hint: Use ; for not showing the result on the command window Hint: Command size(A) returns the size of matrix A Try the following >> 1:4 >> 0:5:20 >> v = 20:-5:0 >> rand(2,3,4) >> i >> 5+3*i >> i = 5 >> 5+3*i >> eps >> pi >> 5e3 >> 5.4e-3 >> beta >> help beta

Introduction to MATLAB - Session 1 Matrix arithmetics A+B, A-B A+c, A-c Matrix multiplication A*B Transpose A.’ and adjoint A’ Matrix power A^c  For noninteger c with spectral calculus \ left matrix divide  Ax=y  x=A\y = inv(A)*y / right matrix divide Array arithmetics A+B, A-B A+c, A-c Array multiplication A.*B Array power A.^c, A.^B Array divide A./B A, B matrices, c scalar – matrix sizes must match !

Introduction to MATLAB - Session 1 Reffering to vector elements Let v = [v1 v2... vn]. Refer to element vj with v(j) v(J)  J can be a matrix of indeces  v(J) is a matrix of size(J) and of elements defined by J Hint: Command length(v) returns the length of vector v Try the following: >> v = 0:5:30 >> v([1 1 3]) >> v(2) = 15 >> v(2:4) = 3:5 >> J = [1 1; 2 2]; >> v(J) >> v(10) >> v(10)=100;

Introduction to MATLAB - Session 1 Reffering to matrix elements Some properties Refer to entire column/raw with the colon :  A(:,5)  A(2,:) A(:) is the matrix A as a vector Referring with end –command  A(3,5:end)  A(2:end, : ) Let A = [a11 a12... a1m a21 a22... a2m  an1 an2... anm]. Refer to element ajk with  A(j,k) or  A(n*(k-1)+j) Consider matrix as a column vector (columns consecutively) A(J,K) with index matrices J,K

Introduction to MATLAB - Session 1 …Reffering to matrix elements Try the following >> A = [1:4;5:8;9:12] >> A(2,4) >> A(11) >> A(2,1:3) >> A(2,:) >> A([2 3],[3 1]) >> A(2:end,3) >> A(5:end) >> reshape(A,4,3) >> help reshape

M-FILES

Introduction to MATLAB - Session 1 MATLAB m-files Not practical to write all commands in the command window  use m-files  Text files of type *.m  For example, test1.m  Each line of an m-file is a MATLAB command line MATLAB executes the lines of an m-file by writing the name of the file in the command window, (or F5 from the editor)  >> test1  Visibility: m-file has to be in the MATLAB’s current directory (or in the MATLAB root) Can be written with any text editor  …but the MATLAB editor is preferable  File  New  m-file (blank)

Problems Session 1

Introduction to MATLAB - Session 1 Problems 1. Go throw the previous ”Try the following” exercises 2. Are the following vectors the same?  a = [1 -2 3]  b = [ ]  c = [1-2 3] 3.How much memory does the matrix I=ones(1000) need? How about I=ones(10000)? Clear memory with ”clear” command. 4.Set a=1 and b=i. Check the memory usage (whos). 5.Compute a) log(-1), b) sqrt(-1),c) 1/0. 6.See help format, and try: >> format long>> format short>> pi

Introduction to MATLAB - Session 1 Problems Matrix manipulation – write your answers to m-files 7.Create (5,5) –matrix of zeros.  Substitute random numbers (with rand –command) to raws 3-5.  Delete the first and the last columns (=substitute empty matrix).  Create a vector [ … 0 1] of length 20.  Create a vector [1,3,5,...,999,2,4,6,...,1000].  Create a vector [2,1,4,3,6,5,...,998,997,1000,999]. 8.Create a (100,100) -matrix … 100 … … 100 Hint: Multiplication with matrix ones(100,1).

Introduction to MATLAB - Session 1 Problems 9.Let A = [1 2; 0 3];  Compute A*ej for e1 = [1;0] and e2=[0;1];  Compute A^2 and A.^2.  Solve equation Ax=y for a) y = [1;0];b) y= [2;3];c) y = [ i ; 0]; 10.Create a ( :,1000) matrix a)X2 whose columns are R 2 rand vectors in the unit square, b)X3 whose columns are R 3 rand vectors in the unit cube.  Compute the lengths of the random vectors.  Calculate the average length of the random vectors. 11.Let p be a vector of annual interest rates% and let s be a vector of initial investments. Create a table A for the values of the investments after 10 years, A(j,k) = s(k)*(1+p(j)/100) 10. You can use e.g., p=.5:.5:5 and s = 2000:2000:10000.

Introduction to MATLAB - Session 1 Problems 12.Study sparse matrices from the MATLAB help >> help sparse 13. Create a sparse matrix S of size 2000x2000 with all diagonal elements 1, S(j,j)=1, and random lower diagonal S(j+1,j) = random number, j=1,…,1999.  Check that S is correct with full(S(1:10,1:10)).  Set y = ones(2000,1); and S2=full(S);  Compare with tic toc –commands (see help tic) the speed of solving Sx=y by  tic; x=S\y; toc  tic; x=S2\y; toc  tic; x=inv(S)*y; toc (see help inv)  Can you explain the result?

>> quit …to exit MATLAB.