tailieunhanh - A Guide to MATLAB for Beginners and Experienced Users phần 3

Kết quả này có thể là khó hiểu nếu bạn đang mong đợi f để hành động như một chức năng. Kể từ khi f là một chuỗi, f (7) là bắt nhân vật thứ bảy trong e là 1 (số lượng không gian). Nhớ lại rằng, giống như biểu tượng đầu ra, đầu ra chuỗi không thụt vào từ lề trái. | Functions and Expressions 49 In MATLAB an expression can belong to either the string or the symbolic class of data. Consider the following example. f x 3 - 1 f 7 ans 1 This result may be puzzling if you are expecting f to act like a function. Since f is a string f 7 denotes the seventh character in f which is 1 the spaces count . Recall that like symbolic output string output is not indented from the left-hand margin. This is a clue that the answer above is a string consisting of one character not a floating-point number. Typing f 5 would yield - and f -1 would produce an error message. You have learned three ways to define your own functions using @ to create an anonymous function using inline see Chapter 2 for these or using an M-file see Chapter 3 . Anonymous or inline functions are most useful for defining simple functions that can be expressed in one line and for turning the output of a symbolic command into a function. Function M-files are useful for defining functions that require several intermediate commands to compute the output. Most MATLAB commands are actually M-files and you can peruse them for ideas to use in your own M-files -to see the M-file for say the command mean you can enter type mean. See also More about M-files below. Some commands such as ode45 a numerical ordinary differential equation ODE solver which we use later in Chapter 10 require their first argument to be a function - to be precise either an inline function as in ode4 5 f 0 2 1 or a function handle that is the name of a built-in function or a function M-file preceded by the special symbol @ as in ode45 @func 0 2 1 . The @ syntax was introduced in MATLAB 6 in earlier versions of MATLAB the substitute was to enclose the name of the function in single quotes to make it a string. But with or without quotes typing a symbolic expression as the first input to ode4 5 gives an error message. On the other hand most symbolic commands require their first argument to be either a string or a .

TỪ KHÓA LIÊN QUAN