TAILIEUCHUNG - APPLIED NUMERICAL METHODS USING MATLAB phần 2

Hai âm mưu lệnh cùng với sinc1 (t, D) và sinc1 (t) sản lượng trong hai đồ thị đẹp, tương ứng, như mô tả trong hình. . Điều quan trọng là cần lưu ý rằng sinc1 () không làm phiền chúng tôi và hoạt động tốt mà không có đầu vào đối số thứ hai D. | 42 MATLAB USAGE AND COMPUTATIONAL ERRORS Figure The graphs of a sinc function defined by sinc1 . D bl -2 b2 2 t b1 0 200 200 b2 - b1 plot t sinc1 t D axis b1 b2 hold on plot t sinc1 t k The two plotting commands coupled with sinc1 t D and sinc1 t yield the two beautiful graphs respectively as depicted in Fig. . It is important to note that sinc1 doesn t bother us and works fine without the second input argument D. We owe the second line in the function sinc1 for the nice errorhandling service if nargin 2 D 1 end This line takes care of the case where the number of input arguments nargin is less than 2 by assuming that the second input argument is D 1 by default. This programming technique is the key to making the MATLAB functions adaptive to different number type of input arguments which is very useful for breathing the user-convenience into the MATLAB functions. To appreciate its role we remove the second line from the M-file defining sinc1 and then type the same statement in the Command window trying to use sinc1 without the second input argument. plot t sinc1 t k Input argument D is undefined. Error in C MATLAB6p5 nma On line 4 x sin pi t D . pi t D This time we get a serious red error message with no graphic result. It is implied that the MATLAB function without the appropriate error-handling parts no longer allows the user s default or carelessness. Now consider the third line in sinc1 which is another error-handling statement. t find t 0 eps TOWARD GOOD PROGRAM 43 or equivalently for i 1 length t if t i 0 t i eps end end This statement changes every zero element in the t vector into eps . What is the real purpose of this statement It is actually to remove the possibility of division-by-zero in the next statement which is a mathematical expression having t in the denominator. x sin pi t D . pi t D To appreciate the role of the third line in sinc1 we remove it from the M-file defining sinc1 and type the following .

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.