1 M ATLAB Short Course Part (2). 2 Elementary Math Function abs, sign: Absolute value and Signum Function sin, cos, asin, acos…: Triangular functions.

Slides:



Advertisements
Similar presentations
Introduction To MATLAB Prof. Muhammad Saeed Mathematical Modeling and Simulation.
Advertisements

MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Lecture 5.
Lecture 5 Fixed point iteration Download fixedpoint.m From math.unm.edu/~plushnik/375.
259 Lecture 17 Working with Data in MATLAB. Overview  In this lecture, we’ll look at some commands that are useful for working with data!  fzero  sum,
Introduction to Matlab
AMS597 Spring 2011 Hao Han April 05, Introduction to MATLAB The name MATLAB stands for MATrix LABoratory. Typical uses include:  Math and computation.
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.
Introduction to M ATLAB Programming
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Exam #3 Review: Comprehensive Exam Class 14.2 Palm Matlab Book Ch. 1-5.
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.
4. Week 04.March Use of M-File Editor/Debugger: text editor, debugger; editor works with file types in addition to.m (MATLAB “m-files”)
Signals and Systems Laboratory 2013 Teacher: Marek OssowskiTeacher: Marek Ossowski Phone: , Room 14, C3,
1 Prof. Nizamettin AYDIN Biosignals and Systems.
ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical calculus and differential equations.
MATLAB Second Seminar. Previous lesson Last lesson We learnt how to: Interact with MATLAB in the MATLAB command window by typing commands at the command.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
M ATLAB Tutorial Course 1. Contents Continued  Desktop tools  matrices  Logical &Mathematical operations  Handle Graphics  Ordinary Differential.
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 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
Hydroinformatics: Session4 Dr Ivan Stoianov Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
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
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Department of Mechanical Engineering, LSU Session IV MATLAB Tutorials Session IV Mathematical Applications using MATLAB Rajeev Madazhy
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Chapter 4 "Inheritance" Inheritance is a powerful feature that allows the reuse of code. However, be sure that functions applied to objects created at.
MATHLAB Prepared for: Md. Monzur Ashraf Prepared By: Md. Shafiul Parvez Aakaash.
CDA6530: Performance Models of Computers and Networks Chapter 4: Using Matlab for Performance Analysis and Simulation TexPoint fonts used in EMF. Read.
ME451 Kinematics and Dynamics of Machine Systems MATLAB  Tutorial September 16, 2014 Dan Negrut University of Wisconsin-Madison 1.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
M ATLAB Tutorial Covered by: Toby Heyn University of Wisconsin, Madison.
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.
6.094 Introduction to programming in MATLAB Danilo Šćepanović IAP 2008 Lecture 3 : Solving Equations and Curve Fitting.
Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros.
Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem.
ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical calculus and differential equations.
Recap Cubic Spline Interpolation Multidimensional Interpolation Curve Fitting Linear Regression Polynomial Regression The Polyval Function The Interactive.
Introduction to Matlab
Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering.
More Functions in MATLAB. Functions that operate on other functions A function F() can take another function G() as an argument by using a notation:
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.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Basis of Mathematical Modeling LECTURE 3 Numerical Analysis with MATLAB Dr. N.K. Sakhnenko, PhD, Professor Associate.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
MATLAB Tutorial Course
Short MATLAB Tutorial.
TexPoint fonts used in EMF.
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
INTRODUCTION TO MATLAB
Presentation transcript:

1 M ATLAB Short Course Part (2)

2 Elementary Math Function abs, sign: Absolute value and Signum Function sin, cos, asin, acos…: Triangular functions exp, log, log10: Exponential, Natural and Common (base 10) logarithm ceil, floor: Round toward infinities fix: Round toward zero

3 Elementary Math Function round: Round to the nearest integer sqrt: Square root function real, imag: Real and Image part of complex rem: Remainder after division

4 Elementary Math Function max, min: Maximum and Minimum of arrays mean, median: Average and Median of arrays std, var: Standard deviation and variance sort: Sort elements in ascending order sum, prod: Summation & Product of Elements trapz: Trapezoidal numerical integration cumsum, cumprod: Cumulative sum, product diff, gradient: Differences and Numerical Gradient

5 Polynomials and Interpolation Polynomials Representing Roots ( >> roots ) Evaluation ( >> polyval ) Derivatives ( >> polyder ) Curve Fitting ( >> polyfit ) Partial Fraction Expansion ( residue ) Interpolation One-Dimensional ( interp1 ) Two-Dimensional ( interp2 )

