Matlab. Textbooks Required:, by Palm (3 rd edition) Required: Introduction to MATLAB for Engineers, by Palm (3 rd edition)

Slides:



Advertisements
Similar presentations
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Advertisements

Introduction to Graphing Using MATLAB. Line Graphs  Useful for graphing functions  Useful for displaying data trends over time  Useful for showing.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Welcome to EGR 106 Foundations of Engineering II Course information Today’s specific topics: – Computation and algorithms – M ATLAB Basics Demonstrations.
Introduction to MATLAB
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Introduction to MATLAB 7 for Engineers
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
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.
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.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
1 Chapter 1 MATLAB Primer This introductory chapter is relatively short and has as its main objective the introduction of MATLAB ® to the reader. This.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
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.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
EECE 360/460 Matlab Tutorial Jan Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
INTRODUCTION TO MATLAB LAB# 01
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
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”
1 CS1371 Introduction to Computing for Engineers Introduction to Matlab.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
Interduction to MATLAB Manal Alotaibi Mathematics department College of science King saud university.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Matlab for Engineers Matlab Environment Chapter 2.
BASICS OF MATLAB Engr.Mian Shahzad Iqbal Lecturer,Telecom Department University of Engineering & Technology Taxila.
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.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
ECE 1304 Introduction to Electrical and Computer Engineering
Release Numbers MATLAB is updated regularly
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
StatLab Matlab Workshop
MATLAB How to use (using M-files) Double click this icon
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files)
Plotting Signals in MATLAB
Experiment No. (1) - an introduction to MATLAB
MatLab Program Used to Calculate Interactive
Introduction to Matlab
Presentation transcript:

Matlab

Textbooks Required:, by Palm (3 rd edition) Required: Introduction to MATLAB for Engineers, by Palm (3 rd edition)

Purchasing MATLAB Software Mathworks web site: ( Mathworks web site: ( UH Bookstore—be sure to get version 7 UH Bookstore—be sure to get version 7 UH-IT Department UH-IT Department

About MATLAB a computer programming language a computer programming language a software environment for using the language effectively a software environment for using the language effectively MATLAB is…

Two modes of operation Interactive calculator mode Interactive calculator mode Script mode: execution of complete programs  script files Script mode: execution of complete programs  script files About MATLAB

Running MATLAB creates one or more windows Running MATLAB creates one or more windows One is called the MATLAB Desktop: Primary graphical user interface for MATLAB One is called the MATLAB Desktop: Primary graphical user interface for MATLAB Contains and manages all other windows that are part of MATLAB Contains and manages all other windows that are part of MATLAB According to the configuration some windows may be visible and some may not. You can see this information in the View Menu According to the configuration some windows may be visible and some may not. You can see this information in the View Menu About MATLAB

About MATLAB Desktop

MATLAB Desktop Windows: MATLAB Desktop Windows: – Command Window: Primary place to interact with MATLAB. Place where the user issues the commands. The MATLAB command prompt is the symbol >> – Command History: Running history of prior commands issued in the Command Window. About MATLAB Desktop

MATLAB Desktop Windows: MATLAB Desktop Windows: – Workspace: GUI for viewing, loading, and saving MATLAB variables. – Launch Pad: Tree layout for access to tools, demos and documentation – Current Directory: GUI for directory and file manipulation in MATLAB. About MATLAB Desktop

MATLAB Desktop Windows: MATLAB Desktop Windows: – Help: GUI for finding and viewing on-line documentation. – Array Editor: GUI for modifying the content of MATLAB variables – Editor/Debugger: Text editor and debugger for MATLAB text files. About MATLAB Desktop

What does Matlab understand? Real numbers: e3 Real numbers: e3 Complex numbers: 2+3i 4-6j 2i Complex numbers: 2+3i 4-6j 2i Arrays of numbers: [1,2,3] [1,2;3,4] Arrays of numbers: [1,2,3] [1,2;3,4] Strings: ‘I wish I knew that!’ Strings: ‘I wish I knew that!’

Basics: Basic Operations SymbolOperationMATLAB ^ exponentiation: a b a^b * multiplication: ab a*b / right division: a/b = a/b \ left division: a\b = a\b + addition: a + b a+b - subtraction: a-b a-b

PrecedenceOperation First Parentheses, evaluated starting with the innermost pair. Second Exponentiation, evaluated from left to right. Third Multiplication and division with equal precedence, evaluated from left to right. Fourth Addition and subtraction with equal precedence, evaluated from left to right. Basics: Rules of Precedence

precedence examples precedence examples »8 + 3 * 5 (8 + (3 * 5)) = 23 (8 + (3 * 5)) = 23 »4 ^ 2 – 12 – 8 / 4 * 2 ((4 ^ 2) – 12 – ((8 / 4) * 2))) = 0 ((4 ^ 2) – 12 – ((8 / 4) * 2))) = 0 »3 * 4 ^ ((3 * (4 ^ 2)) + 5) = 53 ((3 * (4 ^ 2)) + 5) = 53 »27 ^ 1 / ^ 0.2 (((27 ^ 1) / 3 )+ (32 ^ 0.2))=11 (((27 ^ 1) / 3 )+ (32 ^ 0.2))=11 Basics: Rules of Precedence

