Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard

Slides:



Advertisements
Similar presentations
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Advertisements

Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
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:
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
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.
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (2): MATLAB Environment (Chapter 1)
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
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.
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Simulink SubSystems and Masking April 22, Copyright , Andy Packard. This work is licensed under the.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
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.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
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.
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Getting Started with MATLAB (part 3) 1. Algebra, 2. Trig 3. The keyword ans 4. Clean up and suppress output: finalizing the software’s presentation 1.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
EGR 115 Introduction to Computing for Engineers Introduction to MATLAB Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Interduction to MATLAB Manal Alotaibi Mathematics department College of science King saud university.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Introduction to MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Release Numbers MATLAB is updated regularly
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Lecture 1: Introduction
StatLab Matlab Workshop
Class Info E177 January 22, me. berkeley
Chapter 2 - Introduction to C Programming
WEB PROGRAMMING JavaScript.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Introduction to MATLAB
Arithmetic Expressions & Data Conversions
Experiment No. (1) - an introduction to MATLAB
Using Script Files and Managing Data
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
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.
Introduction to MATLAB
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Introduction to C Programming
MatLab Program Used to Calculate Interactive
Introduction to Matlab
Arithmetic Expressions & Data Conversions
Presentation transcript:

Basics of Matlab UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Matlab Basics Starting Matlab double-click the Matlab icon, or in a terminal window, type matlab, and return The Command Window and the Matlab prompt >> The Desktop menu undocking command window tiling other windows (command history, workspace, current directory, profiler) The File menu Preferences The Help menu

Matlab Basics Matlab as a calculator Precedence rules in expressions Type expressions at the >>, and press return Result is computed, and displayed as ans Use numbers, +, *, /, -, (), sin, cos, exp, abs, round,… Precedence rules in expressions Left-to-right within a precedence group Precedence groups are (highest first) Highest precedence is parenthesis, then… Power (^) Multiplication and division (*, /) Addition and subtraction (+, -)

Examples of expressions Legal expressions >> 4 >> 5 + pi >> 6*sqrt(2)^4-12 >> 6 * sqrt( 2.0) ^ 4 - 12 >> sin(pi/3)^2 + cos(pi/3)^2 >> 1.0/0.0 >> -4/inf >> 0/0 Illegal expressions >> 2 4 >> (2,4) Error messages Read them carefully – a large portion of the time you will quickly figure out what is wrong

Variables Use names to assign result of an expression to a variable Variables do not need to be declared before assignment A single “equal” sign (=) is the assignment operator, LHS = RHS Read this as evaluate expression on the right-hand side, and then… assign the result to the variable named on the left-hand-side Therefore The right-hand-side needs to be a legal Matlab expression The left-hand-side needs to be a single variable name (this will get more sophisticated later on) A semicolon at the end of the RHS expression suppresses the display, but the assignment still takes place.

Examples of Variables and Assignment Legal >> A = sqrt(13) >> B = exp(2); >> A = 2*B >> A = A + 1 >> C = tan(pi/4) Illegal (all for different reasons) >> D = sqrt(E) + 1; >> 3 = E >> 3*A = 14 >> F = 2 3

The “workspace” All variables that you create are accessible from the prompt >> Variables are accessed using their name as a reference Builtin Matlab commands >> who >> whos are used to see what is in the workspace. You can clear (ie, erase) variables with the clear command >> clear A clears the variable A from the workspace. How do we check that it worked?

Comparison (as opposed to assignment) Compare the equality of two expressions with a double equal sign, == Several arithmetic comparisons are available. Can also compare greater than (with >) less than (with <) greater than or equal (with >=) less than or equal (with <=), and not equal (with ~=) The comparison is itself an expression Its value is either 1 (true) or 0 (false), and can be used in assignment >> 5==sqrt(25) >> E = 1.72>tan(pi/3) What do you need to know to determine the result of >> E = 4>5-2 Precedence group is lower than addition and subtraction, so…

Quiting Matlab Type quit at the prompt, or Select Exit Matlab from the File menu However…

Saving the workspace >> save >> save Andy When you “quit” Matlab, the variables in the workspace are erased from memory. If you need them for later use, you must save them. You can save all variables (or just some of them) to a file using the command save >> save saves all of the variables in the workspace into a file called matlab.mat (it is saved in the current directory) >> save Andy saves all of the variables in the workspace into a file called Andy.mat >> save Important A B C D* saves the variables A, B, C and any variable beginning with D in the workspace into a file called Important.mat

Loading from a .mat file load is the opposite of save. It reads a .mat file, putting all variables contained in the .mat file into the workspace >> load loads all of the variables from the file matlab.mat >> load Andy loads all of the variables from the file andy.mat There are no known security problems with load. Hence, you can safely send (as attachment), receive and use .mat files from others.

Complex Numbers All arithmetic in Matlab works on complex numbers as well. When you start Matlab, two variables already exist, and are equal to . They are i and j. It’s common to overwrite them without even realizing, but you can always create the number with the expression sqrt(-1) >> i >> sqrt(-1) >> j >> 4 + 6j >> 4 + 6*j >> C = 1 – 2i; >> real(C) >> imag(C) >> abs(C) >> angle(C)*180/pi