1 / 23

259201 Computer Programming for Engineers

259201 Computer Programming for Engineers. Week 12 MATLAB กับการสร้างกราฟ. องค์ประกอบของกราฟ. Plot title. Legend. Y label. Data Symbol. Tick Mark. X label. Tick-mark label. การพลอตกราฟ. ฟังก์ชัน plot(x,y)  พลอตกราฟของ coordinate (x,y) ฟังก์ชัน title (‘text’)  กำหนดชื่อกราฟ

Télécharger la présentation

259201 Computer Programming for Engineers

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. 259201Computer Programmingfor Engineers Week 12 MATLAB กับการสร้างกราฟ

  2. องค์ประกอบของกราฟ Plot title Legend Y label Data Symbol Tick Mark X label Tick-mark label

  3. การพลอตกราฟ • ฟังก์ชัน plot(x,y)พลอตกราฟของ coordinate (x,y) • ฟังก์ชัน title(‘text’)กำหนดชื่อกราฟ • ฟังก์ชัน xlabel(‘text)กำหนดป้ายชื่อแกน x • ฟังก์ชัน ylabel(‘text’)กำหนดชื่อป้ายชื่อแกน y

  4. การพลอตกราฟ • Example >> x =[0:0.1:52]; >> y = 0.4*sqrt(1.8*x); >> plot(x,y) >> xlabel(‘Distance (km)’); >> ylabel(‘Height (km)’); >> title(‘Missile Height as a Function of Downrange Distance’);

  5. สัญลักษณ์ รูปแบบเส้นกราฟ สี จุด วงกลม กากบาท บวก ดอกจัน สี่เหลี่ยม ข้าวหลามตัด สามเหลี่ยมชี้ลง สามเลี่ยมชี้ขึ้น สามเลี่ยมชี้ซ้าย สามเลี่ยมชี้ขวา ดาวห้าแฉก ดาวหกแฉก . O X + * s d v ^ < > P H เส้นทึบ สันประชนิดจุด เส้นประชนิดขีด เส้นประชนิดขีดและจุด - : -- -. ดำ น้ำเงิน ไซแอน เขียว ม่วงแดงแดง ขาว เหลือง k b c g m r w y การพลอตกราฟ

  6. การพลอตกราฟ • ฟังก์ชัน gridแสดงเส้นตาราง grid on (แสดง), grid off (ไม่แสดง) • ฟังก์ชัน axis กำหนดช่วงของการแสดงผลของแกนต่างๆ axis([xmin xmax ymin ymax]) axis square กำหนดรูปสี่เหลี่ยม axis equal กำหนด scale เท่ากันทั้งสองแกน

  7. การพลอตกราฟ grid on axis([0 52 0 5]): ค่าต่ำสุดและสูงสุดทางแกน x คือ 0 และ 52 ค่าต่ำสุดและสูงสุดทางแกน y คือ 0 และ 5

  8. การพลอตกราฟ • เมื่อมีการสั่งพลอตกราฟใหม่ รูปกราฟเดิมจะถูกลบทิ้งและแทนที่ด้วยรูปกราฟใหม่ • หากต้องการพลอตรูปใหม่ซ้อนทับโดยรูปกราฟเดิมไม่หายไป >> hold on • ยกเลิกคำสั่ง hold >> hold off

  9. หรือใช้ฟังก์ชัน hold on (ค้างไว้) >>plot(x,y) >>hold on >>plot(x,z,’-’) การพลอตกราฟ • เส้นกราฟ 2 เส้นบนกราฟเดียวกัน plot(x,y,u,v) >> x = [0:.01:2]; >> y = sinh(x); >> z = tanh(x); >> plot(x,y,x,z,‘--’) >> xlabel(‘x’) >> ylabel(‘Hyperbolic Sine…and Hyperbolic Tangent’) คู่ x-y คู่ x-z

  10. การพลอตกราฟ • ฟังก์ชันlegend ข้อความอธิบายความหมายของสันกราฟ >>legend(‘sinh(x)’, ’tanh(x)’) legend

  11. การพลอตกราฟ • ฟังก์ชัน gtext >>gtext(‘test’)% เมื่อกด enter จะมีเครื่องหมาย % กากบาทปรากฏบนรูป % ให้คลิก mouse ที่ตำแน่งที่ต้องการ % ให้ข้อความ test ปรากฏ • ฟังก์ชัน text >>text(x,y,’test’)%x คือค่า coordinate ในแกน x % และ y คือค่า coordinate ใน % แกน y ที่ต้องการให้ข้อความ % test ปรากฏ x และ y อาจเป็น % vector ก็ได้

  12. 1 2 3 4 5 6 การพลอตกราฟ • การแสดงกราฟย่อย (ฟังก์ชัน subplot) subplot(m,n,p)%m แถว n column รูปที่ p มี 6 กราฟโดยเรียงเป็น 3 แถว 2 columnจะนับรูปจากซ้ายไปขวาบนลงล่างดังต้วเลขในตาราง

  13. การพลอตกราฟ >> x = [0:.01:5]; >> y = exp(-1.2*x).* sin(10*x+5); >> subplot(1,2,1) >> plot(x,y), xlabel(‘x’), ylabel(‘y’) >> axis([0 5 –1 1]) >> x = [-6:.01:6]; >> y = abs(x.^3-100); >> subplot(1,2,2) >> plot(x,y), xlabel(‘x’), ylabel(‘y’) >> axis([-6 6 0 350]) รูปที่ 1 รูปที่ 2

  14. การพลอตกราฟ • ฟังก์ชัน fplot fplot(‘func’,[xmin xmax ymin ymax]) >>[x,y]=fplot(‘cos(tan(x))–tan(sin(x))’,[1 2]) ค่าพิกัดเก็บไว้ใน อาเรย์ x และ y

  15. เปรียบเทียบ fplotกับ plot >> y = ‘cos(tan(x)) - tan(sin(x))’; >> fplot(y,[1,2]) >> x = [1:0.01:2]; >> y = cos(tan(x)) - tan(sin(x)); >> plot(x,y)

  16. การพลอตกราฟ • ฟังก์ชัน polyval 3x5+2x4-100x3+2x2-7x+90 >> x=[-6:0.01:6] >> p =[3, 2, -100, 2, -7, 90]; >> plot(x,polyval(p,x)) >> xlabel(‘x’), ylabel(‘y’)

  17. ตัวอย่างการพลอตกราฟ • ให้พลอตกราฟอุณหภูมิ ระหว่าง อุณหภูมิเซลเซียส (แกน x) กับฟาเรน- ไฮต์ (แกน y) ในช่วงอุณหภูมิเซลเซียสตั้งแต่ -50 ถึง 100 องศาเซลเซียส • Input?? • อุณหภูมิเซลเซียส (TC) • กำหนดค่า TC ในช่วง -50 ถึง 100 • Output ?? • อุณหภูมิฟาเรนไฮต์ (TF) และกราฟ • Algorithm ?? • TF = (9/5)TC + 32

  18. ตัวอย่างการพลอตกราฟ

  19. การพลอตกราฟ • ฟังก์ชัน set สำหรับการกำหนด tick mark >>set(gca, ’Xtick’, [xmin:dx:xmax], ‘Ytick’, [ymin:dy:ymax]) %ให้ค่าประจำแกน x ต่ำสุดที่ xmin และสูงสุดที่ xmax โดยที่มีระยะห่างเป็น dx และ ให้ค่าประจำแกน y ต่ำสุดที่ ymin และสูงสุดที่ ymax โดยที่มีระยะห่างเป็น dy >>set(gca, ’Xtick’, [0:0.2:2], ’Ytick’, [0:0.1:1]) %สามารถ set label ของ Tick mark ทางแกน x ให้เป็นชื่อเดือนได้ >>set(gca,’Xticklabel’,[‘Jan’; ’Feb’; ’Mar’; ’Apr’; ’May’; ’Jun’]) >>set(gca,’Xtick’,[1:6])

  20. การพลอตกราฟ • ฟังก์ชันsetสำหรับการกำหนด tick mark การกำหนด tick mark เป็นข้อความ สามารถทำได้โดยใช้คำสั่ง set เช่นกัน >>set(gca,’Xticklabel’,[‘Jan’; ’Feb’; ’Mar’; ’Apr’; ’May’; ’Jun’]) %สามารถ set label ของ Tick mark ทางแกน x ให้เป็นชื่อเดือนได้

  21. การพลอตกราฟ: กราฟรูปแบบอื่นๆ • ฟังก์ชันstem, stairs, bar >>x=[1:6]; >>y=[13 5 7 14 10 12]; >>stairs(x,y) >>bar(x,y) >>stem(x,y)

  22. การพลอตกราฟ • ฟังก์ชัน hist(x,n)ใช้สร้างกราฟฮีสโตแกรมของตัวแปร x โดยต้องการแบ่งช่วงข้อมูลออกเป็น n ช่วง • ใช้ plot histogram ความถี่ของข้อมูล เช่น • น.ศ. 20 คน สอบได้คะแนนต่างๆ กัน (คะแนนเต็ม 10) • ต้องการแบ่งช่วงคะแนนเป็น 5 ช่วง • อยากทราบจำนวนน.ศ.สอบได้คะแนน สามารถใช้ histogram สร้างกราฟได้

  23. การพลอตกราฟ >> score=[2 9 4 5 4 2 9 5 6 2 8 7 2 3 9 1 6 3 4 6]; >> hist(score,5)

More Related