Basics: Variables Variable is a symbol used to contain a value Variable is a symbol used to contain a value MATLAB has some rules to name variables MATLAB has some rules to name variables –Variables names are case sensitive »BOB, Bob, bob, BoB  all different! –Variables names can contain up to 31 characters. Any additional characters are ignored –Variable names must start with a letter, followed by any number of letters, digits or underscores. Punctuation characters are not allowed, because most of them have a special meaning in MATLAB

Some names cannot be used for variables: for, end, if, while, function, return, elseif, case, otherwise, switch, continue, else, try, catch, global, persistent, break Some names cannot be used for variables: for, end, if, while, function, return, elseif, case, otherwise, switch, continue, else, try, catch, global, persistent, break If you try to use any of these reserved words as a variable, MATLAB will report an error If you try to use any of these reserved words as a variable, MATLAB will report an error Of course, you can use words similar by capitalizing one or more letters Of course, you can use words similar by capitalizing one or more letters Also, MATLAB has a number of special variables and constants Also, MATLAB has a number of special variables and constants Basics: Variables

Basics: Special Variables and Constants NameDescription ans Default variable name used for results eps specifies accuracy of f.p. precision i,j imaginary unit, sqrt(-1) Infinfinity NaN undefined numerical result pi realmax largest real f.p. # on THIS computer realmin smallest real f.p. # on THIS computer

Basics Assignment or replacement operator: = Assignment or replacement operator: = It works different than the equal sign: the variable on the left-hand side is replaced by the value generated by the right-hand side. Therefore, only one variable name must be in the left-hand side It works different than the equal sign: the variable on the left-hand side is replaced by the value generated by the right-hand side. Therefore, only one variable name must be in the left-hand side –x = x – 2 –6 = x  NOT VALID!!! The right-hand side must have a computable value The right-hand side must have a computable value –x = 5 + y; Is y defined?

Basics Example: Volume of a circular cylinder Example: Volume of a circular cylinder Given a circular cylinder with height 15m and radius 8 m, find the radius of a cylinder of the same height but with a volume that is 20% larger.

Solution Algorithm Calculate current volume Calculate current volume Calculate desired volume Calculate desired volume Determine new radius by rewriting volume equation in terms of radius: Determine new radius by rewriting volume equation in terms of radius:

Basics The formula for volume is V=  r 2 h The formula for volume is V=  r 2 h The radius is given by: The radius is given by: >> r = 8; >> h = 15; >> V = pi*r^2*h >> newV = 1.2*V >> newr = sqrt(newV/(pi*h)) ans =

Basics: Commands CommandDescription clcclears Command window clearremoves all variables clear var1 var2removes listed variables exist (‘name’)determines if name (file or var) exists quitStops MATLAB wholists current variables whoslists current variables with sizes :generates array,separates elements of array ;suppresses screen printing, also new line in array …continues a line

Basics: Complex Numbers complex number ex: complex number ex: complex number complex number »s = 3 + 7i »w = 5 – 9i MATLAB Ex. MATLAB Ex. –Given x = i and y = 6 – 2i –find x + y=x + y=1+7i x*y =x*y = i i x/y =x/y = i

Some Simple Functions sin(x) sin(x) sqrt(x) sqrt(x) acos(x) acos(x) exp(x) exp(x) log(x) log(x)

Interpretation sin(180) ????? sin(180) ????? acos(.5) ????? acos(.5) ????? exp(1) ????? exp(1) ????? log(10) ????? log(10) ?????

Polynomials in Matlab A polynomial is represented by its array of coefficients, beginning with the “leading” coefficient (coefficient of highest power): A polynomial is represented by its array of coefficients, beginning with the “leading” coefficient (coefficient of highest power):

