Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3: M/O/F/ for Engineering Applications B Burlingame 16September2015.

Similar presentations


Presentation on theme: "Lecture 3: M/O/F/ for Engineering Applications B Burlingame 16September2015."— Presentation transcript:

1 Lecture 3: M/O/F/ for Engineering Applications B Burlingame 16September2015

2 Announcements Homework 1 due Homework 2 posted tonight  Due in two weeks Homework always gets turned in to me Labs always get turned in to the lab instructor I still don’t have access to Canvas  Hard Copies only until Canvas is alive

3 3 What and Why? Data  Pieces of information we deal with in the problem solving process Computers & Software  Tools we use to help automate finding these solutions Matlab Excel C

4 Computer Architecture (Simplified) Central Processing Unit (CPU) – the math engine of a PC  Accesses memory and performs operations (usually mathematical) upon the contents  Delivers the results to memory or peripherals Memory  Storage places in working memory to hold a changeable value  Working Memory – physical digital memory a central processing unit uses to store information  Volatile – memory state is not stored across reboots Peripherals  Everything else (video cards, speakers, hard drives, etc.)

5 Variables Locations in memory used to hold data  Data has a “type”, or the manner in which bits are to be manipulated Why is it important to know about data types?  Need to know about data types to effectively use a programming language  It’s all just bits the lowest level What would you rather work with… int i; float r; i = 3; r = cos(2*i*PI); or… 00000010 10000000 00000000 00000110 10000010 10000000 01111111 11111100 11001010 00000001 00000000 00000000 etc.

6 6 Kinds of Data (simplified view)   The basic kinds of data that we will mostly use:  Numeric Integers: Real (floating point) numbers:  Character (are enclosed by single quotes in C) All letters, numbers, and special symbols  Ex. ‘A’, ‘+’, ‘5’  String (are enclosed by double quotes in C) Are combinations of more than one character  Ex. “programming”, “ME 30”  Logical (also called ‘Boolean’ named after George Boole an English mathematician from the early 1800’s) True or False (1 or 0)

7 Constants and Variables Constant  A data element that never changes in your program 5, 62.37, 4.219E-6, “record_string”, ‘$’ i = j + 7;/* which one is the constant? */ first_letter = ‘a’;/* which one is the constant? */ Variable  A data element that can take on different values Its name represents a location (address) in memory    i = j + 7;/* which are variables? */  second_letter = ‘b’;/* which is the variable? */ Values are ‘assigned’ using a single equal sign ( = )  Read the statement: i = j + 7;

8 Memory Limitations All data types have a range  The set of valid values  Recall: memory is physical, this implies limitations  What is the range for a signed 16 bit integer? Why does this matter?  We must always keep in mind the limitations of the tools we use Let’s talk about MatLab

9 MatLab Numeric Data Types NameBitsRange double64-1.797e308 – 1.798e308 -2.23e-308 - 2.23e-308 single32-3.4e38 – 3.4 e38 int[8,16,32,64]8, 16, 32, 64(-2 (n-1) + 1) – (2 (n-1) -1) uint[8,16,32,64]8, 16, 32, 640 – (2 n – 1)

10 MatLab 2013 - Current Layout Command Window Interactive ‘scratch pad’ Workspace Workspace: lists variables Directory: files in current dir Workspace/ Current Directory Window Command History Window

11 M/O/F Basics - 1 Fundamental element: the array  even integers are stored as 1x1 array of double floating point value by default How many bytes?  See Workspace Window Useful commands (see documentation for more details!)  who (information about what is in memory)  whos (more detailed information about what is in memory)  clc (clears the command window. In QtOctave, View  Clear Terminal)  clear (clears all user defined variables)  help name (provides information on command name)

