Create a row vector called ``a'' with
the even numbers from 0-8, inclusive, by specifying an interval.
- Show how to extract the number 4 from
a
- Set ``b'' to the transpose of a (i.e.
b will be a column)
- Show how to change the 4 in b to a 7
- Use a for loop to display each member
of b
- Create a matrix ``c'' with three rows
and four columns, with consecutive integers starting
at 1 in the first row, 5 in the second row, 9
in the third
- Show how to extract the numbers 7 and
11 from c
- Set the 10 in c to 10.5
- Set ``d'' to be the third column from
c
- Set ``e'' to be the second row from c
- Use ``sum’’ to compute sum of each
row of c and print to the screen
At a Matlab command line, type the expression 0.3 - 0.2 - 0.1
to see the result. Are you surprised?
Use the fprintf function from Chapter 2.6 (Displaying
Output Data) to examine each of the numbers involved. Change width and
precision fields of the format specifier as described in the
chapter, so that you can see the numbers with great precision. Once you have
learned how to use fprintf to do this, start a diary file and show high
precision versions of all the tenths from 0.1 to 1.0. Each should print on a
separate line.
Calculate the volume of a sphere (volume
= 4/3pi r3) using the command window and then in a
script M-file. Then use the editor program emacs
to write a function file. When you are done, create a diary file ``sphere.txt’’ that
shows all three ways.
3.1 Creating a file with emacs
emacs
&
. If you get a ``command not found’’ message, you may
have to type /opt/bin/emacs &
until you correct your path variable. Copy the files areas.m and getCircleArea.m from
the lab directory (/www/htdocs/CIS/106/chandrak/08F/labs). Place them in your
lab04 directory with a copy of your circleArea.m file, and run the areas() function.
It is good programming practice to
separate user input and output from calculations. Look at the two tables you copied and see
how they work. If there are any lines you don’t understand, use them alone in
the interpreter (the command window) to see how they behave. If they still don’t
make sense, ask!!
Now add the files you need to make
the program work for rectangles. When that is completely working and tested,
add a feature for calculating the area of a right triangle (0.5 * b* h), and
area of ellipse (pi *a *b). Be sure to upload all your files, including the
copied ones.
Write a function M-file using the
relational operators from class and the readings (<,>,<=,
etc) above with an if statement. The function will use fprintf (from chapter 2.2) to show a message saying whether
the input parameter, n, is ten or less; greater than ten but less than twenty;
twenty or greater but less than thirty; or thirty or greater. Open a file
called "output" and write the result there using following commands.
To open the file with a file pointer: fw=fopen('output','wt');
To write into the file (example): fprintf(fw,'This number is less than ten:%d\n',n);
To close the file once finished writing
(at the end of the program): fclose(fw);
Be sure that you have a printed copy
of:
All must be stapled together, with your
name and lab section on the top page. Be sure that you upload a copy of all the
MATLAB ﬦ#129;les to
adapted from a lab by tharvey