Special Polynomial Functions polyval(a,x): evaluates polynomial whose coefficients are in array a at the point x polyval(a,x): evaluates polynomial whose coefficients are in array a at the point x roots(a): returns a vector containing all of the roots of the polynomial whose coefficients are in the array a roots(a): returns a vector containing all of the roots of the polynomial whose coefficients are in the array a poly(v): returns the coefficient vector of a polynomial whose roots are stored in the vector v poly(v): returns the coefficient vector of a polynomial whose roots are stored in the vector v

Polynomial Examples >> a=[2,4,-1,5]; >> polyval(a,0) ans = 5 >> poly([1, 2, 3]) ans = >> v=roots(a) v = i i

Plotting in Matlab The basic command to plot a set of points in the plane is plot(x,y) The basic command to plot a set of points in the plane is plot(x,y) x is a vector containing the horizontal coordinates x is a vector containing the horizontal coordinates y is a vector containing the vertical coordinates y is a vector containing the vertical coordinates x & y must be the same size—either both row vectors or both column vectors x & y must be the same size—either both row vectors or both column vectors The plot actually appears in a separate window, called a “figure” window. The plot actually appears in a separate window, called a “figure” window.

Plotting Example >> x=[0:.01:10]; %generate x values >> y=sin(x); %generate y values --sin function %applied to array of x’s %applied to array of x’s >> figure(1) %give number to figure >> plot(x,y),title('My first Plot'),xlabel('x axis'),... ylabel('y axis')

CommandDescription plot(x,y) Generates plot. title(‘text’) Puts title on plot. xlabel(‘text’) Adds label to horizontal axis. ylabel(‘text’) Adds label to vertical axis. legend Places a legend figure n Opens a new figure window numbered n hold on Holds on the current graph Plotting in MATLAB

Some Plot Options Some Plot Options COLORSSYMBOLS LINE TYPES y yellow. point - solid m magenta o circle : dotted c cyan x x-mark -. dash dot r red + plus -- dashed g green * star b blue s square w white d diamond k black v,^, triangles

Matlab Files Script & function files—plain ASCII, extension.m Script & function files—plain ASCII, extension.m MAT files—binary file, used to “save” workspace variables, extension.mat MAT files—binary file, used to “save” workspace variables, extension.mat

>> pi ans = >> format long >> pi ans = >> format rat >> pi ans = 355/113

Controlling Input and Output The disp Function: Useful to control the output The disp Function: Useful to control the output disp: Display array. disp(X) displays the array, without printing the array name. It's the same as leaving the semicolon off an expression except that empty arrays don't display. If X is a string, the text is displayed. EXAMPLES: EXAMPLES: >> disp(X) displays the value of variable X, but not its name >> disp(‘The value of X is:’) displays the string The value of X is: but no number

Controlling Input and Output The num2str Function: Useful to make the output look better The num2str Function: Useful to make the output look better num2str: Convert number to string. T=num2str(X) converts the array X into a string representation T with about 4 digits and an exponent if required. Allows display of label on same line as value. T=num2str(X) converts the array X into a string representation T with about 4 digits and an exponent if required. Allows display of label on same line as value. >> x= ; >> disp(['The value of x is ',num2str(x),'ft']) The value of x is ft

Controlling Input and Output The input Function: Used to get interactive input. The input Function: Used to get interactive input. input: Prompt for user input. input: Prompt for user input. R = input('How many apples') gives the user the prompt in the text string and waits for input from the keyboard. The input can be any MATLAB expression, which is evaluated, using the variables in the current workspace, and the result returned in R. If the user presses the return key without entering anything, INPUT returns an empty matrix. R = input('How many apples') gives the user the prompt in the text string and waits for input from the keyboard. The input can be any MATLAB expression, which is evaluated, using the variables in the current workspace, and the result returned in R. If the user presses the return key without entering anything, INPUT returns an empty matrix. R = input('What is your name','s') gives the prompt in the text string and waits for character string input. The typed input is not evaluated; the characters are simply returned as a MATLAB string. R = input('What is your name','s') gives the prompt in the text string and waits for character string input. The typed input is not evaluated; the characters are simply returned as a MATLAB string.

Controlling Input and Output The Menu Function: Used to get interactive input by selection of an alternative from a menu. The Menu Function: Used to get interactive input by selection of an alternative from a menu. k = menu(‘title’,’option1’,’option2’,…) k = menu(‘title’,’option1’,’option2’,…) choices=['B','D','F'];k=menu('Whodoyouwant?',choices(1),choices(2),choices(3)); disp(['You picked ',choices(k)])

Input from File load– as a command or as a function, requires each line in file has same number of values, loads entire contents of file into an array >>load data.txt; % name of array will be data >>load(‘new.txt’); % name of array will be new >>mydata = load(‘proj3.txt’); %name of array will be mydata