Built-In MATLAB Functions

# Built-In MATLAB Functions

Télécharger la présentation

## Built-In MATLAB Functions

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Built-In MATLAB Functions >> sqrt (16) ans= 4 >> x=9 x = 9 >> sqrt (x) ans = 3 Square Root is written as sqrt (x) 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

2. 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 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 >> table=[students', cost'] table = 1 6 2 12 3 18 4 24 5 30 6 36 7 42 8 48 9 54 10 60 >> b=2:5 b = 2 3 4 5 >> b=3:8 b = 3 4 5 6 7 8 >> b=[3:8] b = 3 4 5 6 7 8

3. 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 >>

4. Using the HELP features in MATLAB >> help HELP topics: matlab\general - General purpose commands. matlab\ops - Operators and special characters. matlab\lang - Programming language constructs. matlab\elmat - Elementary matrices and matrix manipulation. matlab\elfun - Elementary math functions. matlab\specfun - Specialized math functions. matlab\matfun - Matrix functions - numerical linear algebra. matlab\datafun - Data analysis and Fourier transforms. matlab\polyfun - Interpolation and polynomials. matlab\funfun - Function functions and ODE solvers. matlab\sparfun - Sparse matrices. matlab\scribe - Annotation and Plot Editing. matlab\graph2d - Two dimensional graphs. matlab\graph3d - Three dimensional graphs. (and many more…) Type in the word “help,” and a list of help topics will appear Click on the topic elfun - Elementary math functions

5. 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 sign, angle, unwrap, hypot. Reference page in Help browser doc abs >> Practice: What is the absolute value of -17? Be careful: the function for the absolute value is “abs(x)” not “ABS(x)” -- the abs needs to be lower case >> ABS(-17) ??? Undefined function or method 'ABS' for input arguments of type 'double'. >> abs(-17) ans= 17 >>

6. 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

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

8. 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.

9. 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? Now try these four functions with -3.2 and -3.8 and see what happens Rounding Functions: Oops, now there are only 122 people going, which means we would need 10.1667 vans. The function round(x) will no longer work (we still need 11 vans). So ceil(x) would work best – it always rounds up. >> vans=10.1667 vans = 10.1667 >> round(vans) ans = 10 >> fix(vans) ans = 10 >> floor(vans) ans = 10 >> ceil(vans) ans = 11 vans = 10.5833 >> round(vans) ans = 11 >> fix(vans) ans = 10 >> floor(vans) ans = 10 >> ceil(vans) ans = 11 >> x=[-3.2, -3.8] x = -3.2000 -3.8000 >> round(x) ans = -3 -4 >> fix(x) ans = -3 -3 >> floor(x) ans = -4 -4 >> ceil(x) ans = -3 -3 Now lets try rounding our answers using a few different types of rounding functions: round(x) – Rounds x to the nearest integer (rounds up or down) fix(x) – Rounds x to the nearest integer toward zero - floor(x) – Rounds x to the nearest integer toward negative infinity - (always rounds down - even negatives) ceil(x) – Rounds x to the nearest integer toward positive infinity - (always rounds up - even negatives)

10. 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 >> factor(12) ans = 2 2 3 >> factor(30) ans = 2 3 5 >> >> rats (.75) ans = 3/4 >> rats(2.5) ans = 5/2 >> rats(7.2) ans = 36/5 >> >> a=[24, 62, 39, 24, 23, 43, 47] a = 24 62 39 24 23 43 47 >> max(a) ans = 62 >> min(a) ans = 23

11. Statistics: mean(x), median(x) and mode(x) >> a a = 24 62 39 24 23 43 47 >> mean(a) ans = 37.4286 >> median(a) ans = 39 >> mode(a) ans = 24 >> Lets use the same vector “a” from the previous slide >> a=[24, 62, 39, 24, 23, 43, 47] Mean = average of al values in data set Median = middle value in data set Mode= most frequently appearing value in data set

12. More functions - sort(x), sort(x,’descend’), sum(x), prod(x) Lets use the same vector “a” from the previous slide >> a=[24, 62, 39, 24, 23, 43, 47] >> a a = 24 62 39 24 23 43 47 >> sort(a) ans = 23 24 24 39 43 47 62 >> sort(a,'descend') ans = 62 47 43 39 24 24 23 >> sum(a) ans = 262 >> prod(a) ans = 6.4740e+010 sort(x) = sorts the elements of the vectors in ascending order (lowest to highest) sort(x,’descend’) = sorts the elements of the vectors in descending order (highest to lowest) sum(x) = sums (adds together) the elements (24+62+39+24+23+43+47) = 262 prod(x) = multiplies all of the elements together (24*62*39*24*23*43*47)

13. 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. 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. >> mean(f) ans = 17.1200 >> median(f) ans = 17.1000 >> mode(f) ans = 17.1000 >> sum(f) Ans = 85.6000 >> 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

14. Problem 3- Suppose 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 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 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 >> B=[11, 5, 2] B = 11 5 2 >> C=[2, 3, 10] C = 2 3 10 >> At=sum(A) At = 18 >> Bt=sum(B) Bt = 18 >> Ct=sum(C) Ct = 15 >> Total=At+Bt+Ct Total = 51 >> TotalCost=Total*27 TotalCost= 1377

15. Nesting: using one function as the input to another function >> s=[3, 4] s = 3 4 >> step1=s.^2 step1 = 9 16 >> step2=sum(step1) step2 = 25 >> answer=sqrt(step2) answer = 5 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 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) >> 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.

16. Nesting: using one function as the input to another function >> s=[5,12] s = 5 12 >> answer=sqrt(sum(s.^2)) answer = 13 We can now change the values of our sides. Sides are 5 and 12 Just drag out the equation from the Command History and we just solved the triangle >> s=[1,1] s = 1 1 >> answer=sqrt(sum(s.^2)) answer = 1.4142 This time a triangle with sides of 1 and 1

17. Problem 4 – Who one the basketball game? >> 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 Below is a break down of the points scored by each of the five players from the two teams P1 P2 P3 P4 P5 Team A 9 2 8 7 6 Team B 13 5 3 4 4 For each team: Determine the minimum points scored Determine the maximum points scored Sort the amount of points scored for Calculate the average points scored for each team 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