Introduction to MATLAB Session 2 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2010.

Slides:



Advertisements
Similar presentations
Lecture 5.
Advertisements

Problems and solutions Session 3. Introduction to MATLAB - Solutions 3 Problems 1. Write function Xn = mspolygon(X,x0,a) that scales the INPUT polygon.
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Introduction to Matlab
Matlab Graphics S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: 2D Graphics.
Excel Part I Basics and Simple Plotting Section 008 Fall 2013 EGR 105 Foundations of Engineering I.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
MATLAB - Basics Centro de Informática Universidade Federal de Pernambuco Aprendizagem de Máquina – IN1102 Arley Ristar –
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
Exam #3 Review: Comprehensive Exam Class 14.2 Palm Matlab Book Ch. 1-5.
1 Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix.
MATLAB TUTORIAL Dmitry Drutskoy Some material borrowed from the departmental MATLAB info session by Philippe Rigollet Kevin Wayne.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
EGR 105 Foundations of Engineering I Session 3 Excel – Basics through Graphing Fall 2008.
Introduction to MATLAB
Matlab intro The Environment
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.
An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7.
Eigenvectors and Eigenvalues
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
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 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.
Hydroinformatics: Session4 Dr Ivan Stoianov Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
INTRODUCTION TO MATLAB LAB# 01
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
L – Modelling and Simulating Social Systems with MATLAB Lesson 2 – Statistics and plotting Anders Johansson and Wenjian Yu © ETH.
Recap Sum and Product Functions Matrix Size Function Variance and Standard Deviation Random Numbers Complex Numbers.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
Introduction to MATLAB Session 5 Simopekka Vänskä, THL 2010.
Introduction to MATLAB Session 4 Simopekka Vänskä, THL 2010.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Introduction to Matlab By E. Noura Semary. Contents MATLAB Environment  Command window, Workspace, Path window, Editor window,and Figure window) Basic.
Lec-10 Manipulations of Matlab Matrices. Built-in functions The chapter will be covered quickly There are quite a few built-in functions in MATLAB – If.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Introduction to Matlab
Problems and solutions Session 2. Introduction to MATLAB - Solutions 2 Problems Write your solutions to m-files 1. Check how matrix A = a) [2 0; b) [2.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Intro to Matlab Rogelio Long September 3, How to access MyDesktop Log in with your utep id and password.
Matlab Tutorial Iman Moazzen First Session – September 11, 2013.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
L – Modeling and Simulating Social Systems with MATLAB Lecture 2 – Statistics and plotting © ETH Zürich | Giovanni Luca Ciampaglia,
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
Jake Blanchard University of Wisconsin Spring 2006.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Matrix Mayhem: Outline
Computer Application in Engineering Design
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files) Double click this icon
Introduction to Matlab
Announcements P3 due today
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

Introduction to MATLAB Session 2 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2010

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

Basic commands

Introduction to MATLAB - Session 2 MATLAB commands/functions Genaral syntax: [OUTPUT parameters] = functionname(INPUT parameters); Functions do not change the INPUT parameters Number of parameters can variate Help function: >> help functionname Function INPUT parameters OUTPUT parameters

Introduction to MATLAB - Session 2 Some elementwise functions Operate on each element of the input matrix A: size(OUTPUT) = size(A) Examples >> A = [1 0; pi pi/2]; >> sin(A) ans = >> round(A) ans = Some common functions: sin, cos, tan, asin, acos, atan exp, log, sqrt abs, conj, imag, real, angle fix, floor, ceil, round, sign For more, see >> help elfun Try the following: >> asin(1) >> asin(5) >> exp(1) >> exp(709) >> exp(710) >> log(exp(50)) >> v = [-1.2, 0.5, 1.2]; >> round(v) >> fix(v) >> floor(v) >> ceil(v) >> sign(v) >> sign(0)

Introduction to MATLAB - Session 2 Some columnwise functions Operate on each column of the input matrix A: size(OUTPUT) = size(A,2)  Exception: raw vectors Example >> A = [1 0; pi pi/2]; >> sum(A) ans = Operating dimension choosable >> sum(A,2) ans = Examples sum, prod, cumsum, cumprod mean, median, std, min, max sort Try the following >> A = [1 3 3; 2 2 4] >> mean(A) >> mean(A’) >> min(A) >> [X,I] = min(A) >> cumsum(A) >> cumprod(A) >> sort(A)

Introduction to MATLAB - Session 2 Polynomials and interpolation Finding the roots of the polynomial: roots(C) returns the roots of the polynomial whose coefficients are given by vector C  P(x) = C(1)*X^N C(N)*X + C(N+1).  Recall: Polynomial of degree n has exactly n complex roots. Polynomial fitting: polyfit(x,y,n) fits a polynomial of degree n to the data points (X,Y) and returns the coefficients. Simple interpolation interp1 interp1(X,Y,X0) interpolates the data points (X,Y) to given interpolation points X0.  interp2, interp3 for 2- and 3-dimensional data.

Logical expressions

Introduction to MATLAB - Session 2 Logical expressions and functions Logical datatype  False: 0  True: 1 (nonzero) Relational operations: == equal ~= not equal >=, >, <=,< Logical operations: & (and), | (or), ~ (not), xor, any, all isempty, isnan Try the following >> v = 1:5; >> v==3 >> (v>=2)&(v<5) >> (v>=2).*(v<5) >> f = v==3 >> f = (v==3) >> whos f >> all(f) >> any(f) >> ~f >> g = (v>=2) >> xor(g,f) >> g|f

