|
การฝึกเขียนโปรแกรม SciLab ขั้นแนะนำ (โดย MathGuy)
โพสต์เมื่อ:
11:21 วันที่ 30 เม.ย. 2550 ชมแล้ว:
22,224
ตอบแล้ว:
197
วิชาการ.คอม > ครูอาจารย์
วิชาการ.คอม > ครูอาจารย์ > ครูคุยทั่วไป วิชาการ.คอม > ครูอาจารย์ > การเรียนการสอน วิชาการ.คอม > ครูอาจารย์ > เทคนิควิธีการสอน
น่าจะนับได้ว่า เป็นการนับ 1 ของการที่จะพอมีอะไรแบ่งปันให้กับคนอื่นๆบ้าง ในโลก Internet
จากที่เราได้ใช้ประโยชน์จากแหล่งความรู้ ข้อมูลต่างที่มีประโยชน์ต่อเรา ที่มีคนเสียสละเวลา
เสียสละแรงกายใจ แบ่งปันให้เราได้ใช้ร่วมกัน
เมื่อถึงจุดๆหนึ่ง เราก็น่าจะได้เริ่ม ทดแทนคุณกลับคืนบ้าง (รับมามากแล้ว เราก็ฝึกที่จะให้
)
.........................................................................................................................
กระทู้นี้ จึงเป็นการแบ่งปัน ในเรื่องการฝึกเขียนโปรแกรม SciLab
เข้าไปดูตรงนี้นะครับ : http://www.champa.kku.ac.th/jeerayut/scilab41/scilab41.html
(1) ถ้ารู้ว่า MatLab, Maple, Mathematica, Maxima, Octave คืออะไร ก็จะรู้ว่า SciLab เป็นพี่น้องกันด้วย
(2) SciLab เป็น Freeware และเข้าใจว่าจะเป็นต่อไปเรื่อยๆ (เช่นเดียวกับ Maxima และ Octave)
(3) ผมเชือว่า SciLab มีความสามารถสูงในการใช้งาน และกำลังจะเข้าไปพิสูจน์ หาคำตอบอันนี้
(4) กระทู้นี้จึงเป็นการเชิญชวน ผู้ที่สนใจ ให้เรียนรู้ไปด้วยกัน
(5) ผู้เรียนควรจะมีพื้นความรู้ทางคณิตศาสตร์ (มัธยมปลายน่าจะ OK) และมีพื้นความรู้ทางการเขียนโปรแกรมคอมพิวเตอร์บ้างพอสมควร (เคยเขียนมาบ้าง ภาษาอะไรก็ได้)
(6) ผมจะแนะนำให้น้อยที่สุด (เท่าที่คิดว่าน่าจะพอเริ่มต้นได้) และจะเน้นการทดลองเขียน การเรียนรู้ด้วยตัวเอง
(7) ถ้ามีผู้ใดที่ใช้โปรแกรมนี้อยู่ และถึงขั้นเชี่ยวชาญแล้ว ก็อยากจะให้มาช่วยๆกัน แลกเปลี่ยนประสบการณ์ครับ
(8) ผู้ที่สนใจ ถ้าได้ทดลองเขียน เรียนรู้ตามไป ถ้าไม่เข้าใจ ก็ให้ post ถามในกระทู้นี้นะครับ
ขอบคุณครับ ขอคำสั่งเขียนรูป 1. เส้นตรง จากจุด x1,y1 ไปยัง x2,y2 2. รูปสี่เหลี่ยมที่ x1,y1,x2,y2 3. วงกลมในรูปสี่เหลี่ยมจินตภาพ x1,y1,x2,y2 4. วงกลมที่มีรัศมี r จุดศูนย์กลางอยู่ที่ x,y np (IP:58.8.86.254) ที่คุณ np ขอมาตาม คหพ# 35 น่าสนใจมากครับ ถ้าให้ตอบตอนนี้ ผมก็ยังไม่รู้วิธีครับ แต่จากที่เห็นชุดคำสั่งการวาดกราฟผ่านๆตา คิดว่า Scilab ทำได้ ขอเวลานิดหนึ่งนะครับ -------------------------------------------------------------- ชุดคำสั่งดังกล่าวนั้น เป็นพื้นฐานของการสร้าง graphic เลยทีเดียว จริงๆ แล้วมีโปรแกรมที่เก่งมากๆ ด้านนี้โดยเฉพาะ และเป็น freeware เช่นกัน คือ โปรแกรม LOGO ซึ่งผมก็มีแผนการที่จะศึกษาอย่างลุมลึกเช่นกัน ถ้ายังงัย เราอาจจะช่วยกันเปิด course ตัวที่สอง คือ โปรแกรม LOGO ขั้นแนะนำ ได้ภายในอนาคตอันใกล้ๆนี้ครับ เพิ่มเติม ตัวอย่างการวาดกราฟ 4 กราฟ และให้แสดงในหน้าต่างการแสดงผลพร้อมกัน โดยจัดแบ่งแสดงในรูปของตาราง 2 x 2 -->x=-5:0.1:5; -->y1=2*x.^2+4; -->y2=(-2)*x.^2+4; -->y=-5:0.1:5; -->x1=2*y.^2+4; -->x2=(-2)*y.^2+4; -->subplot(2,2,1) -->plot(x,y1) -->xgrid(1) -->subplot(2,2,2); -->plot(x,y2) -->xgrid(1) -->subplot(2,2,3); -->plot(x1,y) -->subplot(2,2,4); -->plot(x2,y) --> ได้ผลดังนี้ ![]() ข้อความต่างๆ กว่า 10 ความคิดเห็น ที่ post หายไปไหนครับ ! ขอกระทู้ที่หายไปทั้งหมดกลับคืนมาด้วยนะครับ มิฉะนั้น กระทู้นี้คงจะต่อไปไม่ได้ เขาทำอะไรของเขานะ * * * (IP:58.8.95.59) ต้องขออภัยเป็นอย่างสูงนะครับ เรื่องความเห็นในกระทู้ที่หายไป เป็นความผิดพลาดของระบบ(บั๊ก)ครับ คือ ระบบเข้าใจผิดและไม่นำออกมาแสดง แต่จริงๆไม่ได้หายไปไหน ดำเนินการแก้ไขเรียบร้อยแล้วครับ (เนื่องจากช่วงนี้กำลังทำสงครามกับ spam โฆษณาจากต่างชาติอยู่ ก็เลยปรับแก้ระบบให้พร้อมรับมือกับพวกนั้น) ขอบคุณมากๆครับที่แจ้งเข้ามา หาที่สะดวกๆ เหมาะๆ คุย-แลกเปลี่ยนความรู้กันเกี่ยวกับ โปรแกรม ScaiLab ที่ไหน ดี np (IP:58.8.84.100) คุณ MathGuy หายไปไหนเสียแล้ว อยากถามว่า ถ้าจะสร้างกราฟพาราโบลาให้แสดงแกน x, y อยู่ตรงกลาง จะเขียนคำสั่งอย่างไร และ มีที่สะดวก เหมาะๆ กว่านี้ที่จะแลกเปลี่ยนความรู้กัน ไหม np (IP:58.8.84.100) พอดีว่า นักเรียนตัวจริงของผมยังทำงานส่งไม่สมบูรณ์ ต้องได้ปรับแก้กันพอสมควร ประกอบกับ ภาระงานที่เกี่ยวข้องกับนักศึกษาใหม่ และที่กำลังใกล้จะเปิดเทอม ผมก็เลยถือโอกาสเกเรนิดหน่อย ยังไม่ update อะไรเพิ่มเติม ........................................................................ ขอบคุณ คุณ np มากครับ ที่ยังเป็นกำลังใจอยู่อย่างเหนียวแน่น ที่บอกว่า "มีที่สะดวก เหมาะๆ กว่านี้ที่จะแลกเปลี่ยนความรู้กัน ไหม" ไม่ทราบว่า คุณ np จะแนะนำว่า ควรจะเป็นอย่างไรดีครับ ........................................................................ เกี่ยวกับ Scilab (1) คำถามการทำกราฟฟิก ให้เหมือนกับ SketchPad หรือ Logo ผมยังขอติดไว้อยู่นะครับ (2) ส่วน การสร้างกราฟพาราโบลาให้แสดงแกน x, y อยู่ตรงกลาง จะเขียนคำสั่งอย่างไรนั้น ผมขอตอบผ่าน การแนะนำการใช้ Help ของ SciLab แนะนำการใช้ Help ของ SciLab ที่เมนูบาร์ของ SciLab เลือก(คลิ๊ก) ตรง ? แล้วเลือก Scilab Help เพื่อตอบคำถาม "การสร้างกราฟพาราโบลาให้แสดงแกน x, y อยู่ตรงกลาง จะเขียนคำสั่งอย่างไร" ผมเลือก Graphics Library แล้วเลือก xaxis ได้คำอธิบาย ดังนี้ xaxis - draw an axis Calling Sequence xaxis(alpha,nsteps,size,init) Parameters alpha : real, slope in degree of the axis. nsteps : real vector of size 2, number of big and small intervals. size : real vector of size 3, size of the small intervals, and small and big tics. init : real vector of size 2, origin of the axis. Description xaxis draws an axis. The direction of the axis is given by alpha in degree. init=[x0 y0] is the initial point of the axis. nsteps=[n1,n2] gives the number of big and small intervals separated by tics. size=[s1,s2,c1] where s1 gives the size of the small intervals, s2 gives the size of the small tics along the axis and s2*c1 gives the size of the big tics. All the sizes are given using the current x-scale and y-scale and are given as dimensions along the drawn axis. example : n1=3, n2=2, alpha=0 (s2*c1) | (s2) | | | |______|_____|_____|______|_____|_____| s1 Examples x=[-%pi:0.1:%pi]'; // plot without axis plot2d(x,sin(x),1,"010"," ",[-4 -1 4 1]) // draw x axis xpoly([-4 4],[0 0],"lines") xaxis(0,[2 2],[2 0.1 3],[-4 0]) xstring(-4.1,-0.25,"-4"); xstring(-0.2,-0.1,"0"); xstring(4,-0.25,"4") // draw y axis xpoly([0 0],[-1 1],"lines") xaxis(90,[2 2],[0.5 0.025 3],[0 1]) xstring(-0.5,-1.05,"-1"); xstring(-0.35,0.95,"1") คำสั่ง xaxis นี้อ่านดู แล้วจะเห็นว่า เข้าใจยาก ดูจาก code ของตัวอย่าง จะเห็นว่า ต้องใช้ร่วมกับ plot2d(), xpoly() ซึ่งเรายังไม่ได้เรียนรู้วิธีใช้กัน .......................................................... ดังนั้นขอกลับไปที่ตัวเลือก Help ใน Graphics Library ทีนี้ลองเลือก plot() จะได้ผลดังนี้ plot - 2D plot Calling Sequence plot(y, plot(x,y, plot(x1,y1, plot( Parameters x : a real matrice or vector. If omitted, it is assumed to be the vector 1:n where n is the number of curve points given by the y parameter. y : a real matrice or vector. y can also be a function defined as a macro or a primitive. Description plot plots a set of 2D curves. plot has been rebuild to better handle Matlab syntax. To improve graphical compatibility, Matlab users should use plot (rather than plot2d ). Data entry specification : In this paragraph and to be more clear, we won't mention LineSpec nor GlobalProperty optional arguments as they do not interfer with entry data (except for "Xdata" , "Ydata" and "Zdata" property, see GlobalProperty ). It is assumed that all those optional arguments could be present too. If y is a vector, plot(y) plots vector y versus vector 1:size(y,'*') . If y is a matrix, plot(y) plots each columns of y versus vector 1:size(y,1) . If x and y are vectors, plot(x,y) plots vector y versus vector x . x and y vectors should have the same number of entries. If x is a vector and y a matrix plot(x,y) plots each columns of y versus vector x . In this case the number of columns of y should be equal to the number of x entries. If x and y are matrices, plot(x,y) plots each columns of y versus corresponding column of x . In this case the x and y sizes should be the same. Finally, if only x or y is a matrix, the vector is plotted versus the rows or columns of the matrix. The choice is made depending on whether the vector's row or column dimension matches the matrix row or column dimension. In case of a square matrix (on x or y only), priority is given to columns rather than lines (see examples below). y can also be a function defined as a macro or a primitive. In this case, x data must be given (as a vector or matrix) and the corresponding computation y(x) is done implicitly. The LineSpec and GlobalProperty arguments sould be used to customize the plot. Here is a complete list of the available options. LineSpec: This option may be used to specify, in a short and easy manner, how the curves are drawn. It must always be a string containing references to LineStyle, Marker and Color specifiers.These references must be set inside the string (order is not important) in an unambiguous way. For example, to specify a red long-dashed line with the diamond mark enabled, you can write : 'r--d' or '--dire' or '--reddiam' or another unambiguous statement... or to be totally complete 'diamondred--' (see LineSpec ). Note that the line style and color, marks color (and sizes) can also be (re-)set throught the polyline entity properties (see polyline_properties ). GlobalProperty: This option may be used to specify how all the curves are plotted using more option than via LineSpec . It must always be a couple statement constituted of a string defining the PropertyName , and its associated value PropertyValue (which can be a string or an integer or... as well depending on the type of the PropertyName ). Using GlobalProperty , you can set multiple properties : every properties available via LineSpec and more : the marker color (foreground and background), the visibility, clipping and thickness of the curves. (see GlobalProperty ) Note that all these properties can be (re-)set throught the polyline entity properties (see polyline_properties ). Remarks By default, successive plots are superposed. To clear the previous plot, use clf() . To enable auto_clear mode as the default mode, edit your default axes doing: da=gda(); da.auto_clear = 'on' A default color table is used to color plotted curves if you do not specify a color. When drawing multiple lines, the plot command automatically cycles through this table. Here are the used colors: Enter the command plot to see a demo. Examples // x initialisation x=[0:0.1:2*%pi]'; //simple plot plot(sin(x)) clf() plot(x,sin(x)) //multiple plot clf() plot(x,[sin(x) sin(2*x) sin(3*x)]) clf() // axis on the right plot(x,sin(x)) a=gca(); // Handle on current axes entity a.y_location ="right"; clf() // axis centered at (0,0) plot(x-4,sin(x),x+2,cos(x)) a=gca(); // Handle on axes entity a.x_location = "middle"; a.y_location = "middle"; // Some operations on entities created by plot ... a=gca(); a.isoview='on'; a.children // list the children of the axes : here it is an Compound child composed of 2 entities poly1= a.children.children(2); //store polyline handle into poly1 poly1.foreground = 4; // another way to change the style... poly1.thickness = 3; // ...and the tickness of a curve. poly1.clip_state='off' // clipping control a.isoview='off'; //LineSpec and GlobalProperty examples: clf(); t=0:%pi/20:2*%pi; plot(t,sin(t),'ro-.',t,cos(t),'cya+',t,abs(sin(t)),'--mo') scf(2) plot([t ;t],[sin(t) ;cos(t)],'xdat',[1:2]) scf(3) axfig3 = gca(); scf(4) // should remain blank plot(axfig3,[t ;t],[sin(t) ;cos(t)],'zdat',[1:2],'marker','d','markerfac','green','markeredg','yel') xdel(winsid()) //Data specification t=-%pi:0.1:%pi; size(t) plot(t) // simply plots y versus t vector size clf(); // clear figure plot(t,sin(t)); // plots sin(t) versus t clf(); t=[1 1 1 1 2 3 4 5 3 4 5 6 4 5 6 7]; plot(t) // plots each t column versus row size clf(); subplot(221) plot(t,sin(t)); // plots sin(t) versus t column by column this time xtitle("sin(t) versus t") subplot(222) plot(t,sin(t)') xtitle("sin(t)'' versus t") subplot(223) plot(t',sin(t)) a=gca(); a.data_bounds=[0 -1;7 1]; // to see the vertical line hiddden by the y axis xtitle("sin(t) versus t''") subplot(224) plot(t',sin(t)') xtitle("sin(t)'' versus t''") clf(); //Special case 1 //x : vector ([5 6 7 8]) and y : matrix (t) x=[5 6 7 8] plot(x,t); plot(x',t); // idem, x is automatically transposed to match t (here the columns) clf() // Only one matching possibility case : how to make 4 identical plots in 4 manners... // x is 1x4 (vector) and y is 4x5 (non square matrix) subplot(221); plot(x,[t [8;9;10;12]]'); subplot(222); plot(x',[t [8;9;10;12]]'); subplot(223); plot(x,[t [8;9;10;12]]'); subplot(224); plot(x',[t [8;9;10;12]]'); clf() //Special case 2 // Case where only x or y is a square matrix //x : matrix (t) and y : vector ([1 2 3 4]) plot(t,[1 2 3 4]) // equivalent to plot(t,[1 1 1 1;2 2 2 2;3 3 3 3;4 4 4 4]) plot(t,[1;2;3;4]) // the same plot clf(); // t is transposed : notice the priority given to the columns treatment plot(t',[1 2 3 4]) // equivalent to plot(t',[1 1 1 1;2 2 2 2;3 3 3 3;4 4 4 4]) plot(t',[1 2 3 4]') // the same plot clf(); // y is a function defined by.. // ..a primitive plot(1:0.1:10,sin) // equivalent to plot(1:0.1:10,sin(1:0.1:10)) clf(); // ..a macro: deff('[y]=toto(x)','y=x.*x') plot(1:10,toto) จะเห็นว่า ก็อ่านทำความเข้าใจยากเช่นกัน (ผมพบว่า การทำกราฟฟิคใน Scilab เราต้องค่อยๆเรียนรู้ และใช้ความอดทนสูงน่าดู เพราะ ดูแล้วมีความซับซ้อน ตัวอย่างใน help ก็ยังไม่ friendly สำหรับมือใหม่อย่างพวกเราด้วย) แต่พอดีสังเกตเห็นตรงนี้ a=gca(); // Handle on axes entity a.x_location = "middle"; a.y_location = "middle"; ทดลองดู แล้วใช้ได้ครับ ดังนี้ ->x=-5:0.1:5; -->y=x.^2-2; -->plot(x,y) -->a=gca(); -->a.x_location = "middle"; -->a.y_location = "middle"; --> ได้ผลลัพธ์ ดังนี้ ![]() รู้สึกว่า มันจะยากขึ้นทุกที ผมเห็นจะต้องหยุดพักเสียแล้วหละครับ np (IP:58.8.87.160) มันยากน้อยกว่าสมัยที่ครูไผ่เขียนโปรแกรมวาดรูปช้างยิ้มบนระบบ DOS นิดหนึ่งค่ะ แต่ตอนนี้ห้ามถามนะว่าเขียนยังไง เพราะลืมสิ้นแล้วความรู้ที่เรียนมา ตั้งแต่เปลี่ยนมาเป็นระบบ windows ก็ไม่ได้ใช้ความรู้นั้นอีกเลย คิดว่า มันมีโปรแกรมที่ง่ายกว่านี้นะครับ (แต่ไม่ใช่ง่ายแบบที่เขาทำสำเร็จมาให้แล้ว) np (IP:202.57.149.52) ถูกต้องครับ ถ้าเราจะเอาเฉพาะ การวาดกราฟนี่ มี freeware ขนาดเล็กๆ ที่มีความสามารถพอสมควรเยอะแยะเลยครับ (วันหลังผมจะเอามาโพสต์ให้ครับ) โปรแกรม Scilab นี้ เขาวางตัวไว้ ให้ใช้ได้ไม่แพ้ Matlab หรือ Maple ซึ่งเราต้องเสียตังส์ (หลายหมื่น)และลงทีหนึ่งจะกินเนื้อที่ hard disk ของเราเป็น Gigabytes เลยทีเดียว แต่ SciLab เองมีขนาดแค่ 10 กว่า Mb เองครับ คือ เขาเป็นโปรแกรมที่มีความสามารถสูง และกำลังพัฒนาอยู่ครับ หลายๆอย่างๆ เลยยังดูไม่สะดวก สำหรับคนใช้ (ส่วนหนึ่งก็เกิดจากเรายังรู้ไม่มากด้วย เมื่อเรียนรู้มากขึ้น ก็จะรู้วิธีที่สะดวกกว่า) เรื่องกราฟฟิก นี่เป็นเรื่องสำคัญมาก ยิ่งเอาไปพ่วงกับ programming นี่ ไม่ใช่เรื่องธรรมดา จึงเป็น ความยาก หรือ ความซับซ้อน เพื่อที่จะมีความสามารถสูงนั่นเอง เป็นธรรมชาติของมันเองครับ (เราสั่งด้วย code ไม่ได้ใช้ mouse คลิ๊ก หรือ ลากเอา) ................................................................ คุณ np และ ครูไผ่ อย่าพึงถอดใจนะครับ หรือ จะเอาเป็นว่า ให้คุณ np ช่วยตั้งโจทย์การคำนวณที่น่าสนใจ แล้วเราก็มาช่วยกัน ฝึกเขียนโปรแกรมการคำนวณ ดีมั้ยครับ จะทำเป็นโปรเจก หรือแบบเป็นโครงงานคณิตศาสตร์เลยก็น่าจะได้ครับ 1. จงเขียนคำสั่งเพื่อหา ห.ร.ม. ของ n1 และ n2 เมื่อ n1 และ n2 เป็นจำนวนใดๆที่ผู้ใช้ป้อนเข้ามา 2. จงเขียนคำสั่งเพื่อหา เงินรวม ของเงินต้น 50000 บาท ดอกเบี้ยทบต้น ร้อยละ 2 ต่อปีซึ่งฝากธนาคารเป็นเวลา 7 ปี np (IP:202.57.149.52) ขอโทษ ครับ ขอแก้ คหพต.54 ข้อ 2 จาก 5 หมื่น เป็น num และจากร้อยละ 2 เป็นร้อยละ b และจาก เป็นเวลา 7 ปี เป็น t ปี np (IP:202.57.149.52) |
ขอบคุณผู้สนับสนุนHot Links |
Copyright© 2000-2007, Vcharkarn.Com. All rights reserved.
|
คลิ๊กเพื่อดูสถิติ รับรองและสนับสนุนโดย |
![]() สสวท. |
![]() มูลนิธิ พสวท. |
![]() พสวท. |