12 Matlab and Octave M/O/F are ‘high-level’ languages  They abstract away the nitty-gritty details Numerical calculation  Oriented toward engineering, mathematical, and scientific computing Matlab (Matrix Laboratory)  Particularly good at handling lists of numbers (called arrays) Powerful graphics and analysis tools  Controls, signal processing, data analysis, and other specialized ‘toolboxes’ are available Widely used in industry Matlab is commercial software (a student version is available) http://www.mathworks.com/ http://www.mathworks.com/ Octave and FreeMat are open source and free: http://www.gnu.org/software/octave/ (main page) http://freemat.sourceforge.net/index.html (main page) http://www.gnu.org/software/octave/ http://freemat.sourceforge.net/index.html

13 Octave Command Line Enter commands at the prompt

14 QtOctave GUI Enter commands at the prompt

15 Octave as a ‘scratch pad’ dynamically typed

16 M/O/F Basics - 2 Script files (.m files)  Text files containing M/O/F statements Type in a text editor (M/O/F) or use M-file editor in Matlab (File/New/M-File or Ctrl-n)  Comments Matlab: % Octave: % or # FreeMat: %  Example: look at cool_plot.m Verify directory containing cool_plot.m is in the file path  MATLAB: File | Set Path… | select folder  Octave: addpath(‘dir_path’) find from Windows Explorer

17 Script File Example: cool_plot.m

18 Octave Script File Example cool_plot.m needs to be in the ‘load path’ Octave: addpath(dir-path) Example (yours may be different): addpath('C:\Octave\Octave_code') Matlab: File | Set Path…

19 Matlab Script File Example

20 MatLab Variable Names Begins with a letter Followed by letters, numbers, and an underscore _ Case sensitive Cannot be a MatLab reserved word Note: similar to C ValidInvalid area123x area_of_a_circlearea of a circle NumberOfCountriesIf (if is a reserved word) x123xHowMany!

21 M/O/F Basics - 3 Array creation  A = [1 2 3; 4,5,6; 7 8 9] size(A) Indexing elements  A(1,2) Adding elements  A(4,2) = 13  A(4,2) = 13; Separate elements by spaces or commas, and rows by semicolon or carriage return Index by enclosing indices in ( ) What does this do?

22 M/O/F Basics - 4 Vectors ( just 1xn or nx1 arrays)  B=[sin(pi/4), -2^3, size(A,1)] Vectors using the colon operator  C = 1 : 0.25 : 2  D = 0 : 5 Linspace  E = linspace(0,5,3 ) Logspace  F = logspace(1,3,5) base : increment : limit start : end : n For n elements linearly spaced between start and end start : end : n For n elements logarithmically spaced between start and end 1 0.25 2 For an increment of 1: base : limit Format: Note: limit is reached only if (limit-base)/increment is an integer

23 M/O/F Basics - 5 Manipulating arrays  Add a row A = [ A ; [ 10 11 12 ] ]  Extract a sub-array G = A(1:3, 1:2) Colon operator by itself means, “all the elements”  Can apply to the dimensions of the arrays to access all the elements of a row or column  Can apply to the entire array to get all the elements as a column vector  What order will they come out?  Examples H = [1:4; linspace(10,40,4); 400:-100:100] I = H( [1 2], : ) J = H( : ) What will this produce? Assuming A = [ 1 2 3; 4,5,6 ; 7 8 9 ] What does this do? Matlab stores array elements in column- major order.

24 M/O/F Basics - 6 Special matrices  zeros, ones, eye K=zeros(3) L=ones(4) M=eye(3)

25 M/O/F Basics - 7 Matrix operations  Arithmetic just like with scalars! (But need to take care that dimensions agree) N=L*7 O=N+eye(4) P=B*E P=B*E’ Q=A+B Q=B+E [1 2]*[3 ; 4] What does the ‘ do? A=[1 2 3; 4,5,6; 7 8 9] B=[sin(pi/4), -2^3, size(A,1)] E=linspace(0,5,3) L=ones(4)

26 M/O/F Basics - 8 Matrix operations, cont.  Matrix division R1=10k R2=R3=5k V=10V Matrix solution If we had iR = V instead, we’d use ‘right’ division: ( i = R / V ) roughly the same as: i = VR -1 R2 R1 R3 +V i1 i2 i3 R = [1 -1 -1; 0 0 10e3; 0 10e3 0]; V = [0 10 10]’; I = R \ V i 1 - i 2 - i 3 = 0 R 1 i 3 = V (R 2 +R 3 ) i 2 = V