6 polysam=[ ]; roots(polysam) ans = i i Polyval(polysam,[ ]) ans = polyder(polysam) ans = [r p k]=residue(polysam,[1 2 1]) r = 3 7 p = k = 1 -2 polysam=[ ]; roots(polysam) ans = i i Polyval(polysam,[ ]) ans = polyder(polysam) ans = [r p k]=residue(polysam,[1 2 1]) r = 3 7 p = k = 1 -2 Example

7 x = [0: 0.1: 2.5]; y = erf(x); p = polyfit(x,y,6) p = x = [0: 0.1: 2.5]; y = erf(x); p = polyfit(x,y,6) p = Example interp1(x,y,[ ]) ans = NaN interp1(x,y,[ ]) ans = NaN

8 Logical Operations » Mass = [-2 10 NaN Inf 31]; » each_pos = Mass>=0 each_pos = » all_pos = all(Mass>=0) all_pos = 0 » all_pos = any(Mass>=0) all_pos = 1 » pos_fin = (Mass>=0)&(isfinite(Mass)) pos_fin = » Mass = [-2 10 NaN Inf 31]; » each_pos = Mass>=0 each_pos = » all_pos = all(Mass>=0) all_pos = 0 » all_pos = any(Mass>=0) all_pos = 1 » pos_fin = (Mass>=0)&(isfinite(Mass)) pos_fin = = = equal to > greater than < less than >= Greater or equal <= less or equal ~ not & and | or isfinite(), etc.... all(), any() find Note: 1 = TRUE 0 = FALSE

9 Importing and Exporting Data Using the Import Wizard Using Save and Load command load fname load fname x y z load fname load fname x y z save fname save fname x y z save fname save fname x y z

10 Editing and Debugging M-Files What is an M-File? The Editor/Debugger Search Path Debugging M-Files Types of Errors (Syntax Error and Runtime Error) Using keyboard and “ ; ” statement Setting Breakpoints Stepping Through  Continue, Go Until Cursor, Step, Step In, Step Out Examining Values  Selecting the Workspace  Viewing Datatips in the Editor/Debugger  Evaluating a Selection

11 Debugging Select Workspace Set Auto- Breakpoints tips

12 Graphics Fundamentals

13 Graphics Basic Plotting plot, title, xlabel, grid, legend, hold, axis Editing Plots Property Editor Mesh and Surface Plots meshgrid, mesh, surf, colorbar, patch, hidden Handle Graphics

14 2-D Plotting Syntax: Example: plot(x1, y1, 'clm1', x2, y2, 'clm2',...) x=[0:0.1:2*pi]; y=sin(x); z=cos(x); plot(x,y,x,z,'linewidth',2) title('Sample Plot','fontsize',14); xlabel('X values','fontsize',14); ylabel('Y values','fontsize',14); legend('Y data','Z data') grid on x=[0:0.1:2*pi]; y=sin(x); z=cos(x); plot(x,y,x,z,'linewidth',2) title('Sample Plot','fontsize',14); xlabel('X values','fontsize',14); ylabel('Y values','fontsize',14); legend('Y data','Z data') grid on

15 Sample Plot TitleYlabelXlabelGridLegend

16 Subplots Syntax: » subplot(2,2,1); » … » subplot(2,2,2) »... » subplot(2,2,3) »... » subplot(2,2,4) »... » subplot(2,2,1); » … » subplot(2,2,2) »... » subplot(2,2,3) »... » subplot(2,2,4) »... subplot(rows,cols,index)

17 Surface Plot Example x = 0:0.1:2; y = 0:0.1:2; [xx, yy] = meshgrid(x,y); zz=sin(xx.^2+yy.^2); surf(xx,yy,zz) xlabel('X axes') ylabel('Y axes') x = 0:0.1:2; y = 0:0.1:2; [xx, yy] = meshgrid(x,y); zz=sin(xx.^2+yy.^2); surf(xx,yy,zz) xlabel('X axes') ylabel('Y axes')

18 3-D Surface Plotting contourf-colorbar-plot3-waterfall-contour3-mesh-surf

19 Specialized Plotting Routines bar-bar3h-hist-area-pie3-rose

20 Programming and Application Development

