1 Introduction to Matlab-2 Laboratoire Mathématiques, Informatique et Applications.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introdução ao M ATLAB M ATLAB Basico
Introduction to Matlab EE 2303 Lab. MATLAB stands for “Matrix Laboratory” APPLICATIONS OF MATLAB:  Mathematical Calculations  Data Analysis & Visualization.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Introduction to Matlab Οικονομίδης Δημήτρης
Introduction to Matlab Jianguo Wang CSSCR September 2009.
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.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
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.
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.
Introduction to MATLAB Zongqiang Liao Research Computing Group UNC-Chapel Hill.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Eng Advanced Marine Vehicles Todays agenda: Lab tomorrow at 2pm (structures lab) ‏ Advanced Marine Party Introduction to Matlab.
Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
Yasser F. O. Mohammad Approximations and Round-off Errors.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Eng Ship Structures 1 Introduction to Matlab.
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.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Master in Optical Fiber Communications and Photonic Technologies Foundations of Digital Transmission - Fall quarter Introduction to Matlab.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
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.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Introduction to Matlab
Introduction to MATLAB Zongqiang Liao Research Computing Group UNC-Chapel Hill.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
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.
전자장 1 실험 - Matlab 사용법 - Photonic Systems Laboratory School of EE, Seoul National University Photonic Systems Lab School of EECS, S.N.U.
1 Introduction to Matlab. 2 What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for.
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Computer Application in Engineering Design
Introduction to Mat lab
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
INTRODUCTION TO MATLAB
Presentation transcript:

1 Introduction to Matlab-2 Laboratoire Mathématiques, Informatique et Applications

2 Desktop Tools (Matlab v6)  Command Window –type commands  Workspace –view program variables –clear to clear –double click on a variable to see it in the Array Editor  Command History –view past commands –save a whole session using diary  Launch Pad –access tools, demos and documentation

3 Matlab Files (.m)  Use predefined functions or write your own functions  Reside on the current directory or the search path –add with File/Set Path  Use the Editor/Debugger to edit, run

4 Matrices  a vector x = [ ] x = a matrix x = [1 2 3; 5 1 4; ] x = transpose y = x ’ y =

5 Matrices  x(i,j) subscription  whole row  whole column y=x(2,3) y = 4 y=x(3,:) y = y=x(:,2) y = 2 1 2

6 Operators (arithmetic) +addition -subtraction *multiplication /division ^power ‘complex conjugate transpose.*element-by-element mult./element-by-element div.^element-by-element power.‘transpose

7 Operators (relational, logical) ==equal ~=not equal <less than <=less than or equal >greater than >=greater than or equal &AND |OR ~NOT pi … jimaginary unit, isame as j

8 Generating Vectors from functions  zeros(M,N)MxN matrix of zeros  ones(M,N)MxN matrix of ones  rand(M,N)MxN matrix of uniformly distributed random numbers on (0,1) x = zeros(1,3) x = x = ones(1,3) x = x = rand(1,3) x =

9 Operators [ ]concatenation ( )subscription x = [ zeros(1,3) ones(1,2) ] x = x = [ ] x = y = x(2) y = 3 y = x(2:4) y = 3 5 7

10 Matlab Graphics x = 0:pi/100:2*pi; y = sin(x); plot(x,y); xlabel('x = 0:2\pi'); ylabel('Sine of x'); title('Plot of the Sine Function');

11 Multiple Graphs t =0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); plot(t,y1,t,y2); grid on;

12 Multiple Plots t =0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); subplot(2,2,1); plot(t,y1); subplot(2,2,2); plot(t,y2);

13 If Statements IF expression statements ELSEIF expression statements ELSE statements END

14  Example clear all; close all; n=10; x=1:n; y=zeros([1,n]); for k=1:n y(k) = 2*k; end plot(x,y);

15 Homework  Generate a tree structure for the system equation y=sin(ax) + bx knowing that x is the system input and y is the system output. X and y are arrays of n values. Assume n = 100;  Generate a random sequence from -2 to 2 to be used as the system input x using the randn command.  Plot the input versus the output of the above system.

