Working in MATLAB.

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Advertisements

Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
MATLAB Basics CS 111 Introduction to Computing in Engineering and Science.
Introduction to MATLAB
Introduction to MATLAB Week 13 – 4/21/09. Instructor: Kate Musgrave Time: Tuesdays 3-5pm Office Hours: Tuesdays 1:30-3pm
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Introduction to Matlab. Entering Commands Constants and Functions >> pi ans = >> eps ans = e-016 >> sin(pi/2) ans = 1 >> log(1000) ans =
Introduction to MATLAB
MATLAB Tutorial MATLAB Basics & Signal Processing Toolbox.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (2): MATLAB Environment (Chapter 1)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
A Brief introduction to MatLAB ($50 - $99 for students)
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
Introduction to Programming with RAPTOR
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Engineering Analysis – Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Introduction to Matlab
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Tutorial Iman Moazzen First Session – September 11, 2013.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
מטלב וניתוח נתונים ממערכות עצביות היכרות עם MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Matlab Introduction.
Introduction to MATLAB for Engineers, Third Edition
Introduction to Matlab ( Google Search )
Basic operations in Matlab
Introduction to Tharmarajah Thiruvaran March 2007.
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Introduction To MATLAB
MATLAB PRIMER by Michael Medvinsky(2014)
StatLab Matlab Workshop
Part I – Matlab Basics.
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
Communication and Coding Theory Lab(CS491)
Introduction to Matlab
INTRODUCTION TO MATLAB
Introduction to MATLAB 7
Simulation And Modelling
Experiment No. (1) - an introduction to MATLAB
MATLAB Introduction MATLAB can be thought of as a powerful graphing calculator but with a lot more buttons! It is also a programming language, commands.
Terminal-Based Programs
CS 111 Introduction to Computing in Engineering and Science
Matlab Basics Tutorial
MatLab Program Used to Calculate Interactive
Chapter 2 MATLAB Environment
Presentation transcript:

Working in MATLAB

Working in MATLAB You can perform operations in MATLAB in two ways: In the interactive mode, in which all commands are entered directly in the Command window, or 2. By running a MATLAB program stored in script file. This type of file contains MATLAB commands, so running it is equivalent to typing all the commands—one at a time—at the Command window prompt. You can run the file by typing its name at the Command window prompt.

Working in MATLAB Keep in mind when using script files: 1. The name of a script file must begin with a letter, and may include digits and the underscore character, up to 63 characters. 2. Do not give a script file the same name as a variable. 3. Do not give a script file the same name as a MATLAB command or function. You can check to see if a command, function or file name already exists by using the exist command.

Working in MATLAB - Debugging Script Files Program errors usually fall into one of the following categories. Syntax errors such as omitting a parenthesis or comma, or spelling a command name incorrectly. MATLAB usually detects the more obvious errors and displays a message describing the error and its location. Errors due to an incorrect mathematical procedure, called runtime errors. Ex: 0 0

Use of M-File Extension “.m” Click to create a new M-File Extension “.m” A text file containing script or function or program to run

Use of M-File Save file as Denem430.m If you include “;” at the end of each statement, result will not be shown immediately

Working in MATLAB - Programming Style Comments section a. The name of the program and any key words in the first line. b. The date created, and the creators' names in the second line. c. The definitions of the variable names for every input and output variable. Include definitions of variables used in the calculations and units of measurement for all input and all output variables! d. The name of every user-defined function called by the program.

Working in MATLAB - Programming Style Input section Include input data and/or the input functions and comments for documentation. Calculation section 4. Output section This section might contain functions for displaying the output on the screen.

Working in MATLAB - Programming Style- Example Problem: The speed v of a falling object dropped with no initial velocity is given as a function of time t by v = gt. Plot v as a function of t for 0 < t < tfinal, where tfinal is the final time entered by the user. % Program falling_speed.m: % Plots speed of a falling object. % Created on June 30, 2017 by MARK % % Input Variable: % tfinal = final time (in seconds) % Output Variables: % t = array of times at which speed is computed in seconds % v = array of speeds (meters/second)

Working in MATLAB - Programming Style- Example % Parameter Value: g = 9.81; % Acceleration in SI units % % Input section: tfinal = input(’Enter final time in seconds:’); % Calculation section: dt = tfinal/500; % Create an array of 501 time values. t = 0:dt:tfinal; % Compute speed values. v = g*t; % Output section: Plot(t,v),xlabel(’t (s)’),ylabel(’v m/s)’)

The Help System Search for appropriate function >> lookfor keyword Rapid help with syntax and function definition >> help function An advanced hyperlinked help system is launched by >> helpdesk Complete manuals (html & pdf) http://www.mathworks.com/access/helpdesk/help/helpdesk.html

