DOC PREVIEW
UIUC CS 101 - lect05

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 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 26 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 26 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 26 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 26 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 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 265-2What does the Matlab function fplot do?How to use fzero to find find a root of an equation?What is a global variable?How the meshgrid, view and surf functions are used to create 3-D plots.Readings: Matlab by Pratap Chapter 4.2.2,4.3.3,5.6, 6.1,6.35-3>> fplot(@sin, [-10 10]); (plots sin(x) between -10 and 10)>> xlabel(‘x-axis’);>> ylabel(‘y-axis’); >> title(‘y = sin(x)’);>> text(-0.5,-0.5,’any text’,’Fontsize’,20); (text starts at (-.5,-.5))>> grid on;>> axis([-10 10 -1.5 1.5]); (resize Figure window,axis([xmin xmax ymin ymax]))Use the fplot command to plot either built-in or user definedfunctions (see Matlab book 3.8)5-5We will consider only methods for finding real roots.• Definition (for a function of one variable y = f(x) )If value “ r ” satisfiesf(r) = 0then “ r ” is called a root or a ("zero") of f .• Exact SolutionsFor some functions, we can calculate roots exactly; - Polynomials up to degree 4- Simple transcendental functions; sin x = 0, which has an infinite number of roots:5-6fzero -- built-in Matlab function for finding one root for a function y = f(x) that has only one variable. (see Matlab book 4.2.2, 5.6)• Requires a starting point.• The function f must be continuous.• Fast convergence!• Works with polynomials but will return only one “real” root if it exits, so use the roots function for polynomials.5-7Problem: Find a root of the function f(x) = sin(x) near x= .5 .>> fzero(@sin, .5) ans = 1.8428e-18Notice that sin(0) = 0 but fzero returned a value close to but not exactly equal to zero . The expression @sin is called a function handle. A function handle is a MATLAB value that provides a means of calling a function indirectly.Problem: Find a root of the function f(x) = x2 - ex near x= 0 .>> fzero(‘x.^2-exp(x)’ , 0)ans = -0.70355-8One form of the fzero function is:fzero(@function_name, x0)Where function_name is either the name of a built-in Matlab function or the name of a user defined function.x0 is an initial guess for the root. If you have no idea what x0 should be then try using the fplot function to plot the function.Another form of the fzero function is:fzero(‘expression in one variable’, x0)5-9If fzero cannot find a root then it will tell you.Example>> fzero(‘x – log(x)’, 1) % log is natural logarithmExiting fzero: aborting search for an interval containing a sign change because complex function value encountered during search (Function value at -0.28 is 0.99297-3.1416i)Check function or try again with a different starting value.Exiting fzero: aborting search for ...ans = NaNNaN means “not a number”If you don’t believe this then “seeing is believing” so ...>> fplot( @(x) x-log(x) , [0,5] ) % or fplot(‘x-log(x)’ , [0,5])>> axis([-1,5,0,5]);5-11All variables declared in a function are “local” to that function. That is, assigning or changing the value of a variable in the function has no affect on a variable with the same name in the workspace or another function(unless you pass the value as an argument).You may want to be able to change the variable in your workspace or share values with other functions. To do this you must use the global command (see 4.3.3). Consider the example on the next slide. As a naming convention, we will use all capital letters for global variables.5-121. Problem Definition Write a function named total that returns the sum of the values passed to it though the input variable x. The function total also appends the value of x to a global variable named LIST. LIST contains the history of values passed to the function total.2. Refine, Generalize, Decompose the problem definition(i.e., identify sub-problems, I/O, etc.) Assume x is a scalar. (check to see if total works for row vectors) Since LIST is global we must type global LIST in both the function total and at the Matlab prompt.5-13 Natural-Language Algorithm To add the value of x to the end of the LIST use the Matlab code:LIST = [LIST , x];To return the sum of all values in this new list: result = sum(LIST);3. Develop Algorithm (processing steps to solve problem)5-144. Write the “Function" (Code)Use the Matlab editor to create a file total.m .function result = total(x)% function result = total(x)% Programmer: Tom Gambill% Date: 2/10/01% Input: a scalar, x% Output: The function total returns the sum of all values % passed to this function. If you need to access% these values, you must use the command% global LISTglobal LISTLIST = [LIST, x];result = sum(LIST);5-155. Test and Debug the Code Note the execution sequence on the next slide.6. Run CodeDoes the function work if x is a row vector?5-17(continued from previous slide)5-18(continued from previous slide)We can reset LIST from the Matlab prompt.(LIST = empty vector)5-19Example: Plot the 3-D surface described by the equation, in the region in the x-y plane from x = -3 to 3 and y = -2 to 2.Step 1) solve the equation for z, (continued on the next slide)2 24cos( )*cos( )* 0x yx y e z- +- =2 24cos( )*cos( )*x yz x y e- +=5-20Step 2) create two vectors x and y . The values of x and y will determine the region in the xy plane over which the surface is plotted. >> x = linspace(-3,3,7); >> y = linspace(-2,2,5); Step 3) Use the meshgrid function to create two matrices, X and Y . We will use these to compute Z. >> [X , Y] = meshgrid(x,y)5-213 2 1 0 1 2 33 2 1 0 1 2 33 2 1 0 1 2 33 2 1 0 1 2 33 2 1 0 1 2 3- - -� �� �- - -� �� �- - -� �- - -� �� �- - -� �X =Y =2 2 2 2 2 2 21 1 1 1 1 1 10 0 0 0 0 0 01 1 1 1 1 1 12 2 2 2 2 2 2- - - - - - -� �� �- - - - - - -� �� �� �� �� �� �5-22XY5-23Step 4) compute Z by using the X and Y created by meshgrid,>> Z = cos(X).*cos(Y).*exp(-sqrt(X.^2+Y.^2)./4) Z =0.1673 0.0854 .1286 .2524 0.1286 0.854 0.1673.2426 .1286 0.2050 0.4208 0.2050 .1286 .2426.4676 .2524 0.420 8 1.000 0.4208 .2524 .4676.2426 .1286 0.2050 0.4208 0.2050 .1286 .24260.1673 0.0854 .1286 .2524 .1286 0.08- -- - - -- - - -- - - -- - - 54 0.1673� �� �� �� �� �� �� �� �2 24cos( )*cos( )*x yz x y e- +=5-24XYZ5-25Step 4) compute Z by using the X and Y created by meshgrid,>> R = sqrt(X.^2+Y.^2);>> Z =


View Full Document

UIUC CS 101 - lect05

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

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect05
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 lect05 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 lect05 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?