1 / 37

User-Defined Functions charturong.ee.engr.tu.ac.th/CN208

User-Defined Functions charturong.ee.engr.tu.ac.th/CN208. จาตุรงค์ ตันติบัณฑิต ภาควิชาวิศวกรรมไฟฟ้าและคอมพิวเตอร์ มหาวิทยาลัยธรรมศาสตร์. เอกสารประกอบการสอนนี้จัดทำโดย ดร.ทรงยศ นาคอริยกุล. Introduction.

brand
Download Presentation

User-Defined Functions charturong.ee.engr.tu.ac.th/CN208

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. User-Defined Functionscharturong.ee.engr.tu.ac.th/CN208 จาตุรงค์ ตันติบัณฑิต ภาควิชาวิศวกรรมไฟฟ้าและคอมพิวเตอร์ มหาวิทยาลัยธรรมศาสตร์ เอกสารประกอบการสอนนี้จัดทำโดย ดร.ทรงยศ นาคอริยกุล

  2. Introduction • เราได้เรียนรู้การออกแบบโปรแกรมแบบ top-down design โดยการแตกปัญหาใหญ่ให้เป็นปัญหาย่อยๆซึ่งง่ายต่อการเข้าใจและแก้ไขจากนั้นจึงเขียนโปรแกรมแก้ปัญหาย่อยๆเหล่านี้ ก่อนที่จะนำโปรแกรมย่อยๆเหล่านี้มารวมกันเพื่อใช้แก้ปัญหาใหญ่ที่เรามี • ในการออกแบบโปรแกรมแบบ top-down design นี้ โปรแกรม MATLAB ให้ผู้เขียนมีโอกาสเขียนฟังก์ชั่น (function) ของตัวเอง โดยเขียนหนึ่งฟังก์ชั่นสำหรับแก้หนึ่งปัญหาย่อย จากนั้นผู้เขียนสามารถนำฟังก์ชั่นเหล่านี้มารวมกันเพื่อแก้ปัญหาใหญ่ที่ได้รับ • M-files ที่เราได้ฝึกเขียนมาทั้งหมดไม่ใช่ฟังก์ชั่นแต่เป็น script files (การรวมเอาคำสั่ง MATLAB มารวมกันและประยุกต์ใช้ในหนึ่งโปรแกรม)

  3. ประโยชน์ของการเขียนฟังก์ชั่นของตัวเองประโยชน์ของการเขียนฟังก์ชั่นของตัวเอง • ตรวจสอบได้ง่ายและไม่ขึ้นกับโปรแกรมอื่น (independent testing of sub-tasks) • สามารถนำมาใช้ได้อีกหลายครั้ง (reusable code) • ป้องกันข้อผิดพลาดได้ดี (isolation from unintended side effects)

  4. การเขียนฟังก์ชั่นของตัวเองการเขียนฟังก์ชั่นของตัวเอง • การเขียน MATLAB ฟังก์ชั่นต้องเขียนเป็น M-file พิเศษที่ทำงานโดยมีรูปแบบดังต่อไปนี้ • function [outarg1, outarg2, …] = fname(inarg1, inarg2, …) • % H1 comment line • % Other comment lines • (Executable code) • … • (end)

  5. คำสั่ง functionใช้แสดงจุดเริ่มต้นของฟังก์ชั่น ในบรรทัดแรกคำสั่ง functionบ่งบอกชื่อของฟังก์ชั่น และข้อมูลที่ใช้ (input) และผลลัพธ์ของฟังก์ชั่น (output) โดยที่ input argument list (inarg1, inarg2, …) จะปรากฎในเครื่องหมายวงเล็บ ( )(parentheses) หลังชื่อของฟังก์ชั่น และ output argument list (outarg1, outarg2, …) จะปรากฎในเครื่องหมายวงเล็บสี่เหลี่ยม [ ](brackets) ทางด้านซ้ายมือของเครื่องหมายเท่ากับ ถ้ามี output แค่ output เดียว เราไม่จำเป็นต้องใช้เครื่องหมายวงเล็บสี่เหลี่ยม • แต่ละฟังก์ชั่นต้องถูกเซฟเป็น .m file โดยใช้ชื่อของไฟล์เป็นชื่อเดียวกันกับชื่อฟังก์ชั่น นั่นคือถ้าหากเราตั้งชื่อฟังก์ชั่นว่า My_fun ฟังก์ชั่นนั้นต้องถูกเซฟโดยใช้ชื่อ My_fun.m

  6. ใน input argument list จะระบุชื่อและจำนวนของตัวแปรที่จะใช้ในฟังก์ชั่นนั้น ชื่อของตัวแปรนั้นเป็นแบบตัวแปรชั่วคราว หรือ dummy argument ซึ่งใช้สำหรับรับค่าข้อมูลที่แท้จริงเวลาที่ฟังก์ชั่นถูกเรียกใช้ ชื่อของตัวแปรเหล่านี้จะถูกกำจัดทิ้งหลังจากที่ MATLAB ประมวลผลฟังก์ชั่นนั้นเสร็จสิ้นแล้ว • ใน output argument list จะระบุชื่อและจำนวนของตัวแปรที่ฟังก์ชั่นนั้นจะให้ผลลัพธ์ออกมาหลังจากที่ฟังก์ชั่นนั้นทำการประมวลผลเสร็จแล้ว ซึ่งมีการทำงานคล้ายกับ input argument list • การใช้คำสั่ง end เพื่อบอกว่าเป็นจุดสิ้นสุดของฟังก์ชั่นมีการเริ่มใช้ตั้งแต่ MATLAB 7.0 เป็นต้นไป ซึ่งการเขียนคำสั่ง end เพื่อจบฟังก์ชั่นนั้นจะเขียนหรือไม่เขียนก็ได้ เวลาเขียนคำสั่ง end เพื่อจบฟังก์ชั่นมักจะตามด้วยการเขียน comment เพื่อระบุว่าสำหรับฟังก์ชั่นไหน

  7. การเขียน comment บรรทัดแรกในฟังก์ชั่น หรือที่เรียกว่า H1 comment line นั้น ใช้เพื่อระบุหน้าที่หรือจุดประสงค์ของฟังก์ชั่นนั้น ซึ่ง comment บรรทัดแรกมีความพิเศษที่จะถูกค้นหาและแสดงผลบนหน้าจอเมื่อใช้คำสั่ง lookfor ในหน้าต่างคำสั่งใน MATLAB ส่วน comment บรรทัดที่เหลือที่ตามมาหลังจาก H1 comment line จนกระทั่งถึงบรรทัดว่างบรรทัดแรกหรือคำสั่งที่ทำงานได้ของ MATLAB จะถูกแสดงโดยใช้คำสั่ง help

  8. ตัวอย่างที่ 1 การเขียนฟังก์ชั่น • function distance = dist2 (x1, y1, x2, y2) • % dist2 calculates the distance between two points • % Function dist2 calculates the distance between • % two points (x1, y1) and (x2, y2) in a Cartesian • % coordinate system • % Calculate distance • distance = sqrt((x2-x1).^2 + (y2-y1).^2); • end%function distance

  9. การค้นหาฟังก์ชั่นที่เขียนด้วยตัวเองการค้นหาฟังก์ชั่นที่เขียนด้วยตัวเอง • เมื่อเขียนฟังก์ชั่น dist2 เสร็จ ให้เซฟลงบนฮาร์ดดิสค์คอมพิวเตอร์โดยใช้ชื่อ dist2.m • บนหน้าต่างคำสั่งของ MATLAB (command window) เราสามารถทดลองใช้คำสั่ง lookfor หรือ help เพื่อหาคำแนะนำเกี่ยวกับฟังก์ชั่น dist2 ซึ่ง MATLAB จะแสดงผลลัพธ์ออกบนหน้าจอทุกคำสั่งที่มีชื่อ dist2 ดังนี้ • >> lookfor dist2 • dist2 calculates the distance between two points • DIST2STR Distance conversion to a string • EUCDIST2 Compute 2-D Euclidean distance transform. • การใช้คำสั่ง help จะแสดง comment ใน dist2 ไปจนถึงบรรทัดที่ว่าง • >> help dist2 • dist2 calculates the distance between two points • Function dist2 calculates the distance between • two points (x1, y1) and (x2, y2) in a Cartesian • coordinate system

  10. การใช้ฟังก์ชั่น dist2 • เราสามารถเรียกใช้ฟังก์ชั่น dist2 ได้สองวิธี คือ • 1. ในหน้าต่าง command window โดยตรง เช่นหากเราต้องการหาระยะทางระหว่างจุด • (0, 1) กับ จุด (2, 4) ก็ทำได้ดังนี้ • >> out = dist2 (0, 1, 2, 4) • out = 3.6056 • ซึ่งจะเห็นได้ว่าผลลัพธ์ของฟังก์ชั่น dist2 ไม่จำเป็นต้องใช้ชื่อตัวแปรว่า distance แต่จะเป็นชื่อตัวแปรใดๆก็ได้ • 2. เขียนและเรียกใช้ใน script file เหมือนการเรียกใช้ฟังก์ชั่น MATLAB ทั่วไป

  11. ตัวอย่างการใช้ฟังก์ชั่นของตัวเองใน script file • % Script file: test_dist2.m • % • % Purpose: This program tests function dist2 • % Get input data • disp('Calculate the distance between two points:'); • ax = input('Enter x value of point a:'); • ay = input('Enter y value of point a:'); • bx = input('Enter x value of point b:'); • by = input('Enter y value of point b:'); • % Evaluate function • result = dist2(ax, ay, bx, by); • % Write out result • fprintf('The distance between points a and b is %f\n', result);

  12. ซึ่งผลลัพธ์ของการ run ของ script file ข้างต้นที่ชื่อ test_dist2 มีดังนี้ • >> test_dist2 • Calculate the distance between two points: • Enter x value of point a: 0 • Enter y value of point a: 1 • Enter x value of point b: 2 • Enter y value of point b: 4 • The distance between points a and b is 3.605551

  13. ตัวอย่างที่ 2 การเขียนฟังก์ชั่น • จงเขียนไฟล์ฟังก์ชั่นเพื่อหาตัวประกอบทั้งหมดของเลขจำนวนเต็มบวกที่กำหนดให้ กำหนดให้ฟังก์ชั่นมีรูปแบบการเรียกใช้งานคือ fac = find_factor(num) โดยที่ num เป็นตัวเลขจำนวนเต็มบวก และ เอาต์พุต fac เป็นเวกเตอร์ที่มีตัวประกอบของตัวเลข num เป็นสมาชิก

  14. functionfac = find_factor(num) • % find_factor finds all of the factors of a positive integer • ind = 1; • for ii = 1:num • % if num/ii has a remainder of 0, ii is a factor of num • if mod(num, ii) = = 0 • fac(ind) = ii; • ind = ind + 1; • end • end • end% function find_factor

  15. การผ่านค่าตัวแปรในฟังก์ชั่นของ MATLAB • โปรแกรม MATLAB ใช้เทคนิคที่เรียกว่า pass-by-value ในการเรียกใช้ฟังก์ชั่น เมื่อฟังก์ชั่นถูกเรียกใช้ MATLAB จะสร้างสำเนา (copy) ของตัวแปรอินพุตน์และใช้ตัวแปรสำเนานี้ในฟังก์ชั่น นั่นคือแม้ในฟังก์ชั่นค่าของตัวแปรอินพุตน์จะถูกเปลี่ยน ค่าของตัวแปรต้นฉบับจะไม่ถูกเปลี่ยนแปลง • ข้อดีของการใช้เทคนิค pass-by-value คือช่วยป้องกันการผิดพลาดในการเขียนโปรแกรม ถึงแม้ในฟังก์ชั่นจะมี bugs แต่จะไม่มีผลกระทบต่อค่าของตัวแปรต้นฉบับ

  16. ยกตัวอย่างการผ่านค่าตัวแปรแบบ pass-by-value โดยการสร้างฟังก์ชั่น sample ดังนี้ • function out = sample(a, b) • fprintf('In function sample: a = %4.2f, b = %4.2f %4.2f\n', a, b); • a = b(1) + 2*a; • b = a .* b; • out = a + b(1); • fprintf('In function sample: a = %4.2f, b = %4.2f %4.2f\n', a, b); • เราสามารถทดลองการผ่านค่าตัวแปรโดยเขียน script file ชื่อ test_sample.m ดังนี้ • a = 2; b = [6 4]; • fprintf('Before function sample: a = %4.2f, b = %4.2f %4.2f\n', a, b); • out = sample(a, b); • fprintf('After function sample: a = %4.2f, b = %4.2f %4.2f\n', a, b); • fprintf('After function sample: out = %4.2f\n', out);

  17. ผลลัพธ์ที่ได้จากการประมวลผลโปรแกรม test_sample ในหน้าต่างคำสั่งคือ • >> test_sample • Before function sample: a = 2.00, b = 6.00 4.00 • In function sample: a = 2.00, b = 6.00 4.00 • In function sample: a = 10.00, b = 60.00 40.00 • After function sample: a = 2.00, b = 6.00 4.00 • After function sample: out = 70.00 • ซึ่งจะเราจะเห็นได้ว่า ค่าของตัวแปร a และ b ถูกเปลี่ยนภายในฟังก์ชั่น sample แต่การเปลี่ยนแปลงนี้ไม่มีผลกระทบต่อตัวแปรต้นฉบับ

  18. การเรียงตัวเลข (sorting) • การเรียงตัวเลข (sorting) คือการนำตัวเลขทั้งหมดในฐานข้อมูลมาจัดลำดับจากมากไปน้อย หรือ จากน้อยไปมาก ซึ่งการเรียงตัวเลขนี้มีประโยชน์อย่างมากทางวิศวกรรม • เราฝึกเรียงตัวเลขอยู่เป็นประจำ จึงทำให้ปัญหานี้ดูเป็นเรื่องที่ง่าย เช่นการเรียง [10 3 -1 5 8] จากน้อยไปมากเป็น [-1 3 5 8 10] นี้ เราทำได้อย่างไร? • ขั้นตอนการเรียงตัวเลขที่เราคุ้นเคยคือ เราหาตัวเลขที่มีค่าน้อยที่สุดจากตัวเลขทั้ง 5 ก่อน ซึ่งนั่นคือตัวเลข -1 จากนั้นเราจึงมองหาตัวเลขที่น้อยที่สุดตัวต่อไปในเลขที่เหลือ [10 3 5 8] ซึ่งคือตัวเลข 3 ทำอย่างนี้ต่อไปเรื่อยจนกระทั่งตัวเลขทั้งหมดถูกเรียง กระบวนการเรียงตัวเลขแบบนี้เรียกว่า selection sort

  19. Selection Sort • การเรียงตัวเลขที่เรียกว่า Selection Sort คือกระบวนการเรียงตัวเลขที่ง่ายที่สุด ซึ่งการเรียงจากน้อยไปมากทำได้อย่างที่ได้กล่าวมาแล้วในเบื้องต้น • ค้นหาตัวเลขที่มีค่าน้อยที่สุดในเวกเตอร์ และนำค่าตัวเลขนั้นมาไว้เป็นตัวแรกสุดของแถวโดยสลับเปลี่ยนตัวเลขนั้นกับเลขตัวแรกของแถว แต่ถ้าค่าตัวแรกของแถวมีค่าน้อยสุดอยู่แล้ว ไม่ต้องทำอะไร • ตรวจตัวเลขตั้งแต่ตัวที่ 2 เป็นต้นไป เพื่อค้นหาตัวเลขที่มีค่าน้อยที่สุดตัวต่อไป และทำการสลับเปลี่ยนตัวเลขนั้นกับเลขตัวที่สองของแถว แต่ถ้าค่าตัวที่สองของแถวมีค่าน้อยที่สุดเป็นตัวที่สองอยู่แล้ว ไม่ต้องทำอะไร • ทำแบบนี้ไปเรื่อยๆ จนกระทั่งเหลือแค่ 2 ตัวสุดท้าย หลังจากทำการสลับเปลี่ยนตัวเลขสองตัวสุดท้ายในเวกเตอร์แล้ว เป็นอันจบการเรียงตัวเลข

  20. สลับ ไม่ต้องสลับ สลับ สลับ

  21. selection_sort(arr) • เราสามารถเขียน pseudocode สำหรับ selection_sort ได้ดังต่อไปนี้ • for (each index position ii in the arr) • find kk (the index the smallest value between ii and the end of the array arr) • swap the elements at index position ii and kk • end

  22. selection_sort.m • function out = selection_sort(arr) • % selection_sort sorts data in ascending order • nvals = length(arr); • for ii = 1:(nvals-1) • kk = find_smallest_number(arr, ii); • % swap the elements at index position ii and kk if ii ~= kk • if ii ~= kk • temp = arr(ii); • arr(ii) = arr(kk); • arr(kk) = temp; • end • end • out = arr; • end% function selection_sort

  23. function ind = find_smallest_number(arr, ii) • % find_smallest_number finds the index of the smallest number in arr between ii • % and the end of the arr • nvals = length(arr); • ind = ii; • for jj = (ii+1):nvals • if arr(jj) < arr(ind) • ind = jj; • end • end • end% function find_smallest_number

  24. Bubble Sort • การเรียงตัวเลขแบบ bubble sort เริ่มต้นการเรียงโดยการเปรียบเทียบเลข 2 ตัวแรกในฐานข้อมูล และสับเปลี่ยนตำแหน่งตัวเลข 2 ตัวนี้ให้อยู่ในตำแหน่งที่ถูกต้อง จากนั้นจะทำการเปรียบเทียบตัวเลขตัวที่ 2 และตัวที่ 3 ในฐานข้อมูลต่อไป และทำการสับเปลี่ยนตำแหน่งถ้าจำเป็น ทำแบบนี้ต่อไปเรื่อยๆจนจบแถว จากนั้นเราจะเริ่มต้นเปรียบเทียบเลข 2 ตัวแรกในฐานข้อมูลใหม่แบบนี้ต่อไปเรื่อยๆ จนกว่ารอบนั้นจะไม่มีการสับเปลี่ยนเกิดขึ้น จึงเป็นอันจบการเรียงตัวเลขแบบ bubble sort

  25. ตัวอย่างการเรียงตัวเลขแบบ bubble sort ของ [10 3 -1 5 8] ทำได้ดังนี้ • เรียงรอบที่ 1: • [10 3 -1 5 8]  [3 10 -1 5 8] จัดอันดับ 2 ตัวเลขแรก • [3 10-1 5 8] [3 -110 5 8]จัดอันดับ 2 ตัวเลขต่อไป • [3-1 105 8] [3-1 5 10 8] • [3-1 5 108]  [3-1 5 810] • เรียงรอบที่ 1 มีการสับเปลี่ยนตำแหน่ง 4 ครั้ง • เรียงรอบที่ 2: • [3 -1 58 10]  [-1 3 58 10] • [-1 3 5 8 10]  [-1 3 5 8 10] • [-1 3 5 8 10]  [-1 3 5 8 10] • [-1 35 810]  [-1 35 810] • เรียงรอบที่ 2 มีการสับเปลี่ยนตำแหน่ง 1 ครั้ง

  26. เรียงรอบที่ 3: • [-1 3 58 10]  [-1 3 58 10] • [-1 3 5 8 10]  [-1 3 5 8 10] • [-1 3 5 8 10]  [-1 3 5 8 10] • [-1 35 810]  [-1 35 810] • เรียงรอบที่ 3 มีการสับเปลี่ยนตำแหน่ง 0ครั้งเพราะฉะนั้นจบการเรียงแบบ bubble sort

  27. bubble_sort(arr) • เราสามารถเขียน pseudocode สำหรับ selection_sort ได้ดังต่อไปนี้ • whileswap occurs • % compare two adjacent numbers and swap their positions if necessary • for ii = 1:(nvals-1) • if arr(ii) > arr(ii+1) • temp = arr(ii); • arr(ii) = arr(ii+1); • arr(ii+1) = temp; • end • end • end

  28. bubble_sort.m • function out = bubble_sort(arr) • % bubble_sort sorts data in ascending order • swap = 1; • nvals = length(arr); • while swap == 1 % while swap = = 1, continue sorting • swap = 0; • for ii = 1:(nvals-1) • if arr(ii) > arr(ii+1) • temp = arr(ii); • arr(ii) = arr(ii+1); • arr(ii+1) = temp; • swap = 1; % swap = 1 indicates that swapping occurs • end • end • end • out = arr; • end% function bubble_sort

  29. Optional Arguments • ฟังก์ชั่น MATLAB มีการใช้ตัวแปรอินพุตและเอาต์พุตแบบมีหรือไม่มีก็ได้ (optional input and output arguments) ยกตัวอย่างเช่น การใช้ฟังก์ชั่น plot ซึ่งมีตัวแปรอินพุตตั้งแต่ 2 ตัว ไปจนถึงมากกว่า 7 ตัว เป็นต้น ซึ่ง MATLAB รู้ได้อย่างไรว่าฟังก์ชั่นเหล่านี้มีตัวแปรอินพุต หรือ เอาต์พุตกี่ตัว • MATLAB มีฟังก์ชั่นพิเศษสำหรับอ่านจำนวนตัวแปรในฟังก์ชั่น และ แสดงคำเตือนดังต่อไปนี้ • nargin – ให้ค่าจำนวนของตัวแปรอินพุตในฟังก์ชั่น • nargout – ให้ค่าจำนวนของตัวแปรเอาต์พุตในฟังก์ชั่น • nargchk – แสดงข้อความว่ามีความผิดพลาดโดยอัตโนมัติ ถ้าฟังก์ชั่นที่ถูกเรียกมีจำนวนตัวแปรอินพุตหรือเอาต์พุตมากหรือน้อยเกินไป • error – แสดงข้อความว่ามีข้อผิดพลาด (error message) และยกเลิกการเรียกฟังก์ชั่น • warning – แสดงคำเตือนแต่ให้ฟังก์ชั่นทำงานต่อไป • inputname -- ให้ชื่อของตัวแปรอินพุตตามตำแหน่งที่ถูกระบุ

  30. 1. ฟังก์ชั่น nargin และ nargout ต้องถูกเรียกใช้ภายในการเขียนฟังก์ชั่น ซึ่งจะให้ค่าจำนวนตัวแปรอินพุตและเอาต์พุตเวลาที่ฟังก์ชั่นถูกเรียกใช้ • 2. ฟังก์ชั่น nargchk สร้างข้อความที่แสดงข้อผิดพลาดถ้าฟังก์ชั่นมีจำนวนตัวแปรอินพุตหรือเอาต์พุตน้อยเกินไป มีรูปแบบการใช้งานคือ • message = nargchk(min_args, max_args, num_args); • โดยที่ min_args เป็นจำนวนน้อยที่สุดของตัวแปรที่เป็นไปได้ max_args เป็นจำนวนมากที่สุดของตัวแปรที่เป็นไปได้ และ num_args เป็นจำนวนตัวแปรที่แท้จริงที่ถูกเรียกใช้ ถ้าจำนวนตัวแปรไม่อยู่ภายในจำนวนที่กำหนดไว้ ข้อความผิดพลาดจะถูกแสดงอัตโนมัติในตัวแปร message • 3. ฟังก์ชั่น error ใช้แสดงข้อความว่ามีข้อผิดพลาดและยกเลิกการเรียกฟังก์ชั่นนั้น โดยมีการเรียกใช้คือ error(‘msg’) โดยที่ msg คือข้อความที่เรากำหนดให้แสดงเวลาข้อผิดพลาดเกิดขึ้น

  31. 4. ฟังก์ชั่น warning ใช้แสดงคำเตือนซึ่งจะระบุชื่อฟังก์ชั่น และบรรทัดที่ปัญหาเกิดขึ้น แต่จะปล่อยให้ฟังก์ชั่นทำงานต่อไป โดยมีการใช้คือ warning(‘msg’) โดยที่ msg เป็นข้อความที่แสดงคำเตือน • 5. ฟังก์ชั่น inputname ใช้แสดงชื่อของตัวแปรอินพุตเวลาที่ฟังก์ชั่นถูกเรียกใช้จริง มีรูปแบบการใช้คือ name = inputname(argno); โดยที่ argno เป็นตำแหน่งของตัวแปรอินพุต เช่น • function myfun(x, y, z) • name = inputname(2); • disp(['The second argument is named ' name]); • เมื่อฟังก์ชั่นนี้ถูกเรียกใช้ในหน้าต่างคำสั่ง ผลลัพธ์คือ • >> dog = 1; cat = 2; • >> myfun(dog, cat) • The second argument is named cat

  32. ตัวอย่างการใช้ optional arguments • function [mag, angle] = polar_value(x, y) • % polar_value converts (x,y) to (r, theta) • % angle -- Angle in degrees, msg -- Error message, mag -- Magnitude • % x -- Input x value, y -- Input y value (optional) • % Check for a legal number of input arguments • msg1 = nargchk(1,2,nargin); • error(msg1); • % If the y argument is missing, set it to 0 • if nargin < 2 • y = 0; • end

  33. % Check for (0,0) input arguments, and print out a warning message • if x == 0 & y == 0 • msg2 = 'Both x and y are zero: angle is meaningless.'; • warning(msg2); • end • % Now calculate the magnitude. • mag = sqrt(x.^2 + y.^2); • % If the second output is present, calculate angle in degrees. • if nargout == 2 • angle = atan2(y, x) * 180/pi; • end • end% function polar_value

  34. การทดลองเรียกใช้ฟังก์ชั่น polar_value • เราจะเรียกใช้ฟังก์ชั่น polar_value ในหน้าต่างคำสั่ง เพื่อทดสอบการใช้ optional arguments • 1. ทดสอบจำนวนตัวแปรอินพุต • >> [mag angle] = polar_value • ??? Error using ==> polar_value • Not enough input arguments. • >> [mag angle] = polar_value(1, -1, 1) • ??? Error using ==> polar_value • Too many input arguments.

  35. 2. ทดสอบการใช้คำสั่ง warning เมื่อ x = 0 และ y = 0 • >> [mag angle] = polar_value(0, 0) • Warning: Both x and y are zero: angle is meaningless. • > In polar_value at 19 • mag = • 0 • angle = • 0

  36. 3. ทดสอบการใช้ฟังก์ชั่น polar_value กับ 1 หรือ 2 ตัวแปรอินพุต • >> [mag angle] = polar_value(1) • mag = • 1 • angle = • 0 • >> [mag angle] = polar_value(1, -1) • mag = • 1.4142 • angle = • -45

  37. 4. ทดสอบการใช้ฟังก์ชั่น polar_value กับ 1 หรือ 2 ตัวแปรเอาต์พุต • >> mag = polar_value(1, -1) • mag = • 1.4142 • >> [mag angle] = polar_value(1, 0) • mag = • 1 • angle = • 0

More Related