MatLab Chapter 3

advertisement
Built-In MATLAB Functions
>> sqrt (16)
ans =
4
Square Root is written as sqrt (x)
>> x=9
x=
9
>> sqrt (x)
ans =
3
Try a couple on your own to practice
and “get a feel for it”.
>> x=[4 9 16 25 36]
x=
4 9 16 25 36
>> sqrt (x)
ans =
2 3 4
5
6
Set up a matrices where x = [4, 9, 16, 25, 36]
Then solve for the square root of the entire
matrix
PROBLEM 1 - Create a table to solve for the square root
of all numbers from 1-9
Remember: If you need to
create an evenly spaced
array, there is a short cut of
using a COLON between your
first and last value
>> b=2:5
b=
2 3 4
Remember: that the table requires the (‘) transpose
operator for both the students’ and costs’
>> students=[1:10]
students =
1 2 3 4 5
6
7
8
9 10
>> cost=students.*6
cost =
6 12 18 24 30 36 42 48 54 60
5
>> b=3:8
b=
3 4 5
6
7
8
>> b=[3:8]
b=
3 4 5
6
7
8
>> table=[students', cost']
table =
1 6
2 12
3 18
4 24
5 30
6 36
7 42
8 48
9 54
10 60
PROBLEM 1 - Create a table to solve for the square root
of all numbers from 1-9
Your answer should look something like this below: please use your own variables, not
“numbers” and “squareroots” like in my sample answer below.
>> numbers = 1:9
numbers =
1 2 3 4 5
6
7
8
9
>> squareroots=sqrt(numbers)
squareroots =
1.0000 1.4142 1.7321 2.0000 2.2361 2.4495 2.6458 2.8284 3.0000
>> table=[numbers',squareroots']
table =
1.0000 1.0000
2.0000 1.4142
3.0000 1.7321
4.0000 2.0000
5.0000 2.2361
6.0000 2.4495
7.0000 2.6458
8.0000 2.8284
9.0000 3.0000
Using the HELP features in MATLAB
>> help
HELP topics:
Type in the word “help,” and a
list of help topics will appear
matlab\general
matlab\ops
matlab\lang
matlab\elmat
matlab\elfun
matlab\specfun
matlab\matfun
matlab\datafun
matlab\polyfun
matlab\funfun
matlab\sparfun
matlab\scribe
matlab\graph2d
matlab\graph3d
- General purpose commands.
- Operators and special characters.
- Programming language constructs.
- Elementary matrices and matrix manipulation.
- Elementary math functions.
- Specialized math functions.
- Matrix functions - numerical linear algebra.
- Data analysis and Fourier transforms.
- Interpolation and polynomials.
- Function functions and ODE solvers.
- Sparse matrices.
- Annotation and Plot Editing.
- Two dimensional graphs.
- Three dimensional graphs.
(and many more…)
Click on the topic elfun - Elementary math functions
Using the HELP features in MATLAB
Next, scroll down to select abs - Absolute Value
--At the bottom of the Command Window you should see the following:
ABS Absolute value.
ABS(X) is the absolute value of the elements of X. When
X is complex, ABS(X) is the complex modulus (magnitude) of
the elements of X.
See
also What
sign, is
angle,
unwrap,value
hypot.
Practice:
the absolute
of -17?
Be
careful: the
function
the absolute value is “abs(x)” not “ABS(x)” -- the
Reference
page
in Helpforbrowser
abs
needs
doc
abs to be lower case
>> ABS(-17)
??? Undefined function or method 'ABS' for input arguments of type 'double'.
>> abs(-17)
ans =
17
Using the HELP features in MATLAB
You can also get help on a specific math function by simply entering in the function
For example: If you needed help finding the cosine of (pi). You can enter “help cos” and
the help menu will provide the following:
>> help cos
COS Cosine of argument in radians.
COS(X) is the cosine of the elements of X.
See also acos, cosd.
Reference page in Help browser
doc cos
>> cos (pi)
ans =
-1
Using the HELP features in MATLAB
Practice: What is the natural logarithm of 7?
Hint: logarithm is “log”
Remember to enter in “help log”
>> help log
LOG Natural logarithm.
LOG(X) is the natural logarithm of the elements of X.
Complex results are produced if X is not positive.
>> log 7
??? Undefined function or method 'log' for input arguments of type 'char'.
>> log(7)
ans =
1.9459
Rounding Functions: Imagine that there are 127 people going on a
field trip using passenger vans that can hold twelve passengers
each. How many passenger vans will be needed?
>> 127/12
ans =
10.5833
127 people divided by 12 seats = 10.583 vans needed. But
there is no such thing as a 0.583 sized van. We would need
11 vans – YES?
>>
>> people=127
people =
127
>> seats=12
seats =
12
>> vans=people/seats
vans =
10.5833
Here is another way of approaching the problem.
Rounding Functions: Imagine
are122
127
people
going
on a
Oops, nowthat
therethere
are only
people
going,
which
meanstrip
we would
10.1667vans
vans.that
The can
function
no longer work
field
using need
passenger
holdround(x)
twelvewill
passengers
(we stillHow
need many
11 vans).
So ceil(x)vans
wouldwill
work
– it always rounds up.
each.
passenger
bebest
needed?
>> vans=10.1667
Now try these four functions with
-3.2
and
-3.8
and
see
what
happens
Now lets try rounding our answers using >>vans
vans =
= -3.8]
x=[-3.2,
10.5833
>> round(vans)
ans =
11
a few different types of rounding
functions:
round(x) – Rounds x to the nearest
integer (rounds up or down)
x = 10.1667
-3.2000 -3.8000
round(vans)
>>>>
round(x)
ans
ans
==
-310-4
>> fix(vans)
ans =
10
fix(vans)
fix(x)
fix(x) – Rounds x to the nearest integer >>>>
toward zero ans
ans
==
>> floor(vans)
ans =
10
floor(x) – Rounds x to the nearest
floor(vans)
>>>>
floor(x)
integer toward negative infinity ans
ans
==
(always rounds down - even negatives)
-410-4
>> ceil(vans)
ans =
11
ceil(x) – Rounds x to the nearest
integer toward positive infinity (always rounds up - even negatives)
-310-3
ceil(vans)
>>>>
ceil(x)
ans
ans
==
-311-3
Here are some other functions within MATLAB
To find all of the prime
factors of number use
factor (x)
To show a decimal
number as a fraction
use rats(x)
To find the largest value in a vector
use max(x). To find the smallest
value use min(x)
>> factor(6)
ans =
2 3
>> rats (.75)
ans =
3/4
>> a=[24, 62, 39, 24, 23, 43, 47]
a=
24 62 39 24 23 43 47
>> factor(12)
ans =
2 2 3
>> rats(2.5)
ans =
5/2
>> max(a)
ans =
62
>> factor(30)
ans =
2 3 5
>> rats(7.2)
ans =
36/5
>> min(a)
ans =
23
>>
>>
Statistics: mean(x), median(x) and mode(x)
>> a
a=
24 62 39 24 23 43 47
Lets use the same vector “a” from the
previous slide
>> a=[24, 62, 39, 24, 23, 43, 47]
>> mean(a)
ans =
37.4286
Mean = average of al values in data set
>> median(a)
ans =
39
Median = middle value in data set
>> mode(a)
ans =
24
Mode= most frequently appearing
value in data set
>>
More functions - sort(x), sort(x,’descend’), sum(x), prod(x)
>> a
a=
24 62 39 24 23 43 47
Lets use the same vector “a” from the
previous slide
>> a=[24, 62, 39, 24, 23, 43, 47]
>> sort(a)
sort(x) = sorts the elements of the
ans =
vectors in ascending order (lowest to
23 24 24 39 43 47 62 highest)
sort(x,’descend’) = sorts the elements
>> sort(a,'descend')
of the vectors in descending order
ans =
62 47 43 39 24 24 23 (highest to lowest)
>> sum(a)
ans =
262
sum(x) = sums (adds together) the elements
(24+62+39+24+23+43+47) = 262
>> prod(a)
ans =
6.4740e+010
prod(x) = multiplies all of the elements
together (24*62*39*24*23*43*47)
Problem 2 – An engineer made five measurements of the
impact force of a bat hitting a ball. The measurements
were (17.1, 17.3, 16.9, 17.1, 17.2) kN.
a)
b)
c)
d)
e)
Sort the data from lowest to highest
Calculate the average impact force
Determine the median
Determine the mode
Add up the total amount of impact force
experienced by the bat.
>> f=[17.1, 17.3, 16.9, 17.1, 17.2]
f=
17.1000 17.3000 16.9000 17.1000 17.2000
>> sort(f)
ans =
16.9000 17.1000 17.1000 17.2000 17.3000
>> mean(f)
ans =
17.1200
>> median(f)
ans =
17.1000
>> mode(f)
ans =
17.1000
>> sum(f)
Ans =
85.6000
Problem 3Suppose you needed to determine the amount of
money needed for three math classes to go on a field
trip. The cost of the field trip is $27.
Class A has 3 sophomores, 8 juniors, and 7 seniors
Class B has 11 sophomores, 5 juniors and 2 seniors
Class C has 2 sophomores, 3 juniors and 10 seniors
1)
2)
3)
4)
set up three vectors for class “A”, “B”, and “C”
determine the number of students in each class
Calculate the total number of students in all classes
Calculate the total amount of money needed
>> A=[3, 8, 7]
A=
3 8 7
>> At=sum(A)
At =
18
>> Total=At+Bt+Ct
Total =
51
>> B=[11, 5, 2]
B=
11 5 2
>> Bt=sum(B)
Bt =
18
>> TotalCost=Total*27
TotalCost =
1377
>> C=[2, 3, 10]
C=
2 3 10
>> Ct=sum(C)
Ct =
15
Oops, Class B will not be able
to go on the trip. Now
how much will it cost for
only Class A and C
>> (At+Ct)*27
ans =
891
Nesting: using one function as the input to another function
>> s=[3, 4]
s=
3 4
Remember the Pythagorean theorem? a^2 + b^2 = c^2.
We can use nesting to solve for it. Please follow the steps
at the left for a right triangle with sides of 3 and 4
>> step1=s.^2
step1 =
9 16
Remember that when working with exponents in a vector
we need to use the “dot” for all of the values in the vector
(s.^2)
>> step2=sum(step1)
step2 =
25
>> answer=sqrt(step2)
answer =
5
>> answer=sqrt(sum(s.^2))
answer =
5
Similar to the order of operations, nesting will solve for the
inner most parentheses first, then work out.
Nesting: using one function as the input to another function
>> s=[5,12]
s=
5 12
We can now change the values of our sides. Sides are 5 and 12
>> answer=sqrt(sum(s.^2))
Just drag out the equation from the Command History and
answer =
we just solved the triangle
13
>> s=[1,1]
s=
1 1
>> answer=sqrt(sum(s.^2))
answer =
1.4142
This time a triangle with sides of 1 and 1
Problem 4 – Who one the basketball game?
Below is a break down of the points scored by
each of the five players from the two teams
Team A
Team B
P1 P2 P3 P4 P5
9 2 8 7 6
13 5 3 4 4
For each team:
1) Determine the minimum points scored
2) Determine the maximum points scored
3) Sort the amount of points scored for
4) Calculate the average points scored for each
team
5) Add up the total points scored for each team
>> A=[9,2,8,7,6]
A=
9 2 8 7
6
>> B=[13,5,3,4,4]
B=
13 5 3 4 4
>> min(A), min(B)
ans =
2
ans =
3
>> max(A), max(B)
ans =
9
ans =
13
>> sort(A), sort(B)
ans =
2 6 7 8 9
ans =
3 4 4 5 13
>> mean(A), mean(B)
ans =
6.4000
ans =
5.8000
>> sum(A), sum(B)
ans =
32
ans =
29
Download