16 Solution clear all; close all; x=2-4*rand(1,100); a=2; b=3; y=sin(a*x) + b*x; subplot(2,1,1); plot(x); grid; title('system input'); subplot(2,1,2); plot(y,'r'); grid; title('system output');

17

18 Graph Functions (summary)  plotlinear plot  gridadd grid lines  xlabeladd X-axis label  ylabel add Y-axis label  titleadd graph title  subplotdivide figure window  figurecreate new figure window  pausewait for user response

19 Math Functions  Elementary functions (sin, cos, sqrt, abs, exp, log10, round) –type help elfun  Advanced functions (bessel, beta, gamma, erf) –type help specfun –type help elmat

20 Scripts  Matlab editor  Use scripts to execute a series of Matlab commands Matlab Desktop Press to create new m-file in the matlab editor

21 Functions function f=myfunction(x,y) f=x+y;  save it in myfunction.m  call it with y=myfunction(x,y)

22 Functions  Programming in Matlab.  Users can write functions which can be called from the command line.  Functions can accept input variable(s)/matrice(s) and will output variable(s)/matrice(s).  Functions will not manipulate variable(s)/matrice(s) in the Matlab Workspace.  In Matlab functions closely resemble scripts and can be written in the Matlab editor. Matlab functions have the function keyword.  Remember that the filename of a function will be its calling function name.  Don’t overload any built-in functions by using the same filename for your functions or scripts!  Functions can be opened for editing using the open command. Many built-in Matlab functions can also be viewed using this command.

23 >> I=iterate(5) I = Functions (continued) output input function name for statement block function keyword help lines for function Access the comments of your Matlab functions >> help iterate Make sure you save changes to the m-file before you call the function!

24 >> [i j]=sort2(2,4) i = 4 j = 2 >> Functions (continued) Functions can have many outputs contained in a matrix Remember to use the Matlab help command for syntax >> help if if statement block

25 More flow control Method is linear >> i = 4 i = 16 i = 256 While statement blockSwitch statement block Without ; to print output

26 Debugging  Set breakpoints to stop the execution of code >> [i j]=sort2(2,4) K>> K>> whos Name Size Bytes Class a 1x1 8 double array b 1x1 8 double array Grand total is 2 elements using 16 bytes K>> a a = 2 K>> return i = 4 j = 2 Click mouse on the left of the line of code to create a breakpoint local function workspace exit debug mode Debug menus

