________________________________________________________________________
,C=
________________________________________________________________________
3. Given the matrix A=[2 4 1; 6 7 2; 3 5 9] provide the commands needed to:
Note: the standard error of the mean is defined as the standard deviation divided
by the square root of the number of elements used to compute the mean.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
7. Plot the function
________________________________________________________________________
________________________________________________________________________
9. Write a MATLAB script that will use the random number generator “rand” to
determine the following:
a) The number of random numbers it takes before a number between 0.8 and 0.85
occurs.
c) The number of random numbers it takes before the mean of those numbers is
within 0.01 of 0.5.
d) For parts a to c, write a script that repeats these computations 1000 times and
draws a histogram of the results.
________________________________________________________________________
10. Consider a 5x5 matrix. Write a Function in Matlab to:
a) Find the sum of all elements of each column.
b) Find the sum of all elements of each row.
c) Find the sum all elements on the principal and secondary diagonals.
________________________________________________________________________
11. Write a Matlab script that will print the following multiplication table:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
________________________________________________________________________
12. Write a Matlab script that will continue prompting the user for positive
numbers, and storing them in a vector variable, until the user types in a negative
number. Enter 11 positive elements in the vector and sort the elements in
descending order.
________________________________________________________________________
13. Write a Matlab script that plots sin(x) and cos(x) in the same Figure Window
for values of x ranging from 0 to 2*pi. Use proper labeling.
________________________________________________________________________
14. Write a Matlab function prodby2.m that will receive a value of a positive integer n
and will calculate and return the product of the odd integers from 1 to n (or from 1
to n – 1 if n is even). Also return the product of the even integers from 2 to n (or
from 2 to n-1 if n is odd).
________________________________________________________________________
15. Write a Matlab script that will load data from a file (datafile or text file) into a
matrix. Create the data file first (with at least 15 elements), and make sure that
there is the same number of values on every line in the file so that it can be loaded
into a matrix. Using a for loop, it will then create as many Figure Windows as
there are rows in the matrix, and will plot the numbers from each row in a
separate Figure Window.
________________________________________________________________________
16. Write a Matlab script to create a 3 x 5 matrix. Perform each of the following using
loops (with if statements if necessary):
________________________________________________________________________
17. Write a Matlab script to create a vector of five random integers, each in the range
from –10 to 10. Perform each of the following using loops (with if statements if
necessary):
________________________________________________________________________
Write a recursive function to implement this definition. The function will receive
one integer argument n, and it will return one integer value that is the nth
Fibonacci number. Note that in this definition there is one general case but two
base cases.
Then, test the function by printing the first 20 Fibonacci numbers.
________________________________________________________________________
19(a) Write a Matlab function sumsteps2.m that calculates and returns the sum of 1 to n
in steps of 2, where n is an argument passed to the function. For example, if 11 is
passed, it will return 1 + 3 + 5 + 7 + 9 + 11. Do this using a for loop. Calling the
function will look like this:
>> sumsteps2(11)
ans =
36
19(b) Make necessary changes in the function sumsteps2.m to find the result of the
following 1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 -10 + 11 - 12 + 13 - 14 + 15 where
n = 15 is passed as parameter to the function.
________________________________________________________________________
20. Write a script beautyofmath.m that produces the following output. The script
should iterate from 1 to 9 to produce the expressions on the left, perform the
specified operation to get the results shown on the right, and print exactly in the
format shown below
>> beautyofmath
1x8+1=9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
________________________________________________________________________
21. Write a script using for loop that will print the elements from a vector variable in
sentence format. For example, if the following is the vector:
vec = [5.5 11 3.45];
After execution of the script, the following would be the result:
Element 1 is 5.50.
Element 2 is 11.00.
Element 3 is 3.45.
The for loop should work regardless of how many elements are in the vector.
________________________________________________________________________
22. Write a function that will receive the radius r of a sphere. It will calculate and
return the volume of the sphere (4/3 * pi * r3).
Also find the surface area of the sphere using the formula (4 * pi * r2).
Use proper comments in the function describing the function, its parameters,
purpose and creator in detail.
________________________________________________________________________
23. Create a data file (having extension of .dat) containing 20 numbers. Write a script
that will load the vector from the file, and use subplot to do a bar-chart plot and a
stem plot with these data in the same Figure Window. (Note: A loop is not
needed.) Prompt the user for a title for each plot.
________________________________________________________________________
24. Do a quick survey of your friends to find out who prefers cheese pizza, pepperoni,
or mushroom (no other possibilities; everyone must pick one of those three
choices). Draw a pie chart to show the percentage favoring each. Label the pieces
of this pizza pie chart! Use the explode option to highlight a specific slice.
________________________________________________________________________
25. Write a script that will draw the line y = x between x = 2 and x = 5, with a random
thickness between 1 and 10.
________________________________________________________________________
26. Create an x vector that has 30 linearly spaced points in the range from –2 pi to 2
pi, and then y as sin(x). Do a stem plot of these points, and store the handle in a
variable. Use get to see the properties of the stem plot, and then set to change the
face color of the marker.
________________________________________________________________________
________________________________________________________________________
28. Write a Matlab function fives.m that will receive two arguments for the number of
rows and columns and will return a matrix with that size of all 5s.
________________________________________________________________________
29. Create the vector x=3:0.5:35 and compute the following function first using a for
loop and an if block and then using only logical expressions (without using a for
loop).
________________________________________________________________________
30. Given an integer n, write a Matlab script to produce a vector of character arrays
which contains all possible combinations of the integers 0 to 9 of length n.
For instance for n = 4, a vector containing character arrays from 0000 up to 9999.
Hint: recall the int2str function.
________________________________________________________________________
31. Write a while loop which stops at the first entry in x = rand(100,1) which is larger
than 0.95 and returns the number of the entry.
________________________________________________________________________