Equation Analysis: Plotting, Zeroes, Minima/Maxima, and Integration with MATLAB, Assignments of Engineering

Instructions on how to evaluate and analyze mathematical equations using matlab. Topics covered include plotting the function, finding zeroes (roots), minimums and maximums, and calculating the area under the curve (integration). The document also explains how to define equations as string variables and use matlab functions such as fplot, fminbnd, fzero, and eval.

Typology: Assignments

Pre 2010

Uploaded on 09/02/2009

koofers-user-vta
koofers-user-vta 🇺🇸

5

(1)

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
ENGR 0012 Dr. Lunds 10am Section
Tuesday, February 5th, 2007
Discussion of Writing Assignment 3
Analyzing Equations (Section 4.15) :
Plotting
Finding zeroes
Finding minima and maxima
Finding area under equation (integration)
Finding slope of equation (derivative)
Introduction to Matlab functions for
evaluation of equations
We are interested in evaluating the following
equation over the range of x between 10 and 10:
What does a plot of this function look like?
What are its maximums and minimums?
What are its roots (zeroes)?
What is the area under this function (integral)?
What is the derivative of this function?
1
2+
=x
)xsin(
)x(f
Equation Evaluation in MATLAB
In MATLAB, there are 3 ways to enter an
equation for evaluation
1. Enter equation as a function m-file
% Filename: func_x.m
function y = func_x(x)
y = sin(x)./(x.^2+1)
2. Enter equation as an inline function in main script
func_x = inline(sin(x)./(x.^2+1))
3. Enter equation as a string variable in main script
func_x=sin(x)./(x.^2+1)
Entering an equation as a string
variable for evaluation
Matlab has a special convention for defining an equation
that you wish to analyze in various ways. The convention
is to assign the equation as a string to the dependent, or
output, variable
We can pick any name for dependent variable, e.g. y,
func_f, voltage
func_f = sin(x)./(x.^2+1)
Note that the mathematical operators are element by
element matrix operators, because it is assumed the
input, or independent, variable x will be a vector array
We use this convention when we want to do other things
besides just calculating func_f at some set of values, x.
pf3
pf4
pf5

Partial preview of the text

Download Equation Analysis: Plotting, Zeroes, Minima/Maxima, and Integration with MATLAB and more Assignments Engineering in PDF only on Docsity!

ENGR 0012 – Dr. Lund’s 10am Section

Tuesday, February 5th, 2007

• Discussion of Writing Assignment 3

• Analyzing Equations (Section 4.15) :

  • Plotting
  • Finding zeroes
  • Finding minima and maxima
  • Finding area under equation (integration)
  • Finding slope of equation (derivative)

Introduction to Matlab functions for

evaluation of equations

  • We are interested in evaluating the following equation over the range of x between –10 and 10:
  • What does a plot of this function look like?
  • What are its maximums and minimums?
  • What are its roots (zeroes)?
  • What is the area under this function (integral)?
  • What is the derivative of this function?

x

sin(x)

f(x)

Equation Evaluation in MATLAB

In MATLAB, there are 3 ways to enter an

equation for evaluation

  1. Enter equation as a function m-file % Filename: func_x.m function y = func_x(x) y = sin(x)./(x.^2+1)
  2. Enter equation as an inline function in main script func_x = inline(‘sin(x)./(x.^2+1)’)
  3. Enter equation as a string variable in main script func_x=‘sin(x)./(x.^2+1 )’

Entering an equation as a string

variable for evaluation

  • Matlab has a special convention for defining an equation that you wish to analyze in various ways. The convention is to assign the equation as a string to the dependent, or output, variable
  • We can pick any name for dependent variable, e.g. y , func_f , voltage
  • func_f = ‘sin(x)./(x.^2+1)’
  • Note that the mathematical operators are element by element matrix operators, because it is assumed the input, or independent, variable x will be a vector array
  • We use this convention when we want to do other things besides just calculating func_f at some set of values, x.

How can you graph your equation?

• fplot ( function_name, [xmin, xmax] )

  • Ex: fplot(func_f, [-10, 10])
  • Function_f must be a variable with a string

assigned to it that contains an equation

  • The string equation must contain recognized

Matlab function commands with input

arguments

What are local minimums and maximums?

  • Points where function reaches a minimum or maximum value
  • In our example, there are 3 local minima, and 3 local maxima

Where do the minimums occur?

  • X = fminbnd (function_name, x1, x2)
  • Returns the x value for which function_name is a minimum within the range of x1 and x
  • Notice it does NOT return the minimum value, but rather the x location where the minimum occurs
  • Also notice that it will return only one x value corresponding to the lowest value of function_name for the specified range x1 to x
  • For our example, if we typed x = fminbnd(func_f, -10, 10), we would get the x value corresponding to the lowest of our three identified minimums

Minimums for our example…

  • If we want to determine the location of each of the 3

minimums for our func_f, we must rely on our visual

analysis of the plot to determine the general range

in which the minima occur

  • From the plot we see that the minima occur in the

ranges (-8 to -6), (-3 to 0) and (3 to 5)

  • xmin1 = fmin(func_f, -8, -6)

xmin2 = fmin(func_f, -3, 0)

xmin3 = fmin(func_f, 3, 5)

What are the zeroes of our equation?

• Using fplot, we anticipate zeroes in several

locations [-9, -6, -3, 0, 3, 6, 10]

• We can either type the fzero command for

each location, or we can use a for loop

Use of for loop to determine zeros

• x = [-9 –6 –3 0 3 6 10]

• for k = 1:length(x)

xzero(k) = fzero(func_f, x(k))

end

• y_zero = eval(func_f, xzero)

Entering equations as a function

  • An equation that has been stored as an .m function file can also be evaluated using the same MATLAB functions just described
  • The nice thing about equations stored as functions is that they can be used mathematically, once they have been created
  • The nice thing about string equations is that they can be input by the user from the keyboard, meaning a single script can be used for evaluation of any user supplied equation.
  • The solution to combining both of the above benefits into one method of equation analysis is to use the inline MATLAB function

Inline function creation

  • The inline command provides a means of creating a one output function in the main script, without having to create a separate .m file
  • The following command in the main script allows the use of the func_x function just as if it had been stored as a .m file… func_x = inline(‘sin(x)./(x.^2+1)’)
  • This would be equivalent to storing the following .m file… %File func_x.m function y = func_x(x) y = sin(x)./(x.^2+1);
  • The string expression in the argument of the inline command can be acquired using the input command.
  • An equation containing more than one input variable (i.e. more than one argument) will automatically be determined unless specified (see help inline)

Strings vs. m-file or inline Functions

sfunc = ‘cos(x)./(x.^2 + 1)’

function y = mfunc(x) y = cos(x)./(x.^2 + 1)

fplot (sfunc, [dmin, dmax]) xzero = fzero (sfunc , x0)

xmin = fminbnd (sfunc, x1, x2)

fplot (mfunc , [dmin, dmax]) xzero = fzero (mfunc, x0) xmin = fminbnd (mfunc, x1, x2)

fplot (‘mfunc ’, [ dmin, dmax]) xzero = fzero (‘mfunc ’, x0) xmin = fminbnd (‘mfunc ’, x1, x2)

Errors

You must place quotes around m-file name

to correct…

String Function M-File Function

mfunc = inline(‘cos(x)./(x.^2 + 1’)

inline Function

String vs M-File Function

fzero x = fzero (func_s, x0) x = fzero (‘func_m’, x0)

fminbnd x = fminbnd (func_s, x1, x2) x = fminbnd (‘func_m’, x1, x2)

quad, quadl A = quadl (func_s, x1,x2) A = quadl (‘func_m’, x1,x2)

trapz, diff No function in arguments No function in arguments

feval Cannot be used y = feval (‘func_m’, x)

eval y = eval (func_s) Cannot be used

fplot fplot (func_s, [-10 10]) fplot (‘func_m’, [-10 10])

Command String Function M-File Function

sfunc = ‘cos(x)./(x.^2+1)’

function y = mfunc(x) y = cos(x)./(x.^2+1)

eval vs. feval

The only Matlab command that will not work for the variable gen_func is eval.

eval can only be used with a string function feval can only be used with a m-file function

if func_type == 1 x = xmin ymin = eval(gen_func ) else ymin = feval(gen_fucn, xmin) end

Thus, anywhere in your code where you must evaluate the function at certain x values, you would have to incorporate the following condition …

Example: Determining y values at local minima

How can I find the area under a portion

of my curve (i.e. integrate)?

Numerical Differentiation

  • Numerical differential approximates taking the derivative of a curve
  • The derivative can be approximated by using the diff command
  • DIFF (X), for a vector X, is [X(2)-X(1), X(3)-X(2), ... X(n)-X(n-1)].
  • Derivative = diff(y)./diff(x)
  • The smaller the distance between x points, the more accurate the derivative approximation

x

y

dx

dy

y

x ∆

∆ → 0

lim

To plot the derivative of an equation…

  • step = 0.

x = x1:step:x

y = eval(func_s)

der = diff(y)./diff(x)

xm = (x(1 : (length(x)-1)) + x(2 : length(x)))/

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 use to calculate the approximation (xm).