lab 04

Arrays,For,IF,i/o,width/precision,emacs,pico,

General

  • Review the code examples from class.
  • Some programs below are associated with a question. Answer the questions using comments below your code in the m-file.
  • The office hours of the TAs and the instructor are on the class website. Visit us!

Problem 1

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

 

Problem 2

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.

Problem 3

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

  1. Open an XTerm on strauss, and type emacs &. If you get a ``command not found’’ message, you may have to type /opt/bin/emacs & until you correct your path variable.
  2. The & is a special command suffix to let Strauss know that you want emacs to open in a new window and let you keep using the old xterm window, too.
  3. When you first see Emacs, the paragraph of text will tell you how to start running a self-directed tutorial (Your Unix text also has information on using emacs). Do this. The option to run the tutorial appears every time you open emacs. You can run the tutorial as many times as you like until all the material is familiar or you become bored out of your gourd. I suggest you also print out the emacs reference card from the class website and learn the keystroke commands; this will make it easier to work remotely.
  4. Once you are comfortable with using emacs, use emacs to create a text file called sphereVolume.m. In this file write a Matlab function, just as you did using the Matlab  file editor before when you wrote other functions. Save the file when you are done, and then go back to your Matlab window and test out the function.
  5. Use pico editor to comment and debug (as needed), the sphereVolume.m; test your sphere volume function.

Problem 4

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.

Problem 5

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);

 

Submit

Be sure that you have a printed copy of:

  1. function M-files
  2. script M-files
  3. diary files (preferably one big one)

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 Sakai. Then, click submit ONLY ONCE to send these to your Sakai and your TA.

adapted from a lab by tharvey

back to main CISC106 page