Lecture3 - Lecture 3: Matlab Basics Flow control example:...

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

View Full Document Right Arrow Icon
Lecture 3: Matlab Basics •Flow control example: iterative solution of equation •Strings •Making 2-D plots: the plot command •Formatted output •Inline functions •MATLAB scripts •MATLAB script functions •Global variables •Reading from files
Background image of page 1

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

View Full DocumentRight Arrow Icon
Array v.s Matrix Operations: alternative syntax Example: cubing every element of a matrix vs cubing the matrix (assume a square matrix) >> M=[1 2; 3 4]; D=power(M,3), E=mpower(M,3) D = 1 8 E = 37 54 27 64 81 118 Could have also used: M.^3 and M^3
Background image of page 2
Matrix Versions of Functions •How do I issue a sin(M) command that calculates a sine of a square matrix according to the matrix multiplication rules? You have to issue a funm(M,fun) command: >>M = [1 2; 3 4]; A = sin(M) A = 0.8415 0.9093 0.1411 -0.7568 >>B = funm(M,@sin) B = -0.4656 -0.1484 -0.2226 -0.6882
Background image of page 3

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

View Full DocumentRight Arrow Icon
Assignment v.s. relational operators The relational operator “==“ is used to check if x is equal to 4. MATLAB tells you that the statement is FALSE and expresses the logical FALSE as a zero. On the other hand, x==3 is a TRUE statement. To assign a value to a variable: use = >> x = 3 x = 3 >> x == 4 ans = 0 >>x == 3 ans = 1
Background image of page 4
Other relational operators In addition to the operator “==“ there are other relational operators: <, >, <=, >=, ~= (not equal). We will be using these operators a lot in the loops (while, if, etc.). The output of any relational operator is a logical TRUE (1) or FALSE (0). They are often used in conjunction with logical AND (&), OR ( | ), NOT ( ~ ) Here’s how those operators work: >> (3 < 4)&(5<6) ans = 1 >>and(3<4,5>6) ans = 0 >>and(lt(3,4),gt(6,5)) ans = 1
Background image of page 5

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

View Full DocumentRight Arrow Icon
Flow control: if command •This command is often called a conditional. It tells you that something needs to be done when a certain condition is satisfied. •For example, an absolute value of a number is the number itself if it is positive, and minus that number if it is negative. >> x = 4; >> if (x < 0) abs_value = -x; else abs_value = x; end ; >> abs_value abs_value = 4
Background image of page 6
Flow control: if elseif else end command >> x = -4; >> if (x < 0) abs_value = -x; elseif (x==0) abs_value = 0; else abs_value = x; end ; >> abs_value abs_value = 4 The statements after the ELSEIF are executed if the expression is true and all the preceding IF and ELSEIF expressions are false. The statements after the ELSE are executed if all the preceding IF and ELSEIF expressions are false •Not necessary for this example but can be useful in other circumstances as an extra control
Background image of page 7

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

View Full DocumentRight Arrow Icon
Flow control: for loop •There are two types of loops: for and while. For loop repeats the statements in the loop as the loop takes in a given row vector: >> square= [ ]; for i=1:4 square = [square i^2]; end >> square square = 1 4 9 16
Background image of page 8
Flow control: for loop •The row vector need not be integer: >> square= [ ]; for i=0.1:0.1:0.4 square = [square i^2]; end >> square square = 0.01 0.04 0.09 0.16
Background image of page 9

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

View Full DocumentRight Arrow Icon
for loops with conditionals >> for i=1:4 for j=1:4 if (abs(i-j)==1) M(i,j) = -1; elseif (i==j) M(i,j) = 2;
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2011 for the course PHYS 329 taught by Professor Gennady during the Spring '11 term at University of Texas.

Page1 / 41

Lecture3 - Lecture 3: Matlab Basics Flow control example:...

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

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