Matlab Questions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

MATRIXES AND VECTORS

1. Generate a random:

• Real number in the range from 0 to 1


• Real number in the range from 0 to 20
• Real number in the range from 20 to 50
• Integer in the range from 0 to 10
• Integer in the range from 0 to 11
• Integer in the range from 50 to 100

2. Using the colon operator and the transpose operator, create a column vector that
has the values –1 to 1 in steps of 0.2.
3.

4. Create a 3 × 5 matrix of random integers, each in the range from –5 to 5. Get the
sign of every element.
5. Create a vector, x, which consists of 20 equally spaced points in the range from –π
to +π. Create a y vector that is sin(x).

PROGRAMMES
6. Create a file called testtan.dat that has two lines, with three real numbers on
each line (some negative, some positive, in the range from –1 to 3). The file
can be created from the editor, or saved from a matrix. Then, load the file into
a matrix and calculate the tangent of every element in the resulting matrix.
7. Create a 3 × 6 matrix of random integers, each in the range from 50 to 100. Write
this to a file called randfile.dat. Then, create a new matrix of random integers, but
this time make it a 2 × 6 matrix of random integers, each in the range from 50 to
100. Append this matrix to the original file. Then, read the file in (which will be to a
variable called randfile) just to make sure that it worked!
8. Plot sin (x) for x values ranging from 0 to (in separate Figure Windows):
• using 10 points in this range
• using 100 points in this range
9. Experiment, in the Command Window, with using the fprintf function for integers.
Make a note of what happens for each. Use fprintf to print the integer 12345
• without specifying any field width
• in a field width of 5
• in a field width of 8
• in a field width of 3
10. Write an input statement that will prompt the user for a string. Then, find the
length of the string.

SELECTION STATEMENTS
11. Write a script that will ask the user “Are you an engineer? (Y/N)” and then print an
appropriate response based on the user’s answer. Decide what is appropriate!
12. Write a script to prompt the user for a character, and then print whether or not it is
a letter of the alphabet.
13. Write a script that will prompt the user for a numerator and a denominator for a
fraction. If the denominator is 0, it will print an error message saying that division
by 0 is not possible. If the denominator is not 0, it will print the result of the fraction.
14. Write a script that prompts the user for a value of a variable x. Then, it uses the menu
function to present choices between sin(x), cos(x), and tan(x). The script will print
whichever function of x the user chooses. Use an if-else statement to accomplish this.
15. Write a function that will receive one number as an input argument. It will use the
menu function that will display “Choose a function” and will have buttons labeled
ceil, round, and sign. Using a switch statement, the function will then calculate
and return the requested function (e.g., if round is chosen, the function will return
the rounded value of the input argument).

LOOPING
16. Write a for loop that will print the column of real numbers from 1.1 to 2.9 in steps
of 0.1.
17. In the Command Window, write a for loop that will iterate through the integers
from 32 to 255. For each, show the corresponding character from the character
encoding.
18. Create a 1 × 6 vector of random integers, each in the range from 1 to 20. Use
built-in functions to find the minimum and maximum values in the vector. Also
create a vector of cumulative sums using cumsum.
19. Create a 3 × 5 matrix. Perform each of the following two ways: using built-in
functions, and also using loops (with if statements if necessary):
■■ Find the maximum value in each column.
■■ Find the maximum value in each row.
■■ Find the maximum value in the entire matrix.
20. Write a function that will receive a matrix as an input argument, and will calculate
and return the overall average of all numbers in the matrix. Use loops, not built-in
functions, to calculate the average.

MATLAB PROGRAMS
21. Write a function that will receive as an input argument a temperature in degrees
Fahrenheit, and will return the temperature in both degrees Celsius and Kelvin.
The conversion factors are: C = (F – 32) * 5/9 and K = C + 273.15.
22. Write a function that will receive as an input argument a length in feet and will
return the length in both yards and centimeters. One yard is equal to 3 feet. One
inch is equal to 2.54 centimeters, and there are 12 inches in a foot.
23. Write a function that will receive the radius of a circle and will print both the
radius and diameter of the circle in a sentence format. This function will not return
any value; it simply prints.
24. Write a function that will receive as input arguments a length in inches, and will
print in sentence format the length in both inches and centimeters (1 inch = 2.54
centimeters). Note that this function will not return any value.
25. Write a program to calculate and print the area and circumference of a circle. There
should be one script and three functions to accomplish this (one that prompts for
the radius, one that calculates the area and circumference, and one that prints).

