Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Simulation Lab

Similar presentations


Presentation on theme: "Computer Simulation Lab"— Presentation transcript:

1 Computer Simulation Lab
“Lecture 5” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz

2 MATLAB functions Mathematical Function Trigonometric Functions
V = sqrt( (u * cos(a))^2 + (u * sin(a) - g * t)^2 ); Trigonometric Functions SUNY-New Paltz

3 MATLAB functions Utility Functions d = clock();
disp(‘This year is: ‘, num2str(d(1)) Utility Functions SUNY-New Paltz

4 Trigonometric Functions
sin(x) sine of x cos(x) cosine of x. tan(x) tangent of x. cot(x) cotangent of x. asin(x) arc sine (inverse sine) of x between −π/2 and π/2. acos(x) arc cosine (inverse cosine) of x between 0 and π. atan(x) arc tangent of x between −π/2 and π/2. atan2(y, x) arc tangent of y/x between −π and π. sinh(x) hyperbolic sine of x cosh(x) hyperbolic cosine of x, tanh(x) hyperbolic tangent of x.. asinh(x) inverse hyperbolic sine of x, i.e. ln(x + √x2 + 1). acosh(x) inverse hyperbolic cosine of x, i.e. ln(x + √x2 − 1) atanh(x) inverse hyperbolic tangent of x, i.e.1/2 ln[(1 + x)/(1 − x)]. sec(x) secant of x. csc(x) cosecant of x. SUNY-New Paltz

5 Trigonometric Functions (Exercises)
Use the command window to plot a sin(t) for t between -4*pi to + 4*pi. Write a program to prompt the user to enter A and B (between 0 and 1) and then the program plots y(t) = A*sin(t) + B*sin(t), where t is between -2*pi and +2*pi. SUNY-New Paltz

6 Mathematical Functions
abs(x) absolute value of x. log(x) natural logarithm of x. log10(x) base 10 logarithm of x. pow2(x) 2x exp(x) value of the exponential function ex rand pseudo-random number in the interval [0, 1). realmax largest positive floating point number on your computer. realmin smallest positive floating point number on your computer rem(x,y) remainder when x is divided by y, e.g. rem(19, 5) returns 4 (5 goes 3 times into 19, remainder 4). max(x) maximum element of vector x. mean(x) mean value of elements of vector x. min(x) minimum element of vector x. cumsum(x) cumulative sum of the elements of x, e.g. cumsum(1:4) returns [ ] prod(x) product of the elements of x ones(m, n) generates an n by m matrix of 1’s zeros(m, n) generates an n by m matrix of 0’s SUNY-New Paltz

7 Mathematical Functions (Exercises)
Using the function pow2(x), write a program to list the powers of 2 from 0 to 16: SUNY-New Paltz

8 Mathematical Functions (Exercises)
  v SUNY-New Paltz

9 Mathematical Functions (Exercises)
Use the mean() function to prove that the average of random numbers generated by the rand() is .5. Hint: generate a very large vector made of randomly generated numbers. SUNY-New Paltz

10 Mathematical Functions (Exercises)
Write a program to use a for loop to evaluate the average of 100, 1000, 10,000 and 100,00 randomly generated numbers. In each iteration, it should print: the average of xxx numbers is yyy. Hint: your index of the for loop should go through the vector [100, 1000, 10000, 10000]. SUNY-New Paltz

11 Mathematical Functions (Exercises)
Use a single short statement and the prod(x) function to find the factorial of a number . e.g., 5! Then write a short program to prompt the user to enter a number. The program should print, for example: (5)! = 120 SUNY-New Paltz

12 Mathematical Functions (Exercises)
Use the ones() and zeros() function to generate a row vector of size 100 that has values of 50-1’s and 50-0’s; i.e. [ … ] Use the ones() and zeros() function to generate a row vector of size 90 that has values of 30-2’s, 30-1’s and 30-0’s; i.e. [ … … ] SUNY-New Paltz

13 Utility Functions clock time and date in a six-element vector, e.g. the statements Date date in a string in dd-mmm-yyyy format, e.g. 02-Feb-2001, which is thankfully Y2K compliant! floor(x) largest integer not exceeding x, i.e. rounds down to nearest integer, e.g. floor(-3.9) returns -4, floor(3.9) returns 3 ceil(x) smallest integer which exceeds x, i.e. rounds up to nearest integer, e.g. ceil(-3.9) returns -3, ceil(3.9) returns 4 fix(x) rounds to the nearest integer towards zero, e.g. fix(-3.9) returns - 3, fix(3.9) returns 3 round(x) rounds to the integer nearest to x, e.g. round(4.49) returns 4, round(4.5) returns 5 plot(x,y) plot y versus x length(x) number of elements of vector x size(a) number of rows and columns of matrix a. sort(x) sorts elements of vector x into ascending order (by columns if x is a matrix). sign(x) returns -1, 0 or 1 depending on whether x is negative, zero or positive. SUNY-New Paltz

14 Utility Functions (Exercises)
Use the floor() and rand() functions to generate 100 integers between 0 and 9. Use the ceil() and rand() functions to generate 100 integers between 1 and 10. SUNY-New Paltz

15 Utility Functions (Exercises)
Use the clock() function to print: 3:05:42 SUNY-New Paltz

16 Utility Functions (Exercises)
Write a program to prompt the user to enter vector1 and vector2. If the vector have the same lengths, the print the sum and difference of them. Otherwise, print: The vectors must be of the same size. Hint: use length() function. SUNY-New Paltz


Download ppt "Computer Simulation Lab"

Similar presentations


Ads by Google