Programming in Matlab Day 1: Basics. Matlab as a calculator >> 2+2 ans = 4 >> 2^3 ans = 8 >> sqrt(2) ans = 1.4142 Command Window Basic operations Sum:

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Adding Automated Functionality to Office Applications.
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.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
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.
Linux Operations and Administration
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
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.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
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.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Digital Image Processing Introduction to M-function Programming.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Today we will learn MATLAB Click Start  All programm  Class Software  Matlab This command window will be seen with a prompt sign >> Any command can.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
MySQL Getting Started BCIS 3680 Enterprise Programming.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Prof. Mark Glauser Created by: David Marr
Matlab Training Session 4: Control, Flow and Functions
Scripts & Functions Scripts and functions are contained in .m-files
JavaScript: Control Statements.
MATLAB DENC 2533 ECADD LAB 9.
User Defined Functions
Matlab review Matlab is a numerical analysis system
T. Jumana Abu Shmais – AOU - Riyadh
3 Control Statements:.
Logical Operations In Matlab.
Computer Science Core Concepts
Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
2.6 The if/else Selection Structure
Scripts In 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.
Matlab Basics.
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

Programming in Matlab Day 1: Basics

Matlab as a calculator >> 2+2 ans = 4 >> 2^3 ans = 8 >> sqrt(2) ans = Command Window Basic operations Sum: + Difference: - Multiplication: * Division: / Power: ^ Square root: sqrt() Exponential: exp() Logarithm: log() … Useful: Key  brings back the past.

Variables Variable: Object that stores information, and which can be used in operations. >> a=2 a = 2 >> b=3; >> a+b ans = 5 >> a*(b+2) ans = 10 >> resultado=b+a^2 resultado = 7 Command Window a2 ans10 b3 resultado7 Workspace Create variable a and assign value 2. Semicolon (;) prevents visual output Parentheses to group operations Usually we assign the result of an operation to another variable (otherwise, it goes to ans)

Variables They can have any name, except: – Names beginning by numbers. – Names with "strange" characters (ñ,ó,?, etc) or reserved characters (+,-,*,/,^, etc.) – Names used for other functions (this is especially dangerous. Matlab will not issue an error, but the function whose name you have used will not work until the variable is deleted). Valid names: – a – chopo – Gr17 – a7jD28r14 Non-valid names: – 1gatito (starts by a number) – a+b (includes character “+”, reserved for summation). Not recommended: – disp (it is the name of a function) To check if a name is used by a Matlab function, just type it in Command Window. If matlab says ??? Undefined function or variable 'nombre'. Then you can use it. Matlab distinguishes between upper-case and lower-case characters – Variable gatito is NOT the same as variable Gatito.

Variables Basic types – Text (also known as Characters). It goes between apostrophes. Example: ‘Hola’ is text. Hola (without apostrophes) is understood by Matlab as the name of a variable or a function – Numbers. Definition of variables Variables are created the first time they are defined. If the variable already exists, it is overwritten. Examples: >> a = 17 a = 17 >> b = ‘Hola’ b = Hola >> c = a c = 17 >> b=14 b = 14 Problem: Calculotes

Self-referent variable operations >> b = 1; >> b = b + 1 b = 2 This is the old value of variable b (in this case, 1)

save, load, clear Delete variables To delete ALL variables: >> clear To delete only some variables: >> clear pepito juanito Only deletes variables pepito and juanito Save and load variables To save ALL variables to a file called nombrearchivo.mat: >> save nombrearchivo To save only some variables: >> save nombrearchivo pepito juanito To load the variables stored in file nombrearchivo.mat: >> load nombrearchivo

The first program of any programmer >> disp(‘¡Hola, mundo!’)

Scripts >> edit name_of_program Any instruction in the script will be executed as if it had been typed in the Command Window. Comments: Matlab ignores anything that follows the symbol %. Example: b=7; % Matlab will assign the value 7 to variable b, and will not read this sentence.

Execution of scripts Once it has been saved, the script is executed by typing its name in the Command Window. BUT NOTE: Matlab will only look for programs in a few directories. If your script is not in one of those directories, Matlab will not be able to find it. These folders are: Current directory: Indicated at the top of Matlab's window. Matlab's path: It is a list of directories where Matlab always looks for programs. You can add a new directory to the path in the menu File→Set path…

Matlab's help......is wonderful. As well as error messages (although we all hate them).

if if a>b disp('a is greater than b') end disp('program continues') if Condition Code executed if the condition is true end Afterwards, the program continues Main types of conditions: a==b → a equal to b (with two equal signs) a~=b → a different than b (symbol ~ is typed with AltGr+4) a>=b → a greater or equal than b a<=b → a less or equal than b a>b → a greater than b a<b → a less than b The comparison may involve variables or numbers (for example, a==1 )

if...else if a>b disp('a is greater than b') else disp('a is less or equal than b') end disp('Program continues') if Condition Code executed if the condition is true else Afterwards, the program continues end Code executed if the condition is false

if...elseif...else if a>b disp('a is greater than b') elseif a<b disp('a is smaller than b') else disp('a is equal as b') end if Condition Code executed if the condition is true else end Code executed if both the first and the second conditions are false elseif Code executed if the first condition is false and the second condition is true The second condition is tested ONLY if the first condition is false

The for loop for c = 1:1:10 disp('This will be shown 10 times') vector(c)=c+7; end for end Counter variable, which goes from one to ten, in steps of one Loop: Set of instructions that are repeated several times. Portion of the code that is repeated The counter variable may be used inside the loop. This variable is different each iteration (in this case, its value is 1 the first iteration, 2 the second iteration, etc.