Lecture 5.

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
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,
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Introduction to Matlab
Introduction to Graphing Using MATLAB. Line Graphs  Useful for graphing functions  Useful for displaying data trends over time  Useful for showing.
Matlab Graphics S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: 2D Graphics.
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.
1 Chapter 4 Curve Plotting with MATLAB MATLAB provides some very powerful features for plotting and labeling curves. These operations can be performed.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Suggested problems from text (6 th edition) Chapter 3.1 p85 Problems 1, 4, 9, 10 Computer problems 1, 2, 4, 7 Chapter 3.2 p101 Problems 4, 15, 17, 19 Computer.
Flow Charts, Loop Structures
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Exam #3 Review: Comprehensive Exam Class 14.2 Palm Matlab Book Ch. 1-5.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Solving Quadratic (and polynomial) Equations by Factoring.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 4 : Polynomial Edward Cheung Room W311g 2008.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2010.
Recap Function M-files Syntax of Function M-Files Comments Multiple Input and Output Functions.
Intro to Matlab 1.Using scalar variables 2.Vectors, matrices, and arithmetic 3.Plotting 4.Solving Systems of Equations Can be found at:
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Newton’s Method, Root Finding with MATLAB and Excel
Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
MATHPOWER TM 12, WESTERN EDITION Chapter 5 Trigonometric Equations.
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:
2-2 What is a Matlab function? How does the plot function work? What Matlab functions are available to do Data Analysis? How can you create your own function.
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Matlab Tutorial Iman Moazzen First Session – September 11, 2013.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Computer Application in Engineering Design
Lecture 25.
MATLAB DENC 2533 ECADD LAB 9.
User Defined Functions
MATH 493 Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
Introduction to MATLAB
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Experiment No. (1) - an introduction to MATLAB
Using Script Files and Managing Data
Matlab Basics Tutorial
Presentation transcript:

Lecture 5

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.2.2,4.3.3,5.6, 6.1,6.3

fplot Use the fplot command to plot either built-in or user defined functions (see Matlab book 3.8) >> 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]))

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.

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

Using fzero - An Example Problem: Find a root of the function f(x) = sin(x) near x= .5 . >> fzero(@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 @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.7035

fzero - Matlab function One 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)

Using fzero - when it doesn't find a root If fzero cannot find a root then it will tell you. Example >> 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 = NaN NaN means “not a number”

If you don’t believe this then “seeing is believing” so 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]);

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 4.3.3). Consider the example on the next slide. As a naming convention, we will use all capital letters for global variables.

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.

Function - total Natural-Language Algorithm 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);

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, x]; result = sum(LIST);

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?

Function - total

Function - total (continued from previous slide)

Function - total (LIST = empty vector) (continued from previous slide) We can reset LIST from the Matlab prompt. (LIST = empty vector)

3-D Plotting Example: 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)

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

3-D Plotting X = Y =

3-D Plotting X Y

3-D Plotting >> Z = cos(X).*cos(Y).*exp(-sqrt(X.^2+Y.^2)./4) Z = Step 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 =

3-D Plotting X Y Z

3-D Plotting >> R = sqrt(X.^2+Y.^2); Step 4) compute Z by using the X and Y created by meshgrid, >> R = sqrt(X.^2+Y.^2); >> Z = cos(X).*cos(Y).*exp(-R./4); Step 5) graph in 3-D by applying the surface function, >> surf(X,Y,Z);

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 and raw data for which we don’t 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 .