Electrical and Computer Engineering Department SUNY – New Paltz Computer Simulation “Lecture 9” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz
Plots a bar-graph of a vector bar(v) Plots a bar-graph of a vector SUNY-New Paltz
Exercise Use bar() function to display a vector of prime numbers (1 thru 43). Hint: use primes() function. SUNY-New Paltz
Exercise In the previous exercise, use a for loop that will print the value of each bar on the top it. SUNY-New Paltz
Exercise Toss a coin N times (user selectable). Store the number of tails in x(1) and the number of heads in x(2). Then use bar(x) to compare them together. SUNY-New Paltz
Exercise Roll a die N times (user selectable). Store the number of ones in x(1) and the number of two’s in x(2) and so on. Then use bar(x) to compare them together. SUNY-New Paltz
Frequencies, bar charts and histograms A random walk Imagine an ant walking along a straight line, e.g. the x-axis. She starts at x = 40. She moves in Steps of one unit along the line. Each step is to the left or the right with equal probability. We would like a visual representation of how much time she spends at each position. SUNY-New Paltz
MATLAB Code bar(f) f = zeros(1,100); x = 40; for i = 1:1000 r = rand; if r >= 0.5 x = x + 1; else x = x - 1; end f(x) = f(x) + 1; End bar(f) SUNY-New Paltz
hist() What is a histogram? his·to·gram, ˈhistəˌɡram/ Noun, STATISTICS a diagram consisting of rectangles whose area is proportional to the frequency of a variable and whose width is equal to the class interval. SUNY-New Paltz
Exercise Use vectorization and hist() function to repeat the exercises on flipping a coin and rolling a die. Increase the number of trials to 1000, 10,000 and 1,000,000. SUNY-New Paltz
Exercise Generate row vector (128 elements) of random numbers between 0 and 1 and use imshow() to visualize it. Then use hist() function to see the distribution. Repeat the above for vector sizes 256, 512 and 1024 and draw a conclusion regarding the size of the vector. SUNY-New Paltz
Arrays of characters: strings Assignment: s = ’Hi there’; s = ’o’’clock’; Input: name = input(‘Enter Your Name:’) Enter Your Name: ’myname’ name = input(‘Enter Your Name:’,’s’) Enter Your Name: myname SUNY-New Paltz
Strings are arrays s = ’Napoleon’ s(8:-1:1) Concatenation of strings king = ’Henry’; king = [king, ’ VIII’] SUNY-New Paltz
ASCII Table SUNY-New Paltz
ASCII code Exercise Decode from ASCII to string: 69 71 69 45 51 51 49 What are the ASCII codes for: Carriage Return Linefeed SUNY-New Paltz
ASCII codes,double and char double(’Napoleon’) 78 97 112 111 108 101 111 110 char(65:70) ABCDEF x = char(ones(4,20)*double(’#’)) #################### SUNY-New Paltz
Exercise Write a program that prints all ASCII codes along with their corresponding characters in a single column. Show the above table in a multi-column form. SUNY-New Paltz
ASCII codes,double and char s = ’a’ s+1 char(s+1) alpha = double(’a’):double(’z’) SUNY-New Paltz
Comparing strings s1 = ’ann’; s2 = ’ban’; s1 < s2 1 0 0 1 0 0 SUNY-New Paltz
Other string functions blanks generates a string of blanks. deblank removes trailing blanks from a string. int2str, num2str convert their numeric arguments to strings. ischar returns 1 if its argument is a string, and 0 otherwise. lower, upper convert strings to lower- and uppercase respectively SUNY-New Paltz
Exercise Prompt the user to enter his/her name: First name separated from the last name by a blank. Print out the a string starting with the last name separated from the first name by a coma. Capitalize the first characters of the first and last names. SUNY-New Paltz
Matlab Code s = input('Enter Your Name(John Doe):','s') for i=1:length(s) if s(i) == ' ' s(1) = upper(s(1)); s(i+1)=upper(s(i+1)); s1=[s(i+1:length(s)),', ',s(1:i-1)] break; end SUNY-New Paltz
fprintf, fprintf( ’% 8sVIII\n’, ’Henry’ ) ؙؙؙHenryVIII Henry ؙؙؙ VIII HenVIII SUNY-New Paltz
sprintf Value of K: .015 K=.015 s = sprintf( ’Value of K: %g’, K ) SUNY-New Paltz
Two-dimensional strings nameAndAddress = [’Adam B Carr ’; ’21 Barkly Ave’; ’Discovery ’] nameAndAddress = Adam B Carr 21 Barkly Ave Discovery nameAndAddress = char(’Adam B Carr’, ’21 Barkly Ave’, ’Discovery’) double(nameAndAddress(1,12)) SUNY-New Paltz
Eval and text macros s = ’x = -b/(2*a);’ eval(s) x = -b/(2*a); Have the user enter a matlab function, such as sin(x), cos(x), etc. Your program should plot that function. SUNY-New Paltz
Code f = input( ’Enter function (of x) to be plotted: ’, ’s’ ); plot(x, eval(f)),grid Enter function (of x) to be plotted: exp(-0.5*x) .* sin(x) SUNY-New Paltz