Sie sind auf Seite 1von 2

ENG EK 127

Worksheet 3A

Name: Yen Pham


Section: C2

Instructions: Do all problems.

1. Write a simple script that will calculate the volume of a hollow sphere which is
4 3
(ro  ri3 ) where ri is the inner radius and ro is the outer radius. Assign a value to a
3
variable for the inner radius, and also assign a value to another variable for the outer
radius. Then, using these variables, assign the volume to a third variable. Include
comments in the script.

1. % Assgign values respectively for the inner raidus ri and
2. % the outer radius ro
3. ri = input('Enter the value of the inner radius in meters: ');
4. ro = input('Enter the value of the outer raidus in meters: ');
5. %Calculate volume of the hollow sphere
6. V= 4*pi/3*(ro^3­ri^3);
7. fprintf('The volume of the hoolow sphere is %f,\n',V)

2. The input function can be used to enter a vector, e.g.


>> vec = input('Enter a vector: ')
Enter a vector: 4:7
vec =
4 5 6 7
Experiment with this, and find out how the user can enter a matrix.
>> mat= input('Enter a matrix:')
Enter a matrix:randi([50,100],3,4)

mat =

91 96 64 99
96 82 77 58
56 54 98 99

3. In the Command Window, create x and y data points by creating x and y vectors,
 Use the bar function instead of plot to get a bar chart.
>> ylabel('Temperature')
>> x=1:12;
>> y=randi([10,100],1,12);
>> bar(x,y)
>> xlabel('Month')
>> ylabel('Temperature')

4. Experiment, in the Command Window, with the fprintf function. Make a note of
what happens for each.
 First, use fprintf to print the real number 12345.6789
 without specifying any field width
 in a field width of 10 with 4 decimal places
 in a field width of 10 with 2 decimal places
 in a field width of 6 with 4 decimal places
 in a field width of 2 with 4 decimal places

5. Next, use fprintf to print the integer 12345


 in a field width of 5
 in a field width of 8
 in a field width of 3

6. Create the following variables


x = 12.34;
y = 4.56;
Then, fill in the fprintf statements using these variables that will accomplish the
following:
>> fprintf(
x is 12.340
>> fprintf(
x is 12
>> fprintf(
y is 4.6
>> fprintf(
y is 4.6 !

7. Write a script to prompt the user for the length and width of a rectangle, and print its
area with 2 decimal places. Put comments in your script.

8. Write a script that will


 Prompt the user for an angle in degrees
 calculate the angle in radians . (Note:  radians = 180)
 Print the result

Das könnte Ihnen auch gefallen