MATLAB Workshop 1, Exercises of Elementary Mathematics

Objectives: Start MATLAB, declare some variables, do some calculations, ... Exponential (scientific) notation is denoted by e. e-001 means 10 -001.

Typology: Exercises

2022/2023

Uploaded on 03/01/2023

tanvir
tanvir 🇺🇸

5

(4)

224 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB: Workshop 1 - Numbers and Arithmetic page 1
MATLAB Workshop 1 - Numbers and Arithmetic
Objectives: Start MATLAB, declare some variables, do some calculations, use some basic functions,
change display format, quit MATLAB.
MATLAB Features:
basic MATLAB symbols
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 rules for arithmetic operators
Order of
Precedence Symbol Meaning
1 () group together
2 ^exponentiation
*multiplication
/right division
3 \left division
+addition
4 -subtraction
display formats
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
angle functions
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)
pf3
pf4
pf5

Partial preview of the text

Download MATLAB Workshop 1 and more Exercises Elementary Mathematics in PDF only on Docsity!

MATLAB Workshop 1 - Numbers and Arithmetic

Objectives : Start MATLAB, declare some variables, do some calculations, use some basic functions,

change display format, quit MATLAB.

MATLAB Features :

basic MATLAB symbols

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 rules for arithmetic operators

Order of Precedence Symbol^ Meaning 1 ( )^ group together 2 ^^ exponentiation ***** (^) multiplication 3^ /^ right division ** (^) left division + (^) addition (^4) - (^) subtraction

display formats

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

angle functions

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)

exponentiation/logarithmic functions

ex^ exp(x) 10 x^ 10^x natural logarithm

log(x)

common (base 10) logarithm

log10(x)

square root sqrt(x)

rounding functions

round up ceil(x) round down floor(x) round toward zero

fix(x)

round to nearest integer

round(x)

imaginary number functions

real part real(x) imaginary part

imag(x)

complex conjugate

conj(x)

miscellaneous functions

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 constants

MATLAB Name Meaning pi (^) π (=3.14159...)

i or j imaginary unit ( − 1 )

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-13+4.5- Finally, MATLAB works left to right evaluating the additions and subtractions 3+4.5-17.5-16.

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.

b.

√x can be represented by sqrt(x) or x^0.5 in MATLAB

c. Volume = 3

π radius with radius = 5 1

1

π − π is pi in MATLAB.

2. Exponential and logarithmic expressions. Evaluate each of the following. Display in 5-digit

scientific notation.

a. e^5 , ln(e^5 ), log 10 (e^5 ), and log 10 (10^5 )

b. eπ√^33

c. Solve 5x^ = 23. (Note, by taking logarithms, the solution is x =

ln( 5 )

ln( 23 )

. Compute this and

verify that it is the correct answer by direct substitution into the original equation. What

happens if you use base 10 logarithms instead of natural logarithms?

3. Trigonometric functions. Evaluate each of the following. Display in 16-digit scientific notation.

a. ÷

ø

ö

ç

è

æ

sin

, cos(π), tan(45°)

b. ÷

ø

ö

ç

è

æ

÷^ +

ø

ö

ç

è

æ

cos

sin 2 2

(What do you think the result should be? Why?)

c. Solve t^2 =cos 2 (ω ) −sin^2 (ω )with ω = 1. 5 π.

4. Machine limits. Evaluate each of the following. Display in 15-digit scientific notation.

a. » max = realmax greatest magnitude real value permitted

b. » min = realmin least magnitude real value permitted

c. » precision = eps machine accuracy (significant digits)

  • Recap : You should have learned
    • How to start MATLAB and set the current directory
    • How to do simple arithmetic and function calculations
    • How to use the order of precedence rules to control calculations
    • How to assign values to variables
    • How to control screen display (no display and appearance of floating point numbers)
    • Limits of machine precision, size of allowable numbers

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!!