MATLAB Chapter 3 Notes Homework and Assignment
Many of the built-in functions in MATLAB are similar to the cmath functions in C++.
One of the functions
rem(27, 7) produces remainder like the modulus operator % in C++, for example 27%7.
rand('seed', 9) %You use a different number here, such as the number of your birthday. Type this in command window before writing code in the .m file.
1. Uniformly distributed numbers with in the given range of 1-50
Try this:
r = fix(rand(50,1)*12+1)
Here we are using fix() on the results to get integers in the output.
We are asking for 50 double numbers between but not including 0 and 1.
We multiply by 12 to get numbers between but not including 0 and 12.
We add 1 to push the results up one to include 1 and 12.
2. Gausian Random Numbers- a normal distribution of the random numbers which will form a normal or bell-shaped curve on a graph. The mean and standard deviation must be specified in the statement.
Just a quick note about variance and Standard deviation. In looking at data, such as test scores, we need to examine the mean and the highest and lowest scores to understand how scores are vary( variance) from the mean(average).there is a formula on p. 89 of your text for defining the variance. The standard deviation is the square root of the variance.
Now try this:
rn = sort(fix(randn(1, 50)*2.5+3))
We are asking for one row of 50 double numbers with no upper or lower limits that will form a normal curve.
We multiply by 2.5 to establish the standard deviation.
At the end we add 3 to establish the mean.
What happens if we change the +3 to +70 or + 80 or +50 ? Try it out and see. Looking at the larger data examine what happens if we change the *2.5 to 1.5 and then to..9.5.
Examine and discuss functions in:
Table 3.1 ..... p. 68 ..................... Common Math Functions
...............................................(abs(x), sqrt(x), nthroot(x,n), sign(x), rem(x,y), exp(x), log(x), log10(x))
Table 3.2 ..... p. 73 ..................... Rounding Functions ...(round(x), fix(x), floor(x), ceil(x))
Table 3.3 ..... p. 74 ..................... Functions Used in Discrete Mathematics
...............................................(factor(x), gcd(x,y), lcm(x,y), rats(x), factorial(x), nchoosek(n,k), primes(x), isprime(x))
Table 3.4 ..... p. 76 ..................... Trigonometric Functions
...............................................(sin(x), cos(x), tan(x), asin(x), sinh(x), asinh(x), sind(x), asind(x))
Table 3.5 ..... pp. 80 & 81........ Maxima and Minima
...............................................(max(x), [a,b]=max(x), max(x,y), min(x), [a,b]=min(x), min(x,y))
Table 3.6 ..... p. 83 ..................... Averages ...(mean(x), median(x), mode(x))
Table 3.7 ..... p. 84 ..................... Sums and Products ...(sum(x), prod(x), cumsum(x), cumprod(x))
Table 3.8 ..... p. 86 ..................... Sorting Functions ...(sort(x), sort(x, 'descend'), sortrows(x), sortrows(x,n))
Table 3.10 .. p. 88 ..................... Size Functions ...(size(x), [a,b] = size(x), length(x))
Table 3.12 .. p. 96 ..................... Statistical Functions ...(std(x), var(x))
Table 3.13 .. p. 101 ..................... Random Number Generators...( See above.)
Table 3.14 .. p. 107 .................. Functions Used With Complex Numbers
...............................................(abs(x), angle(x), complex(x,y), real(x), imag(x), isreal(x), conj(x))
Table 3.15 .. p. 108 .................. Computational Limits (realmax, realmin, intmax, intmin)
Table 3.16 .. p. 110 .................. Special Functions ...(pi, i, j, inf, NaN, clock, date, esp)
In MATLAB type >> help to choose links to examine these and more functions in greater detail.
Homework and Assignment :
Homework:
Read Chapter 3.
There are eleven Practice Exercises. Pick eight Practice Exercises that are the most meaningful to you based on your interests and/or your major. Be sure to do any that are relevant to courses you are now taking and any that you think they may be relavent to you in the future. You can do all the Practice Exercises if you think they are all important to you. Save this work in one .m file showing output.
Points: 2 points. Each exercise is worth 1/4 point. You can earn 1/4 point for each for doing more than eight of the exercises. Since there are eleven exercises, you can earn as much as 3/4 point extra credit.
You will have some time to start this in class. Finish it before coming to the next class.
When you come to the next class, the first thing you will do is check your code and output with your neighbors and other classmates. If you have different output work it out to see who has the correct output and why, then check it out with other classmates to see if you have uniform agreement about the correct output. If not, why?
Assignment: Range of Temperatures Over 40 days Points: 6 points
1. Using Matlab randn(m,n) function, create a 1 by 40 matrix of random Gaussian values with a standard deviation 1.5 and a mean of 70. Use command format bank to get 2 decimal places. This represents a temperature distribution.
For help on randn, Matlab’s >>help randn.
2. Calculate the following statistics using Matlab’s predefined functions:
*mean *median *variance var(x) *standard deviation std(x)
3. Plot the temperature over the range of days. Title the graph and label the axes suitably.
Insert the following text with values next to the following points of the temperature curve:
Maximum temperature = value Minimum temperature = value
4. Type hold on and create another set of Gaussian random numbers, with a std dev of 6.5 and a mean of 70. Plot those numbers on the same graph using a different line type and data point symbols.
5. Record your observations as comments on the .m file. Explain in what way and why the two lines are different.
| Bring in your C++ textbook for next class, so we can compare C++ code to the MATLAB functions for manipulating arrays (vectors) and 2 dimensional arrays (matrices). |
|