analyzing functions (4.16) y=f(x) matlab. functional analysis includes: plotting and evaluating a...

21
Analyzing Functions (4.16) y=f(x) MATLAB

Upload: jayson-harris

Post on 17-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Analyzing Functions (4.16)y=f(x)

MATLAB

Page 2: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Functional Analysis includes:

Plotting and evaluating a function Finding extreme points Finding the roots (zeros – where y=0) Finding the area under the curve

(integrating) Differentiation Using “inline” functions

Page 3: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Purpose

Equation solving is an essential part of engineering

In various applications we may want to find roots, min and max points, areas, and so forth associated with some function.

Page 4: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Several ways to define the f(x)

Defining a string variable in the command window:name = ‘equation’

Let’s use the standard normal distribution function (a common distribution used to calculate probabilities associated with the Normal Curve or Bell curve).

22

2

1)(

zezf

Note that in this function, I’m defining “y” as a function of “z”. I could just as easily have called it “x” like many of the examples in the book…I used “z” because that’s the common statistical notation.

string_standnorm=‘(1/sqrt(2*pi))*exp(-z.^2/2)’;

Page 5: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Several ways to define the f(x) Using a function (m-file)…this is probably

the best way since you’ll have it saved in a file. If you define it using the previous method and clear your workspace, you’ll have to redefine it.

function y=standnorm(z)y=(1/sqrt(2*pi))*exp(-z.^2/2);

Page 6: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Referencing the function

Depends on how it was defined (as a string variable or a function in an m-file).

You will often need to enclose the name of the function in single quotes if it is an m-file but not if it is a string variable (depends on what your doing with it and where you are calling it from).

When in doubt, reference the book or MATLAB help files!!

Page 7: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Plotting the function

If it’s a string variable that you defined:

fplot(name, [start of range, end of range])

If it’s a function in an m-file:fplot(‘name’, [start of range, end of

range])

So for our standard normal function:

fplot(string_standnorm, [-3,3])

or

fplot(‘standnorm’,[-3,3])

Let’s try this using both methods…plot the function from z=-3 to z=+3

Page 8: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Evaluating the function at any point or points

First define the values you wish to evaluate the function at (can be a single value or a large array of values).

Use the MATLAB command “eval” (for string variables) or “feval” (for functions in m-files):

Let’s try both…

z=[-3, -2, -1, 0, 1, 2, 3]y=feval(‘standnorm’,z)

Or evaluate the function at a single value…

y=feval(‘standnorm’,3.4)

z=[-3, -2, -1, 0, 1, 2, 3]y=eval(string_standnorm,z)

Page 9: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Finding the extreme points We may be interested in finding the location of the

extreme points (minimums and maximums) of our function in a given range.

‘fminbnd’ is used to get the x value (or z value in our example) where the function is minimized.

The you can use ‘eval’ or ‘feval’ to find the value of the function at those points (f(x) or y). Syntax:

fminbnd(‘name’,a,b)

Again, The ‘’ are needed if the function is defined in an m file and not if the function is defined as a string

a – beginning of the range in which to search

b – end of range

Page 10: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Finding the extreme points, cont.

There is no analogous “fmaxbnd” so instead we define the negative of our function and then use fminbnd of this negative which gives us the maximum points of our original function.

There may exist multiple extreme points (or we may need to evaluate the function for local minimum and maximum).

Page 11: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Finding the extreme points, cont.

In our standard normal function, the minimums and maximums are trivial so let’s try something else… (we’ll use this new function to study roots as well):

Define this in an m-file as a function and save it as “func_in_class.m”

Remember that exponentiation, multiplication, and division of arrays requires element by element notation thus in order to evaluate a function you’ll need to put the “.” in your equation as appropriate.

- Plot this between x=-10 and x=+10- Find the minimum points between -10 and -5, between -5 and 0, and between 0 and 5- Find the maximum points between -10 and 0, between 0 and 5, and between 5 and 10

)1(

)sin()(

2 x

xxf

Page 12: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Finding the Roots

Use the command “fzero” to find the points where the function = 0. This command requires the string variable containing the function or the name of the m-file containing the function plus a starting point.

In our current function, there appears to be numerous roots. We can find them one at a time with different start points or we can use a “for” loop and find any number of them all at once.

Page 13: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Finding the Roots, cont. For example:

zero1=fzero(‘func_in_class’, -9)zero2=fzero(‘func_in_class’, -6)

To find multiple roots, try something like:

x=[-9 -6 -3 0 3 6 10]for i=1:7 xroot(i)=fzero('func_in_class', x(i)); %check the results using feval yroot(i)=feval('func_in_class', xroot(i));enddisp(xroot)disp(yroot)

Page 14: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Finding the area under the curve (integrating)

Three different functions in MATLAB use different approximation methods and provide the area under the curve between two points a an b:

trapz (this one sometimes gives large errorsand requires two arguments…x values and

y values) quad quadl

These are more precise than trapz and require three arguments…function name, start point, stop point

b

a

dxxfArea )(

Page 15: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Finding the area under the curve (integrating)

Examples of use:x=0:0.1:4y=feval(‘func_in_class’,x)area=trapz(x,y)

area=quad(‘func_in_class’,0,4)

area=quadl(‘func_in_class’,0,4)

Page 16: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Practice: Finding the area under the curve (integrating)

Using the “standnorm” function find the probability (which is represented by areas under the standard normal function) that z is between -2 and 2. How about between -3 and 3?

Using the “func_in_class” find the area under the curve between the first two roots between -10 and 0.

Page 17: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Plotting the integral of a function

To plot the integral, successively evaluate the area between the minimum range of x, and increasingly higher values, until the maximum of the x range…

step = .01;x = -10:step:10;for k = 1:length(x) int(k)=quadl('func_in_class', x(1), x(k));endfplot('func_in_class',[-10,10])holdplot(x,int,'--')title('Function and Integral')xlabel('x')ylabel('f(x)')

Page 18: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Numerical Differentiation

Unlike integration, differentiation is not as straightforward in MATLAB.

The MATLAB command “diff” is used to calculate the differences between successive points and this can be used to approximate a derivative of a function.

Page 19: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

To plot the derivative of an equation…

x = -10:.01:10;y = feval(‘func_in_class',x);der = diff(y)./diff(x);xm = (x(1 : (length(x)-1)) + x(2 : length(x)))/2;plot(xm,der)

True derivative is at a point. What we have done is to approximate the derivative between two points. Thus the approximation really applies to the point half way between the two points used to calculate the approximation (xm).

Example….

Page 20: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

Using “inline” The inline command allows you to define a function

within the workspace or within a script (you don’t need the function previously defined in an m-file).

You can also use the “input” command so that the user can provide the function then use the inline command.

Examples of use of inline:

myfunction=inline(‘sin(x)./(x.^2+1)’)quadl(myfunction,1,2)

OR

quadl(inline(‘sin(x)./(x.^2+1)’),1,2)

OR

yourfunction=input(‘What is your equation’,’s’)

myfunction=inline(yourfunction)

quadl(myfunction,1,2) Example…

Page 21: Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros

In class exercise: Write a script to do the following… Plot this function between -5 and 5

and put your names and group number on it using ‘gtext’

Print your plot Circle minimums and maximums Determine locations of minimums

and maximums within this range and hand-write the (x,y) points on the plot

Find the roots in this range (where y=0) and write the x values on the plot

Find the area under the curve between -2 and 2 and write it on the plot

Turn in plot

xxxf cos)5.()( 2