上海交通大学交大密西根 ·联合学院一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Exercise compute the value fy=e-asin(x)+l0√, with a=5,x=2,and y=8 Notes: Only use built-in functions on the right hand side of an expression.Reassigning the value to a built-in function can create problems. 8 - There are some exceptions.For example,i and j are pre-assigned tov-1..However,one or both of i or j are often used as loop indices. To avoid any possible confusion,it is suggested to use instead ii or jj as loop indices
Exercise Exercise • compute the value for with a = 5, x = 2, and y = 8 Notes: – Only use built-in functions on the right hand side of an expression. Reassigning the value to a built-in function can create problems. – There are some exceptions. For example, i and j are pre-assigned to . However, one or both of i or j are often used as loop indices. – To avoid any possible confusion, it is suggested to use instead ii or jj as loop indices
上海交通大学交大密西根 联合学院·一 ◆] 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Programming in MatLab Names of variables?Same as ANSI C++ Types of variables andd eclarations? - C++:Many types,MUST declare - MATLAB:1 type(2-D arrays of doubles)only and don't declare ●6 Statements ANSI C++:Terminated by a semicolon MATLAB:Terminated by and end-of-line (carriage return)unless you put a ..at the end of the line to continue. Can also terminate with a which hold the display
Programming in Programming in MatLab • Names of variables? Same as ANSI C++ • Types of variables andd eclarations? – C++: Many types, MUST declare – MATLAB: 1 type (2-D arrays of doubles) only and don’t declare • Statements – ANSI C++: Terminated by a semicolon – MATLAB: Terminated by and end-of-line (carriage return) unless you put a ... at the end of the line to continue. – Can also terminate with a ; which hold the display
上海交通大学交大密西根 B 联合学院·一 ◆] 181t UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Matrix Definition in MatLab zeros:creates and initializes a array with o.o's ones:creates and initializes a array with o.o's rand:creates an array of uniformly distributed random numbers between 0 and 1; 。Direct define >> x=zeros (4,4) >y=ones(1,10) y 0 0 0 0 11111 1 0 0 0 0 0 0 0 0 0 0 0 0
Matrix Definition in Matrix Definition in MatLab • zeros: creates and initializes a array with o.o’s • ones: creates and initializes a array with o.o’s • rand: creates an array of uniformly distributed random numbers between 0 and 1; • Direct define
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Flow Control MATLAB has four control flow OPERATOR DESCRIPTION structures: > Greater than -the if statement, < Less than >= Greater than or equal to -the for loop,t <= Less than or equal to Equal to 181 he while loop,and Not equal to the switch statement. & AND operator ·Logical Operator OR operator NOT operator
Flow Control Flow Control • MATLAB has four control flow structures: – the if statement, – the for loop, t – he while loop, and – the switch statement. • Logical Operator
上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University If Statement The 'if...end"structure if ..end -if ..else...end -if ..elseif...else ..end discr b*b-4*a*c; if discr 0 disp('Warning:discriminant is negative,roots are imaginary'); elseif discr ==0 disp('Discriminant is zero,roots are repeated') else disp('Roots are real') end
If Statement If Statement • The ``if...end'' structure – if ... end – if ... else ... end – if ... elseif ... else ... end