CS101 Lecture 5 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CS101 Lecture 5

Description:

How the meshgrid, view and surf functions are used to create 3-D plots. ... 3-D plots can be done in a 5-step process using the built-in functions meshgrid and surf. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 25
Provided by: tomga
Category:
Tags: cs101 | lecture | surf

less

Transcript and Presenter's Notes

Title: CS101 Lecture 5


1
Lecture 5
2
What will I learn in this lecture?
What 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.3.3,5.6, 6.1,6.3
3
fplot
Use the fplot command to plot either built-in or
user defined functions (p. 168)
gtgt fplot(_at_sin, -10 10) (plots sin(x) between
-10 and 10) gtgt xlabel(x-axis) gtgt
ylabel(y-axis) gtgt title(y sin(x)) gtgt
text(-0.5,-0.5,any text,Fontsize,20) (text
starts at (-.5,-.5)) gtgt grid on gtgt axis(-10 10
-1.5 1.5) (resize Figure window, axis(xmin
xmax ymin ymax))

4
(No Transcript)
5
Roots of functions of one variable
  • Definition (for a function of one variable y
    f(x) )
  • If value r satisfies
  • f(r) 0
  • then r is called a root or a ("zero") of f .
  • Exact Solutions
  • For 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

We will consider only methods for finding real
roots.
6
fzero - finds roots of functions
  • fzero -- built-in Matlab function for finding one
    root for a function y f(x) that has only one
    variable.
  • (see Matlab book p. 94)
  • 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.

7
Using fzero - An Example
Problem Find a root of the function f(x)
sin(x) near x .5 . gtgt fzero(_at_sin, .5) ans
1.8428e-18 Notice that sin(0) 0 but fzero
returned a value close to but not exactly equal
to zero . The expression _at_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 . gtgt fzero(x.2-exp(x)
, 0) ans -0.7035
8
fzero - Matlab function
One form of the fzero function is fzero(_at_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)
9
Using fzero - when it doesn't find a root
If fzero cannot find a root then it will tell
you.Examplegtgt fzero(x log(x), 1) log is
natural logarithm Exiting 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
10
If you dont believe this then seeing is
believing so ...gtgt fplot( _at_(x) x-log(x) , 0,5
) or fplot(x-log(x) , 0,5)gtgt
axis(-1,5,0,5)
11
global Variables
All 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 p98). Consider the
example on the next slide. As a naming
convention, we will use all capital letters for
global variables.
12
Function - total
  • 1. 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.

13
Function - total
3. Develop Algorithm (processing steps to solve
problem)
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)
14
Function - total
4. 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 LIST global
LIST LIST LIST, xresult sum(LIST)
15
Function - total
  • 5. Test and Debug the Code
  • Note the execution sequence on the next slide.
  • 6. Run Code
  • Does the function work if x is a row vector?

16
Function - total
17
Function - total
(continued from previous slide)
18
Function - total
(continued from previous slide)We can reset LIST
from the Matlab prompt.
(LIST empty vector)
19
3-D Plotting
Example Plot the 3-D surface described by the
equation, in the region in the x-y
plane from x -5 to 5 and y -4 to 4. Step
1) solve the equation for z, (continued on
the next slide)
20
3-D Plotting
Step 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. gtgt x
linspace(-3,3,7) gtgt 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. gtgt X , Y meshgrid(x,y)
21
3-D Plotting
X
Y
22
3-D Plotting
Step 4) compute Z by using the X and Y created by
meshgrid, gtgt R sqrt(X.2Y.2) gtgt Z
cos(X).cos(Y).exp(-R./4) Z
23
3-D Plotting
Step 4) compute Z by using the X and Y created by
meshgrid, gtgt R sqrt(X.2Y.2) gtgt Z
cos(X).cos(Y).exp(-R./4)Step 5) graph in 3-D
by applying the surface function, gtgt surf(X,Y,Z)
24
What have I learned in this lecture?
We can use the Matlab functions fzero to find
zeros or roots of a function of one variable. The
function fzero is generic but finds only one
root, the function roots applies only to
polynomials but finds all roots. The function
fplot is used when you have an explicit
function, (user defined or built-in). The plot
function can be used for both explicitly defined
functions andraw data for which we dont know
the functional relationship. The global command
allows more than one function to share(access) a
single copy of a variable. You can also share
the global variable with the active workspace.
3-D plots can be done in a 5-step process using
the built-in functions meshgrid and surf .
Write a Comment
User Comments (0)
About PowerShow.com