Ch E 310 - Fall 10 - Lecture 4

Ch E 310 - Fall 10 - Lecture 4 - Lecture 4 September 2 2010...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 4 – September 2, 2010 Agenda: Review of delimiters Control structures Boolean expressions If. . Then. . Else. . End structures While loops Matrix math review HW 1 Questions/Help
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MATLAB: Review of Delimiters Parentheses () are used for: Grouping expressions: a = 1 ./ (b .* c .* d .* (e + f)) Assigning values to parts of arrays: A(1,3) = 4 Accessing values from arrays: b = A(2,4) Passing arguments to functions: r = sin(pi/2) Creating a list with the : operator: c = (1:3:10) Square brackets [] are used for: Creating arrays: A = [1 2 3 4] Defining multiple function outputs: function [a b] = manyout(input1,input2) Assigning function outputs: [a b] = multfunc(x1,x2)
Background image of page 2
MATLAB: Review of Delimiters Single quotes ' are used for: Enclosing strings: new_string = ' hello. ' Taking the transpose of an array: b = a ' Double quotes " do not work! Braces {} are used for: Creating cell arrays: CA = { 'string1' 'string2'} Accessing cell arrays: part = CA{1} Ellipsis ... are used for: Continuing a line: function result = . .. my_func(input1,input2)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Semicolon ; is used for: Suppressing output: a = 5; Creating a new array row: B = [1 2; 3 4] Entering more than one command: a = 5; b = 1; c = 6; Colon : is used for: Creating lists: f = [10:-2:2] Selecting out an entire row or column: n = A(2,:) Comma , is used for: Separating arguments: a = my_function(b,c,d) Entering multiple commands: a = 1, b = 2 MATLAB: Review of Delimiters
Background image of page 4
MATLAB: Boolean Operators Boolean operator: an expression with a true or false outcome In MATLAB: true ≡ 1, false ≡ 0 Boolean expressions are constructed from Boolean operators A “relational operator” returns a Boolean result by comparing two numbers: < less than <= less than or equal to > greater than >= greater than or equal to == equal to ~= not equal to Both sides of the operator are evaluated before the comparison
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MATLAB: Relational Operators >> a = .3; b = .1; c = .2; % define variables >> c == 2 * b % test if c equals 2 * b ans = 1 >> a > b % test if a is greater than b ans = 1 >> a < b % test if a is less than b ans = 0 >> a ~= b % test if a is not equal to b ans = 1 >> a - b * 3 == 0 % why doesn’t this work?? ans = 0 Examples:
Background image of page 6
MATLAB: Relational Operators >> a - b * 3 % test if this is zero ans = -5.5511e-017 >> a - b * 3 ~= 0 % should be true ans = 1 If there is a small numerical error, the arithmetic expectation may not match the numerical result. Better construction: >> tol = 1e-15; abs((a - b * 3) < tol) ans = 1 This defines a tolerance accuracy for the comparison
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MATLAB: Logical Operators Logical operator: returns a Boolean result based on the relationship between one or two Boolean expressions ~ NOT AND (see also: ) | OR (see also: || ) Examples (true results): >> a = .3; b = .1; c = .2; % re-define variables >> ~ (a == .2)
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/21/2011 for the course CH E 310 taught by Professor Staff during the Spring '08 term at Iowa State.

Page1 / 30

Ch E 310 - Fall 10 - Lecture 4 - Lecture 4 September 2 2010...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online