27 M/O/F Basics - 9 Matrix operations, cont.  Element-by-element operations use. (dot) and the operator (other than addition/subtraction) dot product of two vectors Note!! period-asterisk means element-by-element multiplication

28 M/O/F Basics - 10 Functions  Like script M-files, but several differences: first line must be of the form: function [output args] = function_name(input args) variables generated in the function are local to the function, whereas for script files (.m files), variables are global the file must be named, ‘function_name.m’  Make sure you add comments at the start that describe what the function does  Example: root-mean-square function, rms1.m keyword Name that you assign

29 M/O/F Basics - 10.1 Functions, cont.  Example: root-mean-square function, cont. Pseudocode:  square each element of x  sum the squares  divide by N  take the square root Expression to square each element in vector x  xs = x.^2 Sum the squares  sums = sum(xs) Divide by N  N = length(x)  ms = sums/N Take the square root  rms = sqrt(ms) Before you write the function, make sure the name you propose is not already used! help fn_name

30 M/O/F Basics - 10.2 Functions, cont.  Example: root-mean-square function, cont. function [y] = rms(v) % rms(v) root mean square of the elements of the column vector v % Function rms(v) returns the root mean square of the elements % of the column vector, v. If v is a matrix, then rms(v) returns % a row vector such that each element is the root mean square %of the elements in the corresponding column of v. vs = v.^2; % what does this line do? Also note semicolon. s = size(v); % what does this line do? y = sqrt(sum(vs,1)/s(1)); % what is s(1)? Let v=sin([0: 0.0001*pi: 2*pi]’), one period of a sine wave. The RMS value of a sine wave is its amplitude*1/sqrt(2) Does rms() work with a row vector? How about a matrix? H1 comment line (used in lookfor) Comments that will be displayed by help command

31 M/O/F Basics - 10.3 Functions, cont.  Make rms function more robust to work with row or column vector or matrix with column vectors of data  Pseudocode: Test for size of v  if > 2, print error message  else  if row vector  transpose  calculate rms  See rms3.m

32 Vector Dot Product Example X Y Find the X and Y components of the vector, V ivv x ˆ   Back

33 References Matlab. (2009, November 6). In Wikipedia, the free encyclopedia. Retrieved November 6, 2009, from http://en.wikipedia.org/wiki/Matlab http://en.wikipedia.org/wiki/Matlab  Matlab tutorials: http://www.mathworks.com/academia/student_center/tutorials/launchpad.html http://www.mathworks.com/academia/student_center/tutorials/launchpad.html GNU Octave. (2009, October 31). In Wikipedia, the free encyclopedia. Retrieved November 6, 2009, from http://en.wikipedia.org/wiki/GNU_Octave http://en.wikipedia.org/wiki/GNU_Octave  Octave main page: http://www.gnu.org/software/octave/ (http://octave.sourceforge.net/ access to pre-built installers)http://www.gnu.org/software/octave/http://octave.sourceforge.net/  Octave tutorials: http://homepages.nyu.edu/~kpl2/dsts6/octaveTutorial.html, http://smilodon.berkeley.edu/octavetut.pdf http://homepages.nyu.edu/~kpl2/dsts6/octaveTutorial.html http://smilodon.berkeley.edu/octavetut.pdf FreeMat. http://freemat.sourceforge.net/index.htmlhttp://freemat.sourceforge.net/index.html ftp://www.chabotcollege.edu/faculty/bmayer/ChabotEngineeringCour ses/ENGR-25.htm ftp://www.chabotcollege.edu/faculty/bmayer/ChabotEngineeringCour ses/ENGR-25.htm


Download ppt "Lecture 3: M/O/F/ for Engineering Applications B Burlingame 16September2015."

Similar presentations


Ads by Google