STRING MANIPULATION
26. Write a function that will prompt the user separately for a first and last name and
will create and return a string with the form ‘last, first’.
27. Write a script that will, in a loop, prompt the user for four course numbers. Each
will be a string of length 5 of the form ‘CS101’. These strings are to be stored in a
character matrix.
28. Write a script that will create x and y vectors. Then, it will ask the user for a color (‘red’,
‘blue’, or ‘green’) and for a plot style (‘o’, ‘*’). It will then create a string pstr that contains
the color and plot style, so that the call to the plot function would be plot(x,y,pstr).
For example, if the user enters ‘blue’ and ‘*’, the variable pstr would contain ‘b*’.
29. Either in a script or in the Command Window, create a string variable that
stores a string in which numbers are separated by the character ‘x’, for example
‘12x3x45x2’. Create a vector of the numbers, and then get the sum (e.g., for the
example given it would be 62 but the solution should be general).
CELL ARRAYS AND STRUCTURES
30. Two variables store strings that consist of a letter of the alphabet, a blank space,
and a number (in the form ‘r 14.3’). Write a script that would initialize two such
variables. Then, use string manipulating functions to extract the numbers from the
strings and add them together.

31. Create a 2 × 2 cell array by using the cell function to preallocate and then put
values in the individual elements. Then, insert a row in the middle so that the cell
array is now 3 × 2. Hint: Extend the cell array by adding another row and copying
row 2 to row 3, and then modify row 2.
32. Create a row vector cell array to store the string ‘xyz’, the number 33.3, the vector 2:6,
and the logical expression ‘a’ < ‘c’. Use the transpose operator to make this a column
vector, and use reshape to make it a 2 × 2 matrix. Use celldisp to display all elements.
33. Create a structure variable that would store for a student his or her name,
university ID number, and GPA. Print this information.
34. Create a data structure to store information on the planets in our solar system.
For every planet, store its name, distance from the sun, and whether it is an inner
planet or an outer planet.
35.

ADVANCED FILE INPUT AND OUTPUT


36.
37. Create a multiplication table and write it to a spreadsheet.
38. Write a script to read in division codes and sales for a company from a file that has
the following format:
A. 4.2
B. 3.9
Print the division with the highest sales.
39. Create a file ‘parts_inv.dat’ that stores on each line a part number, cost, and
quantity in inventory, for example:
123 5.99 52
456 3.97 100
333 2.22 567
Use fscanf to read this information, and print the total $ amount of inventory (the
sum of the cost multiplied by the quantity for each part).
40. A spreadsheet, ‘popdata.xls’, stores the population every 20 years for a small town
that underwent a boom and then a decline. Create this spreadsheet (include the
header row) and then read the headers into a cell array and the numbers into a
matrix. Plot the data using the header strings on the axis labels.
Year Population
1920 4021
1940 8053
1960 14994
1980 9942
2000 3385

ADVANCED FUNCTIONS
41. Write an anonymous function to implement the following quadratic: 3x2 − 2x + 5.
Then, use fplot to plot the function in the range from –6 to 6.
42. Write an anonymous function to calculate and return the area of a rectangle.
43. The hyperbolic sine for an argument x is defined as:
hyperbolicsine(x) = (ex − e−x)/ 2
Write an anonymous function to implement this. Compare yours to the built-in
function sinh.
44. There is a built-in function function called cellfun that evaluates a function for
every element of a cell array. Create a cell array, then call the cellfun function,
passing the handle of the length function and the cell array in order to determine
the length of every element in the cell array.
45. Write a function that will receive a variable number of input arguments: the
length and width of a rectangle, and possibly also the height of a box that has
this rectangle as its base. The function should return the rectangle area if just the
length and width are passed, or also the volume if the height is also passed.

You might also like