31
MATLAB Week 4

MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Embed Size (px)

Citation preview

Page 1: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

MATLAB

Week 4

Page 2: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

QUIZ (Monday)•

จากโปรแกรมขางลาง หากใช while loop ก็ไมจําเปนตองใชคําสั่ง break fy

ดังนั้นจงเขียนโปรแกรมขางลางนี้ใหม โดยใช while loop

Page 3: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

QUIZ (Tueday)•

จากโปรแกรมขางลางนี้ใหม โดยใช if statement อันเดยีว

Page 4: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Simple Plot•

เขียนกราฟ x-y แบบงายๆ

คําสั่ง

plot(x,y)

: สราง linear plot ของ x-y โดย x เปนแกนนอน และ y เปนแกนตั้ง

plot(z)

: ถา z เปน complex number จะ plot ดวย (Re(z),Im(z))

plot(x,y,’str’)

: เมือ่ ‘str’ คือ character string กําหนดลักษณะ และสีของเสนกราฟ

plot(x,y,’str1’,w,v,’str2’)

: plot curve มากกวา 1 เสน ในระบบ แกนเดยีวกัน

Page 5: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Simple Plot•

เขียนกราฟ x-y แบบงายๆ

คําสั่ง

plot(x,y)

: สราง linear plot ของ x-y โดย x เปนแกนนอน และ y เปนแกนตั้ง

plot(z)

: ถา z เปน complex number จะ plot ดวย (Re(z),Im(z))

plot(x,y,’str’)

: เมือ่ ‘str’ คือ character string กําหนดลักษณะ และสีของเสนกราฟ

plot(x,y,’str1’,w,v,’str2’)

: plot curve มากกวา 1 เสน ในระบบ แกนเดยีวกัน

Page 6: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Character string

Page 7: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 8: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Simple Plot•

คําสั่ง

plotyy(x1,y1,x2,y2)

: สราง linear

plot ของ x1-y1 และ x2-y2 แตจะ วางแกน y ไวสองขางของกราฟ y1 อยูซายมือ

และ y2 อยูขวามอื

semilogx(x,y) : สราง plot ของ x-y โดย x เปนสเกล log และ y เปนเชิงเสน

semilogy(x,y)

: สราง plot ของ x-y โดย y เปนสเกล log และ x เปนเชิงเสน

loglog(x,y)

: สราง plot บนสเกล log ของ x-y

Page 9: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Simple Plot•

คําสั่ง

title(‘txt’)

: เขียนชื่อกราฟ วางที่ดานบนตรงกลาง

xlabel(‘txt’)

: เขียนชื่อแกน x วางตรงกลางของแกน x

ylabel(‘txt’)

: เขียนชื่อแกน y วางตรงกลางของแกน y

grid on

: ใหแสดงลายเสนบนกราฟ

grid off

: ไมตองแสดงลายเสนบนกราฟ

hold on

: plot ซ้ําในกราฟรูปเดิม โดยไมตองลบกราฟเดิม

hold off

: ยกเลิก hold on

Page 10: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 11: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 12: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 13: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

SubPlot•

การสรางกราฟยอยใน Graphic window เดียว

คําสั่ง

subplot(m,n,p)

: สรางกราฟยอย โดยวางอยูในรูป matrix ขนาด

mxn และให plot ลงในตําแหนง p

Page 14: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 15: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

การใชคําสั่งบน Figure Windows

เลือกเมน ูedit แลว เลือก current object

properties

Show plot toolEdit plot

Page 16: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Current object properties

Page 17: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Curve fitting and Interpolation•

คอื การเปลี่ยนขอมูลที่มีลักษณะเปนกลุมใหเปน function ที่มคีวาม

ตอเนื่อง

Interpolation

: ฟงกชั่นจะผานทุกจุดของขอมูล

Curve fitting

: ฟงกชั่นอาจจะผานหรอืผานทุกจุดของ ขอมูลก็ได แตจะตองเปนฟงกชั่นตอเนื่อง และใกลเคียงกับทุกจุดมากที่สุด

Page 18: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Curve fitting•

วิธีที่ MATLAB ใชคือ

Least square : เลือก Curve ที่ไดจาก polynomial ซึ่งมี

degree ตามที่ตองการ โดย curve จะผานจุด ขอมูลที่ผลรวมระยะหางกําลังสองนอยที่สุด

P(x) = an

xn

+ an-1

xn-1

+ an-2

xn-2

+…+ a2

x2

+ a1

x + a0

Page 19: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Curve fitting•

คําสั่ง :

polyfit(x,y,n)

: fit curve ของขอมูล x และ y ดวย

polynomial degree n ผลที่ไดจะเปนคา สปส

ของ polynomial เรียงจากกําลังมากไปนอย

polyval(P,x)

: หาคา polynomial ที่ x ใดๆ

Page 20: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

การทดสอบระยะทางที่วัตถุเคลื่อนที่

เปลี่ยนไปตามเวลา

ถาตองการหาระยะทางที่วัตถุเคลื่อนที่ไป ได เมื่อเวลา t = 2.5 วินาที

Page 21: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 22: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 23: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 24: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Interpolation•

วิธีที่ MATLAB ใชคือ

Polynomial interpolation

: เชื่อมตอจุดขอมูล 2 จุดที่อยูตดิกัน ดวยเสนโคงหรอืเสนตรง ถาเปน

เสนตรง linear interpolation

Cubic-Spline

interpolation

: เปน Curve ที่ไดจาก

polynomial degree 3 ที่เชื่อมตอ ระหวางจุดสองจุด และ slope

ระหวางเสนสองเสนที่รอยตอจะเทากัน

Page 25: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Linear Interpolation•

คําสั่ง :

interp1(x,y,s)

: หา linear interpolation ของชุดขอมลู x และ y

เมือ่ y=y(x) โดยจะไดผลคําตอบเปน y=y(s)

Note

: คา x จะตองเรยีงจากนอยไปหามาก

Page 26: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

การทดสอบระยะทางที่วัตถุเคลื่อนที่

เปลี่ยนไปตามเวลา

ถาตองการหาระยะทางที่วัตถุเคลื่อนที่ไป ได เมื่อเวลา t = 2.5 วินาที

Page 27: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Cubic Interpolation•

คําสั่ง :

interp1(x,y,s,’cubic’)

: หา interpolation ของชุดขอมลู x และ y

เมือ่ y=y(x) จะไดผลคาํตอบ เปน y=y(s) โดยใช cubic interpolation

Note

: คา x จะตองเรยีงจากนอยไปหามาก

Page 28: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

Spline Interpolation•

คําสั่ง :

interp1(x,y,s,’spline’)

: หา interpolation ของชุดขอมลู x และ y

เมือ่ y=y(x) จะไดผลคาํตอบ เปน y=y(s) โดยใช cubic spline

interpolation

spline(x,y,s)

: จะให piecewise polynomial ที่ไดจากการ

Interpolate ขอมูล (x,y) โดย polynomial จะอยูใน รูป y=y(x)

ppval(pp,s)

: pp คอื piecewise polynomial และ s คอื จุดท

ี่

ตองการหาคา

Page 29: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 30: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง

Page 31: MATLAB - eng.sut.ac.theng.sut.ac.th/me/meold/3_2551/425461/MATLAB/Matlab_week4.pdf · Simple Plot • เขียนกราฟ x-y แบบง ายๆ • คํัาส่ง

ตัวอยาง