



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Objectives: Start MATLAB, declare some variables, do some calculations, ... Exponential (scientific) notation is denoted by e. e-001 means 10 -001.
Typology: Exercises
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Symbol Meaning » (^) command line prompt
%
comment operator ignore everything after this appears until next line
=
assignment operator assign value on rhs to variable name on lhs
;
suppress display operator suppress display of all values assigned by statement
Order of Precedence Symbol^ Meaning 1 ( )^ group together 2 ^^ exponentiation ***** (^) multiplication 3^ /^ right division ** (^) left division + (^) addition (^4) - (^) subtraction
MATLAB Command Display Comment format short (default) 27.1828 (^) 4 decimal places format long 27.18281828459045 (^) 14 decimal places format short e 2.7183e+01 (^) 5 signif. figures format long e 2.718281828459045e+01 (^) 16 signif. figures Note : MATLAB rounds appropriately for display Note : If value is a whole number (an integer), MATLAB displays an integer
Name Trig Inverse Trig
Hyperbolic Inverse Hyperbolic sine sin(x)^ asin(x)^ sinh(x)^ asinh(x) cosine cos(x)^ acos(x)^ cosh(x)^ acosh(x) tangent tan(x)^ atan(x)^ tanh(x)^ atanh(x) cotangent cot(x)^ acot(x)^ coth(x)^ acoth(x) secant sec(x)^ asec(x)^ sech(x)^ asech(x) cosecant csc(x)^ acsc(x)^ csch(x)^ acsch(x)
ex^ exp(x) 10 x^ 10^x natural logarithm
log(x)
common (base 10) logarithm
log10(x)
square root sqrt(x)
round up ceil(x) round down floor(x) round toward zero
fix(x)
round to nearest integer
round(x)
real part real(x) imaginary part
imag(x)
complex conjugate
conj(x)
absolute value
abs(x)
remainder after division
rem(x,y)
greatest common divisor
gcd(x,y) (^) modulus mod(x,y)
least common multiple
lcm(x,y) sign of value
sign(x)
MATLAB Name Meaning pi (^) π (=3.14159...)
inf (^) ∞ (infinity) eps (^) machine (computer) precision realmax (^) largest real number realmin (^) smallest real number
NaN
“not a number” occurs when values not defined for some reason
Now enter the following at the Command Line prompt » 8
ans = 0.
This is “left” division (the denominator is on the left of the divisor symbol). Although this is not used much for scalar algebra as shown here, it is used for vector and matrix algebra. The statement is read as “three divided by eight.”
Note : regardless of whether right or left division, the numerator always appears “above” or “over” the slash and the denominator always appears “below” or “under” the slash.
(3) MATLAB has rules for evaluating arithmetic expressions.
For example, enter the following at the Command Line prompt » 3+3/82^33/2- ans = 6.**
MATLAB evaluates expressions by the order of precedence rules. It will work from left to right, executing the highest order of precedence first. Then working left to right, it will evaluate the next order of precedence, etc. In the above arithmetic expression, MATLAB will evaluate the exponentiation first 3+3/82^33/2-1** → 3+3/883/2-** Next, MATLAB will work left to right evaluating the divisions and multiplications 3+3/883/2-1** → 3+0.37583/2- 3+0.37583/2-1** → 3+33/2- 3+33/2-1** → 3+9/2- 3+9/2-1 → 3+4.5- Finally, MATLAB works left to right evaluating the additions and subtractions 3+4.5-1 → 7.5-1 → 6.
You can control the execution of expressions by using parentheses, ( ) , to override the order of precedence rules. MATLAB will always execute inside parentheses (by the order of precedence rules) before excuting outside the parentheses.
For example, enter the following at the Command Line prompt » 3+3/82^(33)/2- ans = 98 Why?**
For example, enter the following at the Command Line prompt » 3+3/82^(33/2)- ans =
Why?**
For example, enter the following at the Command Line prompt » (3+3/82)^(33/2)- ans = 381.**
Why?
(4) MATLAB uses (user-defined) variable names to capture the results of calculations.
You can choose to capture the results of any calculation in a variable name of your choice. For example, enter the following at the Command Line prompt » myname = 4- myname = 1
MATLAB performed the calculation and, since the assignment operator, = , was used to assign the value to the variable named on the left, MATLAB assigned the result to myname before displaying the answer.
(5) Using variable names allows for series of assignments and computations.
For example, enter the following at the Command Line prompt » % calculate the area of a circle » radius = 5; » area = piradius^ area = 78.*
MATLAB ignored the first line because it started with the comment operator, %. Note that MATLAB highlights comments in green text. MATLAB also suppressed the output from the assignment in second line because the assignment statement was terminated by the suppress display operator, ;. Finally, MATLAB perfomed the calculation requested by the third line and displayed the result.
Note that the third line is an assignment statement. The right hand side of the assignment operator is “computer language” that is shorthand for the following commands (following the order of precedence rules) Go get the value assigned to radius (in this case 5) Raise that value to the power 2 Go get the value from the MATLAB-defined function, pi Multiply the two values together Assign the result to area
(6) MATLAB has pre-defined functions for more complex computations.
For example, enter the following at the Command Line prompt » % computing the sine of an angle » angle_deg = 30; » sin_deg = sin(angle_deg) sin_deg = -0.
This result is obviously a problem since we know from trigonometry that the sine of 30° is 0.5. What happened? Whenever we use MATLAB functions, we need to know what values are required by the function and what units the values must have.
Trig functions are in radians (not degrees). Using an angle in degrees will produce a wrong result.
√x can be represented by sqrt(x) or x^0.5 in MATLAB
1
ex^ is written as exp(x), 10x^ is written as 10^x, ln(x) is written as log(x), and log 10 (x) is written as log10(x) in MATLAB.
MATLAB trig functions are provided at the start of this Workshop. Remember: angle measurements are in radians!!