27 Visualisation - plotting data >> figure % create new figure >> t=0:pi/12:8*pi; >> y=cos(t); >> plot(t,y, ‘ b.-') Investigate the function >> y=A*cos(w*t+phi); for different values of phi (eg: 0, pi/4, pi/3, pi/2), w (eg: 1, 2, 3, 4) and A (eg: 1, 0.5, 2). Use the hold on Matlab command to display your plots in the same figure. Remember to type hold off to go back to normal plotting mode. Try using different plot styles (help plot) A = amplitude phi = phase w = angular frequency = 2*pi*frequency Plot style

28 Flow Control A=3; B=2; if A > B 'greater' elseif A < B 'less' else 'equal' end for x = 1:10 r(x) = x; end if statement switch statement for loops while loops continue statement break statement

29 Miscellaneous  Loading data from a file –load myfile.dat  Definition – x = [ ];

30 Getting Help  Using the Help Browser (.html,.pdf) –View getstart.pdf, graphg.pdf, using_ml.pdf  Type –help –help function, e.g. help plot  Running demos –type demos –type help demos

31 >> help stem STEM Discrete sequence or "stem" plot. STEM(Y) plots the data sequence Y as stems from the x axisterminated with circles for the data value. STEM(X,Y) plots the data sequence Y at the values specified in X. STEM(...,'filled') produces a stem plot with filled markers. STEM(...,'LINESPEC') uses the linetype specified for the stems and markers. See PLOT for possibilities. H = STEM(...) returns a vector of line handles. See also PLOT, BAR, STAIRS.

32 Random Numbers x=rand(100,1); stem(x); hist(x,100)

33 Coin Tosses  Simulate the outcomes of 100 fair coin tosses x=rand(100,1); p=sum(x<0.5)/100 p =  Simulate the outcomes of 1000 fair coin tosses x=rand(1000,1); p=sum(x<0.5)/1000 p =

34 Coin Tosses  Simulate the outcomes of 1000 biased coin tosses with p[Head]=0.4 x=rand(1000,1); p=sum(x<0.4)/1000 p =

35 Sum of Two Dies  Simulate observations of the sum of two fair dies

36 Sum of Two Dies for i=2:12 z(i)=sum(y==i)/10000 end bar(z)

Boolean Operations » x=[0 1 1]; y=[1 1 1]; » and(x,y) » ans = » » or(x,y) » ans = » » x=[0 1 1]; y=[1 1 1]; » and(x,y) » ans = » » or(x,y) » ans = » = TRUE 0 = FALSE »bool_ops

Solving Simultaneous Equations using “Left Division”  If [A] is square matrix (m = n):  For overdetermined system (m>n): using Least squares regression “curve fit” of data Warning if rank deficient (dependent columns) - solution not unique  For undetermined system (m<n): using QR factorization with column pivoting Never unique [A]{x} = {b} {x} = ? {x} = [A] -1 {b} »x = inv(A)*b; »x = A\b; »x = inv(A)*b; »x = A\b; Error if singular Warning if nearly singular [A] = mxn {x} = nx1 {b} = mx1 »x = A\b;

 Solve this set of simultaneous equations: Example: Solving Equations »A = [-1 1 2; ;-1 3 4]; »b = [2;6;4]; »x = inv(A)*b x = »x = A\b x = »A = [-1 1 2; ;-1 3 4]; »b = [2;6;4]; »x = inv(A)*b x = »x = A\b x = x 1 + x 2 + 2x 3 = 2 3x 1 - x 2 + x 3 = 6 -x 1 + 3x 2 + 4x 3 = 4

40 Return to our example clear all; close all; n=20; x=rand(1,n); x1= [0 x(1:n-1)]; x2= [0 0 x(1:n-2)]; a=2; b=5; y= a*x1 + b*x2; figure; plot(y,'r'); grid; title('system output');

String Arrays  Created using single quote delimiter (')  Each character is a separate matrix element (16 bits of memory per character)  Indexing same as for numeric arrays »str = 'Hi there,' str = Hi there, »str2 = 'Isn''t MATLAB great?' str2 = Isn't MATLAB great? »str = 'Hi there,' str = Hi there, »str2 = 'Isn''t MATLAB great?' str2 = Isn't MATLAB great? 1x9 vector str = Hithere,

»str ='Hi there,'; »str1='Everyone!'; »new_str=[str, ' ', str1] new_str = Hi there, Everyone! »str2 = 'Isn''t MATLAB great?'; »new_str2=[new_str; str2] new_str2 = Hi there, Everyone! Isn't MATLAB great? »str ='Hi there,'; »str1='Everyone!'; »new_str=[str, ' ', str1] new_str = Hi there, Everyone! »str2 = 'Isn''t MATLAB great?'; »new_str2=[new_str; str2] new_str2 = Hi there, Everyone! Isn't MATLAB great? 1x19 vector 1x9 vectors String Array Concatenation Using [ ] operator: Each row must be same length Row separator: semicolon (;) Column separator: space / comma (,) For strings of different length: STRVCAT STR2MAT »new_str3 = strvcat(str, str2) new_str3 = Hi there, Isn't MATLAB great? »new_str3 = strvcat(str, str2) new_str3 = Hi there, Isn't MATLAB great? 2x19 matrix (zero padded) 1x19 vectors

Working with String Arrays String Comparisons – STRCMP - compare whole strings – STRNCMP - compare first ‘N’ characters – FINDSTR - finds substring within a larger string Converting between numeric & string arrays: – NUM2STR - convert from numeric to string array – STR2NUM - convert from string to numeric array

44 End of Lecture