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

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
259 Lecture 17 Working with Data in MATLAB. Overview  In this lecture, we’ll look at some commands that are useful for working with data!  fzero  sum,
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Functional analysis y = f(x) things to do 1. sketch graph 2. find roots (zeros) 3. find.
Exponential Functions Logarithmic Functions
Introduction to Matlab. I use Matlab for: Data analysis Data plotting Image Analysis Also – Simulations (solving odes/pdes/finite element methods) – Minimisations,
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
1 The Basics of Regression Regression is a statistical technique that can ultimately be used for forecasting.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter ,
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
The Real Zeros of a Polynomial Function
Dividing Polynomials.
A quadratic equation is a second degree polynomial, usually written in general form: The a, b, and c terms are called the coefficients of the equation,
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
3 DERIVATIVES.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
1 Chapter 1 MATLAB Primer This introductory chapter is relatively short and has as its main objective the introduction of MATLAB ® to the reader. This.
AP CALCULUS AB Chapter 4: Applications of Derivatives Section 4.1:
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Chapter 1 Computing Tools Analytic and Algorithmic Solutions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
DERIVATIVES 3. If it were always necessary to compute derivatives directly from the definition, as we did in the Section 3.2, then  Such computations.
Department of Mechanical Engineering, LSU Session IV MATLAB Tutorials Session IV Mathematical Applications using MATLAB Rajeev Madazhy
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
CSC 211 Data Structures Lecture 13
3.3 Rules for Differentiation What you’ll learn about Positive integer powers, multiples, sums, and differences Products and Quotients Negative Integer.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Scientific Computing Introduction to Matlab Programming.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
First, a little review: Consider: then: or It doesn’t matter whether the constant was 3 or -5, since when we take the derivative the constant disappears.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Mathematical Applications using MATLAB (Cont….)
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem.
Using Derivatives to Sketch the Graph of a Function Lesson 4.3.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical calculus and differential equations.
Applications of Loops: The power of MATLAB Mathematics + Coding
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Recap Cubic Spline Interpolation Multidimensional Interpolation Curve Fitting Linear Regression Polynomial Regression The Polyval Function The Interactive.
More Functions in MATLAB. Functions that operate on other functions A function F() can take another function G() as an argument by using a notation:
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Basis of Mathematical Modeling LECTURE 3 Numerical Analysis with MATLAB Dr. N.K. Sakhnenko, PhD, Professor Associate.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
1 Lecture 8 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
CMPSC 200 Fall 2013 Lecture 37 November 18, 2013.
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to Programming for Mechanical Engineers
Introduction to Summary Statistics
Applications of User Defined functions in MATLAB
Discrete Least Squares Approximation
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

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

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

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.

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). 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)’;

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);

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!!

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

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)

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

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).

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

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.

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=[ ] 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)); end disp(xroot) disp(yroot)

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 errors and 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

Finding the area under the curve (integrating) Examples of use: x=0:0.1:4 y=feval(‘func_in_class’,x) area=trapz(x,y) area=quad(‘func_in_class’,0,4) area=quadl(‘func_in_class’,0,4)

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.

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)); end fplot('func_in_class',[-10,10]) hold plot(x,int,'--') title('Function and Integral') xlabel('x') ylabel('f(x)')

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.

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….

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…

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