The help system example >> lookfor convolution CONV Convolution and polynomial multiplication. CONV2 Two dimensional convolution. CONVN N-dimensional convolution. DECONV Deconvolution and polynomial division. CONVENC Convolutionally encode binary data. DISTSPEC Compute the distance spectrum of a convolutional code. ... >> help conv C = CONV(A, B) convolves vectors A and B. The resulting vector is length LENGTH(A)+LENGTH(B)-1. If A and B are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials. Class support for inputs A,B: float: double, single See also deconv, conv2, convn, filter and, in the Signal Processing Toolbox, xcorr, convmtx.... Step 1: Step 2:

The help system - Example Step 3: doc funcname: Opens the Help Browser to the reference page for the specified function funcname, providing a description, additional remarks, and examples.

The help system

The help system

Note “%” is the neglect sign for Matlab (equaivalent of “//” in C). Anything after it on the same line is neglected by Matlab compiler. Sometimes slowing down the execution is done deliberately for observation purposes. You can use the command “pause” for this purpose pause %wait until any key pause(3) %wait 3 seconds

Useful Commands The two commands used most by Matlab users are >>help functionname >>lookfor keyword

Interactive Calculations Matlab is interactive, no need to declare variables >> 2+3*4/2 >> a=5e-3; b=1; a+b Most elementary functions and constants are already defined >> cos(pi) >> abs(1+i) >> sin(pi)

MATLAB can do simple math just as a calculator. OPERATION SYMBOL EXAMPLE Addition, a + b + 3 + 22 Subtraction, a – b - 90-44 Multiplication, a*b * 3.14*4.20 Division, a b / or \ 56/8 = 8\56 Exponentiation, ab ^ 2^16

MATLAB Variable Names Variable names are cASE sENSITIVE Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores. All variables are shown with >> who >> whos Variables can be stored on file >> save filename >> clear >> load filename

MATLAB Special Variables ans Default variable name for results pi Value of  inf NaN Not a number e.g. 0/0 i and j i = j = eps Smallest incremental number realmin The smallest usable positive real number realmax The largest usable positive real number

Some Useful MATLAB commands what List all m-files in current directory dir List all files in current directory ls Same as dir type test Display test.m in command window delete test Delete test.m cd a: Change directory to a: chdir a: Same as cd pwd Show current directory which test Display current directory path to test.m

Functions MATLAB has many built-in functions. Some math functions are: acos, acosh acot, acsc, acsch, asec, asech, asin, asinh, atan, atan2, atanh, cos, cosh, cot, coth, csc, csch, sec, sech, sin, sinh, tan, tanh, exp, log, log10, log2, pow2, sqrt, nextpow2, abs, angle, conj, imag, real, unwrap, isreal, cplxpair, fix, floor, ceil, round, mod, rem, sign, cart2sph, cart2pol, pol2cart, sph2cart, factor, isprime, primes, gcd, lcm, rat, rats, perms, nchoosek, airy, besselj, bessely, besselh, besseli, besselk, beta, betainc, betaln, ellipj, ellipke, erf, erfc, erfcx, erfinv, expint, gamma, gammainc, gammaln, legendre, cross, dot

MATLAB Math & Assignment Operators Power ^ or .^ a^b or a.^b Multiplication * or .* a*b or a.*b Division / or ./ a/b or a./b or \ or .\ b\a or b.\a NOTE: 56/8 = 8\56 - (unary) + (unary) Addition + a + b Subtraction - a - b Assignment = a = b (assign b to a)

MATLAB Matrices MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored. Vectors are special forms of matrices and contain only one row OR one column. Scalars are matrices with only one row AND one column

Polynomial example Find polynomial roots: >> x=[1.2,0.5,4,10] 1.200 0.500 4.00 10.00 >> roots(x) ans = 0.59014943179299 + 2.20679713205154i 0.59014943179299 - 2.20679713205154i -1.59696553025265

Graphics Visualization of vector data is available >> x=-pi:0.1:pi; y=sin(x); >> plot(x,y) >> xlabel(’x’); ylabel(’y=sin(x)’); plot(x,y) stem(x,y)

Integration example Find the integral: example with trapz function: >> x = 0:0.5:10; y = 0.5 * sqrt(x) + x .* sin(x); >> integral1 = trapz(x,y) integral1 = 18.1655

Symbolic Math Toolbox –Integration & Differentiation The Symbolic Math Toolbox uses "symbolic objects" produced by the "sym" funtion. >> x = sym('x'); % produces a symbolic variable named x >> f=x^3; % defines a function Example: - ? - ? >> x = sym('x'); >> diff(x^3) ans = 3*x^2 >> int(x^3) 1/4*x^4

Symbolic Math Toolbox Once a symbolic variable is defined, you can use it to build functions. EZPLOT makes it easy to plot symbolic expressions. >> x = sym('x'); >> f = 1/(5+4*cos(x)) >> ezplot(f) >> help ezplot ezplot Easy to use function plotter ezplot(FUN) plots the function FUN(X) over the default domain -2*PI < X < 2*PI, where FUN(X) is an explicitly defined function of X.

Symbolic Math Toolbox Plot the following functions: >> x = sym('x'); Gaussian >> ezplot (exp(-pi*x*x)) sinc(x)=si(x)=sin(x)/(x) >> ezplot(sinc(x))