Introduction to MATLAB - Session 2 FIND find(X) returns the indeces of non-zero (true) entries of X >> I = find(X) returns the vector indeces >> [I,J] = find(X) returns the matrix indeces >> I = find(X,k) return the k first indeces Restricting vectors with find. Optional notations >> X(find(X<4)) and >> X(X<4) are the same. Try the following >> X = rand(4) >> I = find(X<0.4) >> [I,J] = find(X<0.4) >> Y = X(X<0.4) >> I = find(X<0.4) >> X(I)

Graphics 1 - PLOT

Introduction to MATLAB - Session 2 No. 1 plotting command - plot Basic idea: Command plot(x,y) connects the points (x(1),y(1)), (x(2),y(2)), …, (x(n),y(n)) with lines.  Into current figure and axis (if does not exist, creates). For matrices X and Y: plot(X,Y) acts columnwise (exceptions exist).

Introduction to MATLAB - Session 2 Plot line properties Basic line properties  Plot symbols:., o, x, +, *, …  Color: b, g, r, c,…  Line style: -, :, -., -- For more, see >> help plot How to use: >> plot(x,y,’bo’) Multiple data in one plot: >> plot(x1,y1,’bo’,x2,y2,’r’,…) OR use hold command: >> plot(x1,y1,’bo’) >> hold on >> plot(x2,y2,’r’) >> hold off Additional properties: Graphics 2 session. Remark:  Text within ’ ’ is a string  A string is a char array, a char valued matrix, e.g. >> J = ’the Lord’ Try the following >> x = (-3:.1:3)’; >> plot(x,exp(x)) >> c = 1:5; >> plot(x,x.^2*c) >> plot(x,x,’rx’,x,x+2,’b-’)

Introduction to MATLAB - Session 2 Basic plot editing commands Example >> plot(x,y,’r’,x0,y0,’bo’) Title for the image >> title(’Linear fit’) Labels for the axes >> xlabel(’Month’) >> ylabel(’Cases’) Setting the axis limits >> axis([ ]) Labeling of the plot lines >> legend(’fit’,’data’) Here, an additional property ’Location’ was used: >> legend(’fit’,’data’,’Location’,’NorthWest’) OR move with the mouse in the figure window.

Introduction to MATLAB - Session 2 Figure and subplot >> figure(n)  sets figure n as current figure  or, creates figure n if it does not exist Example: Numbering subplots. >> subplot(m,n,k)  Breaks the figure window to subfigures with m raws and n columns and sets current axis to axis number k For more, see >> help subplot Try the following: >> subplot(1,2,1) >> plot(rand(3)) >> subplot(2,2,2) >> plot(rand(10)) >> subplot(2,2,4) >> plot(0:.1:5,sqrt(0:.1:5))

Problems Session 2

Introduction to MATLAB - Session 2 Problems Write your solutions to m-files 1. Check how matrix A = a) [2 0; b) [2 0; c) [-1 0; d) [ 1 1; e) [1 -1; f) [2 1; 0 1]; 0 -1]; 0 1]; -1 1]; 1 1]; 0 2]; maps the points P = [0, 4, 4, 3, 3, 2.5, 2.5, 2, 0, 0; 0, 0, 3, 4, 5, 5, 4.5, 5, 3, 0]; by plotting P and points A*P. To plot P you can use plot(P(1,:),P(2,:)). 2. Plot functions y=sin(x) and y = cos(x) on interval [0,4  in the same figure but with different colors.

Introduction to MATLAB - Session 2 Problems 3. Draw the unit circle in R 2.  Draw the unit circle so that the line is green for x>0 and black for x<0. 4. Map the unit circle to the ellipse with major axes u = [2;1], minor axes v = [-1/2;1], and center (1,1). Draw the ellipse in the same picture with the unit circle.  Hint: Map linearly and transport. 5. Draw the image of the mapping f: 1 + [-i,i]  C, a)f(z) = log(z), b)f(z) = z^2, in the complex plane. Hint: Real plane.

Introduction to MATLAB - Session 2 Mortality fitting 6. In this exercise we consider mortality in Finland at 2007 (data loaded from Tilastokeskus website). Copy kuolleisuus.xls (at the wikipage of the course) to your working directory. Load it to MATLAB (start your m-file with M = xlsread(’kuolleisuus.xls’);). The file contains matrix M with  M(:,1) = age  M(j,2) = mortality for mails at age(j) [1/1000]  M(j,3) = mortality for f s at age(j) Fit polynomials of degree 2 and 3 to the mortality data. Fit an exponential function to the mortality data, i.e., fit a polynomial of degree 1 to the log(mortality) –data. Present your fit graphically. Use subplots, colors, titles, legends, and axis labels.

Introduction to MATLAB - Session 2 Computing area with random points 7. Compute the area of the unit triangle T = span((0,0),(1,0),(0,1)) with uniformly distributed random numbers as follows: Generate N uniformly distributed random points x =(x1,x2) in the unit square Find the fraction of the points falling in T. Illustrate this graphically, plot the random points and T. Plot the points in T and the points out T with different colors. Approximate area of T. Test the accuracy with different number of points N.

>> quit …to exit MATLAB.