21 Script and Function Files Script Files Work as though you typed commands into MATLAB prompt Variable are stored in MATLAB workspace Function Files Let you make your own MATLAB Functions All variables within a function are local All information must be passed to functions as parameters Subfunctions are supported

22 Basic Parts of a Function M-File function y = mean (x) % MEAN Average or mean value. % For vectors, MEAN(x) returns the mean value. % For matrices, MEAN(x) is a row vector % containing the mean value of each column. [m,n] = size(x); if m == 1 m = n; end y = sum(x)/m; Output Arguments Input Arguments Function Name Online Help Function Code

23 if ((attendance >= 0.90) & (grade_average >= 60)) pass = 1; end; if ((attendance >= 0.90) & (grade_average >= 60)) pass = 1; end; eps = 1; while (1+eps) > 1 eps = eps/2; end eps = eps*2 eps = 1; while (1+eps) > 1 eps = eps/2; end eps = eps*2 if Statement while Loops Flow Control Statements

24 a = zeros(k,k) % Preallocate matrix for m = 1:k for n = 1:k a(m,n) = 1/(m+n -1); end a = zeros(k,k) % Preallocate matrix for m = 1:k for n = 1:k a(m,n) = 1/(m+n -1); end for Loop Flow Control Statements

25 M-file Programming Features SubFunctions Varying number of input/output arguments Local and Global Variables Obtaining User Input Prompting for Keyboard Input Pausing During Execution Errors and Warnings Displaying error and warning Messages Shell Escape Functions (! Operator) Optimizing MATLAB Code Vectorizing loops Preallocating Arrays

26 Function M-file function r = ourrank(X,tol) % rank of a matrix s = svd(X); if (nargin == 1) tol = max(size(X)) * s(1)* eps; end r = sum(s > tol); function r = ourrank(X,tol) % rank of a matrix s = svd(X); if (nargin == 1) tol = max(size(X)) * s(1)* eps; end r = sum(s > tol); function [mean,stdev] = ourstat(x) [m,n] = size(x); if m == 1 m = n; end mean = sum(x)/m; stdev = sqrt(sum(x.^2)/m – mean.^2); function [mean,stdev] = ourstat(x) [m,n] = size(x); if m == 1 m = n; end mean = sum(x)/m; stdev = sqrt(sum(x.^2)/m – mean.^2); Multiple Input Arguments use ( ) Multiple Output Arguments, use [ ] »r=ourrank(rand(5),.1); »[m std]=ourstat(1:9);

27 Ordinary Differential Equations (Initial Value Problem) An explicit ODE with initial value: Using ode45 for non-stiff functions and ode23t for stiff functions. [t,y] = solver(odefun,tspan,y0,options) [initialtime finaltime] Initialvlue function dydt = odefun(t,y) Use odeset to define options parameter

28 Nonlinear Numerical Functions inline function » f = inline('3*sin(2*x.^2)','x') f = Inline function: f(x) = 3*sin(2*x.^2) » f(2) ans = » f = inline('3*sin(2*x.^2)','x') f = Inline function: f(x) = 3*sin(2*x.^2) » f(2) ans = Use char function to convert inline object to string Numerical Integration using quad » Q = quad('1./(x.^3-2*x-5)',0,2); » F = inline('1./(x.^3-2*x-5)'); » Q = quad(F,0,2); » Q = quad('myfun',0,2) » Q = quad('1./(x.^3-2*x-5)',0,2); » F = inline('1./(x.^3-2*x-5)'); » Q = quad(F,0,2); » Q = quad('myfun',0,2) function y = myfun(x) y = 1./(x.^3-2*x-5); Note : quad function use adaptive Simpson quadrature

29 Nonlinear Numerical Functions fzero finds a zero of a single variable function fun is inline function or m-function fminbnd minimize a single variable function on a fixed interval. x 1 <x<x 2 fminsearch minimize a several variable function Use optimset to determine options parameter. [x,fval]= fzero(fun,x0,options) [x,fval]= fminbnd(fun,x1,x2,options) [x,fval]= fminsearch(fun,x0,options)

30 ODE Example: » [t,y]=ode45( ' myfunc ',[0 20],[2;0]) function dydt=myfunc(t,y) dydt=zeros(2,1); dydt(1)=y(2); dydt(2)=(1-y(1)^2)*y(2)-y(1); Note: Help on odeset to set options for more accuracy and other useful utilities like drawing results during solving.