MATLAB Built-in Functions - Fun with MATLAB - Lab #1 | CS 1112, Lab Reports of Computer Science

Material Type: Lab; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Fall 2008;

Typology: Lab Reports

Pre 2010

Uploaded on 08/30/2009

koofers-user-ya2
koofers-user-ya2 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Section (Lab) 1
When you have completed the lab, show this sheet and any associated programs to your lab instructor, who will
record that you have completed the lab. If you do not finish this exercise during the lab, show the instructor
what you have done at the end of the lab and be sure to complete it in the next few days.
If you have any questions, ask your lab instructor or a consultant immediately! They are in the lab to help you
learn the material.
1Matlab built-in functions... fun with Matlab
Matlab provides numerous built-in variables and functions. For each line below, type the text in the Command
Window and press <Enter>to see what happens. Is the result what you expect? Fill in the blanks below with
the screen output for that line.
% This is a comment--no action is executed by the computer
% From this point on, read but do not type the text after the % symbol in a line.
% Variables, constants, and simple calculations:
a= 100 % Look at the Workspace Pane: a VARIABLE called a has been created
b= 99 % Look at the Workspace Pane: a VARIABLE called b has been created
format compact
a/b % _____________________________
ans
y= ans % _____________________________
format long
y
format short
y
p=(3*2)^2
q=(3*2)^2; % Did you type the semi-colon? Look at the Workspace Pane: q is
% created but its value is not shown in the Command Window.
x = 2; y = x^x; z = y^y % _____________________________
format loose
% Built-in functions:
sqrt(x)
pi % a built-in variable
cos(pi) % _____________________________
abs(ans)
abs(cos(pi)) % _____________________________
exp(ans)
rem(5,2) % What does function rem do? If you’re not sure, try a few more
% examples: rem(9,7), rem(10,6), ... ___________________________
rand(1) % Generate one random number in the range of (0,1)
help rand
lookfor magic % MATLAB searches its documentation for the keyword ’magic’
% Wait a few seconds. If this command takes too long to
% complete, press <Ctrl><c> to make it stop.
2 Running and editing a Matlab program
Download the file spiral.m from the Exercises page to the Desktop of your computer. (You can use another
directory/path, but be sure to write down the location of your file.)
Now set the Current Directory to be the directory in which you have stored your file spiral.m. You should now
see the file listed in the Current Directory Pane.
1
pf2

Partial preview of the text

Download MATLAB Built-in Functions - Fun with MATLAB - Lab #1 | CS 1112 and more Lab Reports Computer Science in PDF only on Docsity!

CS1112 Section (Lab) 1

When you have completed the lab, show this sheet and any associated programs to your lab instructor, who will record that you have completed the lab. If you do not finish this exercise during the lab, show the instructor what you have done at the end of the lab and be sure to complete it in the next few days.

If you have any questions, ask your lab instructor or a consultant immediately! They are in the lab to help you learn the material.

1 Matlab built-in functions... fun with Matlab

Matlab provides numerous built-in variables and functions. For each line below, type the text in the Command Window and press to see what happens. Is the result what you expect? Fill in the blanks below with the screen output for that line.

% This is a comment--no action is executed by the computer

% From this point on, read but do not type the text after the % symbol in a line.

% Variables, constants, and simple calculations: a= 100 % Look at the Workspace Pane: a VARIABLE called a has been created b= 99 % Look at the Workspace Pane: a VARIABLE called b has been created format compact a/b % _____________________________ ans y= ans % _____________________________ format long y format short y p=(32)^ q=(32)^2; % Did you type the semi-colon? Look at the Workspace Pane: q is % created but its value is not shown in the Command Window.

x = 2; y = x^x; z = y^y % _____________________________ format loose

% Built-in functions: sqrt(x) pi % a built-in variable cos(pi) % _____________________________ abs(ans) abs(cos(pi)) % _____________________________ exp(ans) rem(5,2) % What does function rem do? If you’re not sure, try a few more % examples: rem(9,7), rem(10,6), ... ___________________________ rand(1) % Generate one random number in the range of (0,1) help rand lookfor magic % MATLAB searches its documentation for the keyword ’magic’ % Wait a few seconds. If this command takes too long to % complete, press to make it stop.

2 Running and editing a Matlab program

Download the file spiral.m from the Exercises page to the Desktop of your computer. (You can use another directory/path, but be sure to write down the location of your file.)

Now set the Current Directory to be the directory in which you have stored your file spiral.m. You should now see the file listed in the Current Directory Pane.

To run the program (script) spiral, in the Command Window type the file name without the extension .m. What do you see on the screen?

To read the program, open the program file in the Editor Window: select menu options File→Open and then select or type the file name spiral.m. (Or in the Current Directory Pane double click on the file name.)

Now experiment with the program!

  1. Change the value of turnAngle from 100 to 137. Save the program and run it to see what happens. Try other values for turnAngle. What does the variable turnAngle represent (or control) in the program?
  2. Now change the value of numEdges and run the program again. Try again with another value. What does variable numEdges represent?
  3. Which variable controls the color of the spral? Now change the color.
  4. Now experiment with the print format. Look at Line 43 in the program. The title(.. .) command prints a string of text at the top of the figure window. The substitution sequence %d inside the quotation marks is replaced by the value of the variable numEdges when the command is executed. Notice that the value is printed as a whole number (no decimal part). Change the substitution sequence %d to %f and run the program. How has the format changed?
  5. Now we try to control the number of decimal places that are printed. Change one of the substitution sequences from %f to %.2f. Run the program again and notice that the print format of only one of the two numbers printed has changed. What does the substitution sequence %.2f specify?
  6. Say, you want to use 10 character spaces for printing the entire value (including the decimal point) with 2 decimal places shown. You will then use the substitution sequence %10.2f. Again, make this change and observe the print format.

3 Temperature conversion (from FVL M1.1.3)

A temperature can be converted from the Fahrenheit scale to Celsius using the formula c = 59 (f − 32) where f is a temperature given in ◦F and c is the resulting temperature in ◦C. Write a script to prompt the user for a temperature in ◦F and convert and print the temperature in ◦C. Save the file as convertF2C.m.

4 CS1112 course web page http://www.cs.cornell.edu/courses/cs1112/

Answer the following questions:

  1. What is AEW and when is the first meeting?
  2. Where are the reading assignments listed?
  3. What does the CS1112 AI policy say about code found on the Internet?
  4. Can you use the office hour of a TA who is not your section instructor?
  5. Where do you find the consultants to get help? (Note: consulting begins on Wednesday)
  6. How do you indicate that you work with a partner on a project? (See CMS link)

5 CMS: Course Management System

You will use CMS to access the online book chapters, submit homework, find your scores, and view grading comments. If you enrolled in CS1112 before 4pm on 8/28, you can use CMS already. Try to log on CMS now. If you cannot log on, ask your section instructor to add your information into CMS. Note that your homework must be submitted through CMS.

Please delete your files from the computer before you leave!