DOC PREVIEW
UIUC CS 101 - lect02

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PowerPoint PresentationSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 362-2What is a Matlab function?How does the plot function work?What Matlab functions are available to do Data Analysis?How can you create your own function ?Readings: Matlab by Pratap Chapters 2.4, 4.1, 4.2, 4.3, 5.2.2, 5.6.12-3Basic Mathematical Functions (see 3.2.4 in Pratap) However, since there are hundreds of Matlab functions, a useful tool is Matlab Helpdesk.Exponential Function>> exp(0) (exponential function ex)ans =1name argument parenthesis>> x = [-1 0 1];>> exp(x) ans =0.3679 1.0000 2.7183 ( [exp(-1), exp(0), exp(1)] )The exp function argument can be a scalar, vector or matrix.4Three forms of the use of a function in Matlab are:>> VAR = function_name(arg1,arg2, …);>> [VAR1,VAR2,...] = function_name(arg1,arg2, …);>> function_name(arg1,arg2, …);A Matlab function like a mathematical function is a rule where given a certain input or inputs, the rule tells you how to compute the output value or how to produce an effect (e.g. the plot function produces a figure). The inputs are called the “arguments” to the function.As an illustration, a function can be likened to the sequence of instructions on an income tax form(see next two slides).• The instructions must be followed in sequential order.• Fields (or boxes in the form represent variables). All variables in a function are local to that function. They cannot been seen by other users filling out other forms.• Fields in the form that must be filled in with information provided by the person whose tax is being computed are called parameters . The values entered in these fields, (like name , social security number),... are called input arguments. • If the taxpayer recieves a return this amount is always found in one specific field on the form, field 67a. This field is called an output variable.name fieldrefund amountamount due2-8Example: Plot the sine function using the built-in function “sin” .(see section 5.1)>> x = -10 : .1 : 10; % (remember ; suppresses output)>> y = sin(x); % x in radians>> plot(x,y);By default Matlab will connect the points (xi ,yi) with a straight line. A more general version of the plot command is:plot(x,y,’color_linestyle_marker’);where color is:cyan magenta yellow red green blue white blackc m y r g b w k2-9Linestyle can be any of the following:solid dashed dotted dashed-dot no lineline line line line- - - : -. noneand marker can be specified as:plus asterisk point cross square diamond+ * . x square diamondExample:>> x = -10 : .1 : 10;>> y = sin(x); >> plot(x,y,’m--square’);Figure Window2-11Note: the function plot can have a variable number of arguments. However, the order of placement of the arguments is significant. On an income tax form, if in field 7 wages, tips,... you typed in the number of dependents you would not expect to compute the true income tax.Example:>> plot(y,x); (may not give the same results as plot(x,y) )>> plot(’m--square’,x,y); (does not work)>> plot(x,y); plot x and y>> plot(x,y,’m--square’); plot x , y and a string >> y = sin(x+1); sin expression “x+1”Name of function Arguments2-12>> x = pi / 4; % line 1>> y = sin(x) ; % line 2During execution of the above lines, the mechanism for the call works this way:1) A copy of the value of the variable x is passed (by Matlab) to the code for the built-in function sin .2) The execution temporarily suspends (at line 2) while the code for the function sin executes.3) After the code in sin finishes executing, execution proceeds with line 2. The value (.7071…) that the sin function computes is returned to line 2 and replaces the expression shaded in yellow above.2-13>> vec1 = [-2 ; 3 ; 4] ;>> vec2 = [-2 3 4] ;>> sum(vec1) input is a 3 x 1 column vector and output is a scalar.ans =5>> sum(vec2) input is a 1 x 3 row vector and output is a scalar.ans =5Matlab’s built-in functions accept a wide variety of array sizes. Example: The built-in function sum.(see section 5.3)2-14Users can add to Matlab’s built-in functions by “programming” a function. A program in Matlab is either in the form of a script or a function.Programming a function is described in the Matlab book in Chapter 4.2 and scripts are described in Chapter 4.1.2-15Programming - • A process for obtaining a computer solution to a problem. • A computer program is a sequence of instructions that tell the computer what to do.2-16• Modular ProgrammingBreak complicated tasks up into pieces(functions).• Functions can “call” other functions.This means you don’t have to re-write the code for the function again and again.• Variables in Functions are “local”.All variables in the function are “ local ” by default. That means that if you have a variable in your workspace with the same name as the variable in a function, then assigning a value to the variable in the function has no affect on the variable in the workspace. That is, a function cannot accidentally change (destroy) the data in your workspace.2-171. Problem Definition2. Refine, Generalize, Decompose the problem definition(i.e., identify sub-problems, I/O, etc.)3. Develop Algorithm(processing steps to solve problem)4. Write the "Program" (Code)(instruction sequence to be carried out by the computer)5. Test and Debug the Code6. Run Code2-181. Problem Definition Write a function that converts a temperature in Fahrenheit to Celsius. 2. Refine, Generalize, Decompose the problem definition(i.e., identify sub-problems, I/O, etc.)Use the fact that celsius = (fahr - 32) * 5/92-19Natural-Language Algorithm The function “definition” should list one input variable named fahr and one output variable celsius. Compute the value of the celsius temperature and assign the value to a variable “celsius” The “function” mechanism of Matlab will automatically return the value in the variable celsius when the function is used (called) at the Matlab prompt.3. Develop Algorithm (processing steps to solve problem)2-204. Write the “Function"


View Full Document

UIUC CS 101 - lect02

Documents in this Course
Notes

Notes

114 pages

lect2223

lect2223

35 pages

lect2223

lect2223

35 pages

lect1920

lect1920

23 pages

lect1920

lect1920

23 pages

lect1617

lect1617

25 pages

lect1617

lect1617

25 pages

lect1314

lect1314

34 pages

lect1314

lect1314

34 pages

lect0607

lect0607

25 pages

lect0607

lect0607

25 pages

lect25

lect25

31 pages

lect24

lect24

15 pages

lect21

lect21

25 pages

lect21

lect21

25 pages

lect18

lect18

22 pages

lect18

lect18

22 pages

lect15

lect15

37 pages

lect15

lect15

37 pages

lect12

lect12

31 pages

lect12

lect12

31 pages

lect11

lect11

28 pages

lect11

lect11

28 pages

lect10

lect10

28 pages

lect09

lect09

24 pages

lect09

lect09

6 pages

lect08

lect08

23 pages

lect08

lect08

23 pages

lect05

lect05

26 pages

lect05

lect05

26 pages

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect02
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lect02 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lect02 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?