Chap.3 二維平面繪圖 方煒 台大生機系.

Slides:



Advertisements
Similar presentations
SCILAB第二章 二維平面繪圖 撰文者: 1.中央大氣科學系 楊善文 2. (如對本文有貢獻者,記得在此留名)
Advertisements

應用於BIM科技中 幾何平面切割與解構 教案設計人:Shuyi.
MATLAB小结、 经典迭代法、CG.
Performance Evaluation
“走进三国” 读书汇报会 广州市玉岩中学 李玉明( ).
Introduction to Matlab
Matlab教學 Speaker:林昱志 Date:2012/10/18.
1012 MATLAB 教學 彭奕翔 2013/02/27.
張智星 (Roger Jang) 台大資工系 多媒體檢索實驗室
数学软件 Matlab —— 二维平面作图 —— 三维空间作图.
MATLAB程序设计基础 MATLAB的图形处理.
——Matlab语言丰富的图形表现方法,使得数学计算结果可以方便地、多样性地实现了可视化,这是其它语言所不能比拟的。
数值计算的工具—MATLAB 电子计算机技术为应用数学解决实际问题创造了物质条件 。
说课.
税务认定 永州市国家税务局纳税人学校.
MATLAB介紹.
第三章 MATLAB图形系统 3.1 图形绘制 3.2 图形标注 3.3 对数和极坐标系中图形绘制 3.4 复杂图形绘制 3.5 坐标轴控制
第二章 SAS的描述统计功能 2.1 描述性统计的基本概念 2.2 在SAS中计算统计量 2.3 统计图形.
張智星 清大資工系 補充內容:方煒 台大生機系 小幅修改:吳俊仲 長庚機械系
MATLAB 程式設計進階篇 一般數學函數的處理與分析
MATLAB 程式設計進階篇 一般數學函數的處理與分析
機率與統計 Introduction   講師:黃弘州.
第六章 图 形 基 础 6.1 概述 6.2 基本二维绘图 6.3 格式化绘图 6.4 特殊图形函数 6.5 基本三维绘图
范洪源 臺灣師範大學數學系 MATLAB 基本功能介紹 范洪源 臺灣師範大學數學系.
Matlab教學 Speaker:林昱志 Date:2012/10/25.
EndNote X5 Advance your Research and Publish Instantly
訪視委員 蒞臨指導 熱烈歡迎 Chrome text with reflection (Advanced)
數學與電腦 的初相識 汪群超 個人網址: 變有不可者三,有不可不變者三: 能力未至不可變也、 學識未敷不得變也、 功侯未到不能變也。
文献管理软件EndnoteX4及其新功能 学科馆员:张欣利.
Endnote與Word的應用(進階).
Origin绘图和数据分析 2006年11月.
XBRL未來發展趨勢 2009年12月 For information on applying this template onto existing presentations, refer to the notes on slide 3 of this presentation. The Input.
第一讲 MATLAB简介 1.1 MATLAB与通信仿真 1.1.1 通信电路与系统仿真 1.1.2 MATLAB的发展史
Application of Matlab Language
第7章 MATLAB图形用户界面设计 7.1 菜单设计 7.2 对话框设计 7.3 用户界面设计工具.
第4章 MATLAB绘图 4.1 MATLAB二维曲线绘图 基本绘图指令 1.plot(Y) 功能:画一条或多条折线图.
Learning Polynomials 台大生機系 方煒.
第八讲 MATLAB图形用 户界面设计.
Matlab基础介绍 Matlab 简介 Matlab 的安装与启动 Matlab 编程基础 Matlab 在数字信号处理课程中的应用.
Tel: 第11章 SPSS在时间序列预测中的应用 周早弘 旅游与城市管理学院
黃聰明 國立臺灣師範大學數學系 MATLAB 基本功能介紹 黃聰明 國立臺灣師範大學數學系
数据分析绘图工具 ——Origin6.0.
張智星 (Roger Jang) 清大資工系 多媒體檢索實驗室
第三章 MATLAB图形系统 3.1 图形绘制 3.2 图形标注 3.3 对数和极坐标系中图形绘制 3.4 复杂图形绘制 3.5 坐标轴控制
Introduction to MATLAB
数学建模与数学实验 MATLAB作图.
范洪源 臺灣師範大學數學系 分支宣告與程式設計 范洪源 臺灣師範大學數學系.
授課教授:張寶基 助教:梁凱雯 郭千豪 音視訊處理實驗室 2014 / 9 / 30
§ 9.1常用数学软件简介及MATLAB基础知识
图表制作与数据分析工具 Origin快速入门
MATLAB 程式設計入門篇 動畫製作.
数学建模 江西财经大学 数学与管理决策系 制作:华长生 华长生制作.
MATLAB 程式設計入門篇 二維平面繪圖 (part2)
MATLAB 入门教程.
第11章 MATLAB图形用户界面设计 11.1 菜单设计 11.2 对话框设计 11.3 图形用户界面设计工具
MATLAB 程式設計入門篇 二維平面繪圖 改自張智星講義
MATLAB 程式設計入門篇 三維立體繪圖 (part1)
三維繪圖 Helix t = 0:pi/50:10*pi; % linspace(0,10*pi,500); figure plot3(sin(t),cos(t),t) grid on axis square Remark: zlabel, view, surf.
第二章 MATLAB编程与作图 2.1 程序设计 2.2 作图 2.3 在线帮助和文件管理 2.4 习题 2019年4月23日
Q & A.
Inspiration From Above 1 Chinese Evangelical Free Church
張智星 (Roger Jang) 台大資工系 多媒體檢索實驗室
第二节 MATLAB 图形处理 内容: §1 二维图形显示 §2 图形可视编辑工具 §3 三维图形显示 §4 视角变换与三视图
Create and Use the Authorization Objects in ABAP
第2章 MATLAB的基本语法 2.1 变量及其赋值 2.2 运算符与数学表达式 2.3 控制流 2.4 数据的输入输出及文件的读写
科研工具之图表制作---Sigmaplot
When using opening and closing presentation slides, use the masterbrand logo at the correct size and in the right position. This slide meets both needs.
Presentation transcript:

Chap.3 二維平面繪圖 方煒 台大生機系

A complete plot w/ title, axis labels, legend, grid and multiple line styles

5-2

基本的繪圖指令 Plot : 最基本的繪圖指令 對 x 座標及相對應的 y 座標進行作圖 範例:plotxy01.m x = linspace(0, 2*pi); % 在 0 到 2π 間,等分取 100 個點 y = sin(x); % 計算 x 的正弦函數值 plot(x, y); % 進行二維平面描點作圖

Plot基本繪圖-1 linspace(0, 2*pi) 產生從 0 到 2π且長度為 100 (預設值)的向量 x y 是對應的 y 座標 只給定一個向量 該向量則對其索引值(Index)作圖 plot(y)和plot(1:length(y), y)會得到相同的結果

Plot基本繪圖-2 (I) 一次畫出多條曲線 將 x 及 y 座標依次送入plot 指令 範例:plotxy02.m x = linspace(0, 2*pi); % 在 0 到 2 間,等分取 100 個點 plot(x, sin(x), x, cos(x), x, sin(x)+cos(x)); % 進行多條曲線描點作圖

Plot基本繪圖-2 (II) 畫出多條曲線時,會自動輪換曲線顏色 Plot(x,sin(x), x, cos(x), x, sin(x)+cos(x)); 畫出多條曲線時,會自動輪換曲線顏色

Plot基本繪圖-3 (I) 若要以不同的線標(Marker)來作圖 範例:plotxy03.m x = linspace(0, 2*pi); % 在 0 到 2 間,等分取 100 個點 plot(x, sin(x), 'o', x, cos(x), 'x', x, sin(x)+cos(x), '*');

Plot基本繪圖-3 (II)

Plot基本繪圖-4 (I) 只給定一個矩陣 y 對矩陣 y 的每一個行向量(Column Vector)作圖 範例:plot04.m y = peaks; % 產生一個 49×49 的矩陣 plot(y); % 對矩陣 y 的每一個行向量作圖

Plot基本繪圖-4 (II) peaks 指令產生一個49×49的矩陣,代表二維函數的值 plot(y) 直接畫出 49 條直線

Plot基本繪圖-5 (I) x 和 y 都是矩陣 plot(x, y) 會取用 y 的每一個行向量和對應的 x 行向量作圖 範例:plotxy05.m x = peaks; y = x'; % 求矩陣 x 的轉置矩陣 x' plot(x, y); % 取用矩陣 y 的每一行向量,與對應矩陣 x % 的每一個行向量作圖

Plot基本繪圖-5 (II)

提示 一般情況下,MATLAB 將矩陣視為行向量的集合 對只能處理向量的函數(Ex : max、min、 mean) 給定一個矩陣,函數會對矩陣的行向量一一進行處理或運算

Plot基本繪圖-6 (I) z 是一個複數向量或矩陣 plot(z) 將 z 的實部(即 real(z))和虛部(即 imag(z))當成 x 座標和 y 座標來作圖, 其效果等於 plot(real(z), imag(z)) 範例:plotxy06.m x = randn(30); % 產生 30×30 的亂數(正規分佈)矩陣 z = eig(x); % 計算 x 的「固有值」(或稱「特徵值」) plot(z, 'o') grid on % 畫出格線

Plot基本繪圖-6 (II) x 是一個 30×30 的亂數矩陣 z 則是 x 的「固有值」(Eigenvalue,或「特徵值」)

Z=0.1+0.9i; N=[0:0.1:10]; Plot(z.^n), xlabel(‘Real’), ylabel(‘Imaginary’);

基本二維繪圖指令 指令 說明 Plot x 軸和 y 軸均為線性刻度(Linear Scale) loglog x 軸和 y 軸均為對數刻度(Logarithmic Scale) semilogx x 軸為對數刻度,y 軸為線性刻度 semilogy x 軸為線性刻度,y 軸為對數刻度 plotyy 畫出兩個刻度不同的y軸

Plot基本繪圖-7 (I) Semilogx指令 使 x 軸為對數刻度,對正弦函數作圖 範例:plotxy07.m x = linspace(0, 8*pi); % 在 0 到 8 間,等分取 100 個點 semilogx(x, sin(x)); % 使 x 軸為對數刻度,並對其正弦函數作圖

Plot基本繪圖-7 (II) X軸為對數刻度

Ex4_5.m x=0:.1:8; y=exp(x); semilogx(x,y); title('Semilogx for y=exp(x)') pause semilogy(x,y); title('Semilogy for y=exp(x)') loglog(x,y); title('Loglog for y=exp(x)')

Plot基本繪圖-8 (I) plotyy 指令 畫出兩個刻度不同的 y 軸 範例:plotxy08.m x = linspace(0, 2*pi); % 在 0 到 2 間,等分取 100 個點 y1 = sin(x); y2 = exp(-x); plotyy(x, y1, x, y2); % 畫出兩個刻度不同的 y 軸,分別是 y1, y2

Plot基本繪圖-8 (II) y1 的刻度是在左手邊 y2 的刻度是在右手邊 兩邊的刻度不同 Y2的刻度 Y1的刻度

圖形的控制 plot 指令,可以接受一個控制字串輸入 用以控制曲線的顏色、格式及線標 使用語法 C:曲線的顏色(Colors) plot(x, y, ‘CLM‘) C:曲線的顏色(Colors) L:曲線的格式(Line Styles) M:曲線所用的線標(Markers)

圖形控制範例-1 (I) 用黑色點線畫出正弦波 每一資料點畫上一個小菱形 範例:plotxy09.m x = 0:0.5:4*pi; % x 向量的起始與結束元素為 0 及 4, % 0.5為各元素相差值 y = sin(x); plot(x, y,‘k:diamond’) % 其中「k」代表黑色,「:」代表點 % 線,而「diamond 」則指定菱形為曲 % 線的線標

圖形控制範例-1 (II)

plot 指令的曲線顏色 Plot指令的曲線顏色字串 曲線顏色 RGB值 b 藍色(Blue) (0,0,1) c 青藍色(Cyan) (0,1,1) g 綠色(Green) (0,1,0) k 黑色(Black) (0,0,0) m 紫黑色(Magenta) (1,0,1) r 紅色(Red) (1,0,0) w 白色 (1,1,1) y 黃色(Yellow) (1,1,0)

plot 指令的曲線格式 plot 指令的曲線格式字串 曲線格式 - 實線(預設值) -- 虛線 : 點線 -. 點虛線

plot 指令的曲線線標 plot 指令的曲線線標字串 曲線符號符號 > 朝右三角形 < 朝左三角形 square 方形 diamond 菱形 pentagram 五角星形 hexagram 六角星形 None 無符號(預設值) plot 指令的曲線線標字串 曲線符號符號 O 圓形 + 加號 X 叉號 * 星號 . 點號 ^ 朝上三角形 V 朝下三角形

Enhanced control of plotted lines Plot(x,y,’PropertyName’, value,..) Property 的名稱 LineWidth MarkerEdgeColor MarkerFaceColor MarkerSize … X=0:pi/15:4*pi; y=exp(2*sin(x)); Plot(x,y,’-ko’,’LineWidth’,3,’MarkerSize’,6,… ‘MarkerEdgeColor’,’r’,‘MarkerFaceColor’,’g’)

圖軸的控制 plot 指令會根據座標點自動決定圖軸範圍 也可以使用 axis 指令指定圖軸範圍 使用語法: axis([xmin, xmax, ymin, ymax]) xmin , xmax:指定 x 軸的最小和最大值 ymin , ymax:指定 y 軸的最小和最大值

圖軸控制範例-1 (I) 畫出正弦波在 y 軸介於 0 和 1 的部份 範例:plotxy10.m x = 0:0.1:4*pi; % 起始與結束元素為 0 及 4,0.1 為各 % 元素相差值 y = sin(x); plot(x, y); axis([-inf, inf, 0, 1]); % 畫出正弦波 y 軸介於 0 和 1 的部份

圖軸控制範例-1 (II) inf指令: 以資料點(上例: x 軸的資料點)的最小和最大值取代之

Ex4_7.m close all; x=.01:.01:20; y=cos(x)./x; plot(x,y); axis([0 25 -5 5]); %xlim([0, 20]); %ylim([-5, 5]);

圖軸控制範例-2 (I) 指定圖軸上的格線點(Ticks) 範例:plotxy11.m x = 0:0.1:4*pi; plot(x, sin(x)+sin(3*x)) set(gca, ‘ytick’, [-1 -0.3 0.1 1]); % 在 y 軸加上格線點 grid on % 加上格線

圖軸控制範例-2 (II) grid on:加上格線 gca: get current axis的簡稱 傳回目前使用中的圖軸 使用者加入的格線點和文字 grid on:加上格線 gca: get current axis的簡稱 傳回目前使用中的圖軸 gca屬Handle Graphics的指令

圖軸控制範例-3 (I) 將格線點ytick的數字改為文字 範例:plotxy12.m x = 0:0.1:4*pi; plot(x, sin(x)+sin(3*x)) set(gca, 'ytick', [-1 -0.3 0.1 1]); % 改變格線點 set(gca, ‘yticklabel’, {‘極小’,‘臨界值’,‘崩潰值’,‘極大’}); % 改變格線點的文字 grid on % 加上格線

圖軸控制範例-3 (II)

圖軸控制範例-4 (I) 將格線點xtick的數字改為文字 範例: x = [1:6];y=[13,5,7,14,10,12]; plot(x, y, ‘o’, x, y) set(gca, ‘xtick', [1:6], axis([1 6 0 15]), xlabe;(‘Month’); set(gca, ‘xticklabel’, [‘Jan’;’’Feb’;’Mar’;’Apr’;’May’;’Jun’]); Ylabel(‘Monthly Sales ($1000)’); Title(‘Printer sales for January to June, 1997’);

圖軸控制範例-4 (II)

一次畫出多條曲線在同一圖上Ex4_3.m dx=.01; x=.5*dx:dx:10-0.5*dx; y=sin(5*x); y2=cos(x); % the second function % plot both plot(x,y,'r-',x,y2,'b-');

一次畫出多條曲線在同一圖上 x=-pi:pi/20:pi; y1=sin(x); y2=cos(x); plot(x,y1,’b-’, x,y2,’k—’); legend (‘sin(x)’,’cos(x)’);

分批畫出多條曲線在同一圖上 x=-pi:pi/20:pi; y1=sin(x); y2=cos(x); plot(x,y1,’b-’); hold on; plot(x,y2,’k—’); hold off legend (‘sin(x)’,’cos(x)’);

legend 指令 x = [0:0.01:2]; y = sinh(x); z = tanh(x); plot(x,y,x,z,’--’),xlabel(’x’), ... ylabel(’Hyperbolic Sine and Tangent’), ... legend(’sinh(x)’,’tanh(x)’)

Possible locations for a plot legend

分批畫出多條曲線在不同視窗 figure(1) figure(2) x=-pi:pi/20:pi; y1=sin(x); y2=cos(x); plot(x,y1,’b-’); legend (‘sin(x)’); figure(2) plot(x,y2,’k—’); legend (’cos(x)’);

Subplot subplot 在一個視窗產生多個圖形(圖軸) 一般形式為 subplot (m, n, p) 將視窗分為 m ×n 個區域 下一個 plot 指令繪圖於第 p 個區域 p 的算法為由左至右,一列一列

圖軸控制範例-4 (I) 同時畫出四個圖於一個視窗中 範例:plotxy13.m x = 0:0.1:4*pi; subplot(2, 2, 1); plot(x, sin(x)); % 此為左上角圖形 subplot(2, 2, 2); plot(x, cos(x)); % 此為右上角圖形 subplot(2, 2, 3); plot(x, sin(x).*exp(-x/5)); % 此為左下角圖形 subplot(2, 2, 4); plot(x, x.^2); % 此為右下角圖形

圖軸控制範例-4 (II) Subplot(2,2,1) Subplot(2,2,2) Subplot(2,2,3)

Subplot(2,1,1) Subplot(2,1,2)

圖軸控制範例-5 (I) 長寬比(Aspect Ratio) 一般圖軸長寬比是視窗的長寬比 可在 axis 指令後加不同的字串來修改 範例:plotxy14.m t = 0:0.1:2*pi; x = 3*cos(t); y = sin(t); subplot(2, 2, 1); plot(x, y); axis normal subplot(2, 2, 2); plot(x, y); axis square subplot(2, 2, 3); plot(x, y); axis equal subplot(2, 2, 4); plot(x, y); axis equal tight

圖軸控制範例-5 (II) axis normal axis square axis equal axis equal tight

改變圖軸長寬比的指令 改變目前圖軸長寬比的指令 需在 plot 指令之後呼叫才能發揮效用 指令 說明 axis normal 使用預設長寬比(等於圖形長寬比) axis square 長寬比例為 1 axis equal 長寬比例不變,但兩軸刻度一致 axis equal tight 兩軸刻度比例一致,且圖軸貼緊圖形 axis image 兩軸刻度比例一致(適用於影像顯示)

改變圖軸背景顏色的指令 colordef 改變圖軸與視窗之背景顏色 先呼叫 colordef 指令,其後 plot 指令產生的圖形才有效用 說明 colordef white 圖軸背景為白色,視窗背景為淺灰色 colordef black 圖軸背景為黑色,視窗背景為暗灰色 colordef none 圖軸背景為黑色,視窗背景為黑色(這是 MATLAB 第 4 版的預設值)

grid 和 box 指令 畫出格線或畫出圖軸外圍的方形 指令 說明 grid on 畫出格線 grid off 取消格線 box on 畫出圖軸的外圍長方形 box off 取消圖軸的外圍長方形

加入說明文字 在圖形或圖軸加入說明文字,增進整體圖形的可讀性 指令 說明 title 圖形的標題 xlabel x 軸的說明 ylabel zlabel z 軸的說明(適用於立體繪圖) legend 多條曲線的說明 text 在圖形中加入文字 gtext 使用滑鼠決定文字的位置

說明文字範例-1 (I) 範例:plotxy15.m subplot(1,1,1); x = 0:0.1:2*pi; y1 = sin(x); y2 = exp(-x); plot(x, y1, '--*', x, y2, ':o'); xlabel('t = 0 to 2\pi'); ylabel('values of sin(t) and e^{-x}') title('Function Plots of sin(t) and e^{-x}'); legend('sin(t)','e^{-x}');

說明文字範例-1 (II) legend 指令 「\」為特殊符號 畫出一小方塊,包含每條曲線的說明 產生上標、下標、希臘字母、數學符號等 遵循一般 LaTex 或 TeX數學模式

說明文字範例-2 (I) text指令 使用語法: text(x, y, string) x、y :文字的起始座標位置 範例:plotxy16.m x = 0:0.1:2*pi; plot(x, sin(x), x, cos(x)); text(pi/4, sin(pi/4),'\leftarrow sin(\pi/4) = 0.707'); text(5*pi/4, cos(5*pi/4),'cos(5\pi/4) = -0.707\rightarrow', 'HorizontalAlignment', 'right');

說明文字範例-2 (II) 「HorizontalAlignment」及「right」指示 text 指令將文字向右水平靠齊

Enhanced control of Text Strings \bf Boldface 粗體字 \it Italics 斜體字 \rm Remove modifiers 取消字體修飾,恢復預設字體 \fontname{fontname) 定義使用的字型名稱 \fontsize{fontsize} 定義使用的字型大小 _{xxx} {}內的字為下標 ^{xxx} {}內的字為上標

‘\bf\itx_{1}^{2} + x_{2}^{2} \rm(units: \bfm^{2}\rm) ’ 練習 ‘\bf{B}_{\itS}’ Bs ‘\tau\it_{m}’ m ‘\bf\itx_{1}^{2} + x_{2}^{2} \rm(units: \bfm^{2}\rm) ’

Enhanced control of Text Strings \circ \sim \infty \pm \leq \geq \neq \prop \div \int \cong                 \leftarrow \rightarrow \uparrow \downarrow \leftrightarrow

範例:ex4_1.m close all; x=.01:.01:20; y=cos(x)./x; plot(x,y); axis([0 25 -5 5]); str1='\alpha \beta \gamma \delta \epsilon \phi '; str2='\theta \kappa \lambda \mu \nu \pi '; str3='\rho \sigma \tau \xi \zeta'; xlabel([str1 str2 str3]); text(10,1,'\theta_1'); text(10,2,'\theta_{12}'); text(10,3,'\theta^{10}');

Ex4_2.m dx=.01; x=.5*dx:dx:10-0.5*dx; y=sin(5*x); %plot(x,y,'r-'); nhalf=ceil(length(x)/2); plot(x(1:nhalf),y(1:nhalf),'b-') hold on plot(x(nhalf:end),y(nhalf:end),'k-') xlabel('\theta') ylabel('F(\theta)') %title('F(\theta)=sin(5 \theta)') s=sprintf('F(\\theta)=sin(%i \\theta)',5); title(s)

gtext指令 使用語法 gtext(string) 在圖上點選一位置後,string 顯示在其上。 gtext 只能用在二維平面繪圖

gtext

Enhanced control of Text Strings \alpha \beta \gamma \delta \epsilon \eta \theta        \lambda \mu \nu \pi \phi \rho \sigma \tau \omega          \Omega \Sigma \Pi \Lambda \Delta \Gamma      

練習:ideal gas law PV=nRT 請完成本程式,結果如下圖所示 n=1; R=8.314; T=273; P=1:0.1:1000; V=(n*R*T)./P; figure(1); loglog(……); … grid on; hold on; T=373; loglog(…); hold off; legend(‘T=273 K’,’T=373K’);

The power function y = 2x -0.5 and the exponential function y = 101-x .

Exponential function : polyfit(x,y,n)

The exponential function: y = b(10)mx. In this case log10 y = mx + log10b which has the form w = p1z + p2 where the polynomial variables w and z are related to the original data variables x and y by w = log10 y and z = x. We can find the exponential function that fits the data by typing p = polyfit(x, log10(y),1) The first element p1 of the vector p will be m, and the second element p2 will be log10b. We can find b from b = 10p2 .

時間,秒 溫度,oF 145 620 130 2266 103 3482 90 詳見 coffee.m 範例:咖啡冷卻問題 室溫 68oF下瓷杯內的咖啡由 145oF逐漸冷卻,溫度對應於經過時間的數據如上,請建立溫度對時間的函數,並與此模式預測,經過多少時間,杯內溫度會達120oF? time=[0,620,2266,3482]; temp=[145,130,103,90]; temp=temp-68; subplot(2,2,1); plot(time,temp, time,temp,’o’),xlabel(‘Time (sec)’),… ylabel(‘Relative Temperature (deg F)’) subplot(2,2,2); semilogy(time,temp, time,temp,’o’),xlabel(‘Time (sec)’),…

範例:咖啡冷卻問題 (續) P=polyfit(time, log10(temp),1); m=p(1); b=10^p(2); t_120=(log10(120-68)-log10(b))/m %show thederived curve and estimated point t=[0:10:4000]; T=68+b*10.^(m*t); subplot(2,2,3); semilogy(t,T-68,time,temp,'o',t_120,120-68,'+'),xlabel('Time (sec)'),... ylabel('Relative Temperature (deg F)'); %show in rectilinear scales subplot(2,2,4); plot(t,T,time,temp+68,'o',t_120,120,'+'),xlabel('Time (sec)'),... ylabel('Temperature (deg F)'););

Power function: polyfit(x,y,n)

The power function: y = bxm. In this case log10 y = m log10x + log10b which has the form w = p1z + p2 where the polynomial variables w and z are related to the original data variables x and y by w = log10 y and z = log10x. Thus we can find the power function that fits the data by typing p = polyfit(log10(x),log10(y),1) The first element p1 of the vector p will be m, and the second element p2 will be log10b. We can find b from b = 10p2 .

互動式繪圖 Interactive Plotting

Tools on the figure toolbar

Edit tool

產生 M 檔案 點選File 選單 選取 Generate M-File 選項

Figures 的存檔 Figure 的預設存入檔案名稱為 .fig. 選取Figure 視窗中 File menu 內的 Save 選項 或者直接點選工具列內的 Save 按鈕 (磁碟片圖示). 該檔案為首次存入時會先出現 Save As 對話框確認存入的是 MATLAB Figure (*.fig). 確認檔名後可以點選 OK.

Figures 的輸出 To save the figure in a format that can be used by another application, such as the standard graphics file formats TIFF or EPS, perform these steps. 1. Select Export Setup from the File menu. This dialog lets you specify options for the output file, such as the figure size, fonts, line size and style, and output format. 2. Select Export from the Export Setup dialog. A standard Save As dialog appears. 3. Select the format from the list of formats in the Save As type menu. This selects the format of the exported file and adds the standard file name extension given to files of that type. 4. Enter the name you want to give the file, less the extension. Then click Save.

Figures 的剪貼 On Windows systems, you can also copy a figure to the clipboard and then paste it into another application: 1. Select Copy Options from the Edit menu. The Copying Options page of the Preferences dialog box appears. 2. Complete the fields on the Copying Options page and click OK. 3. Select Copy Figure from the Edit menu.

其他平面繪圖指令 各種二維繪圖指令 指令 說明 errorbar 在曲線加上誤差範圍 fplot、ezplot 較精確的函數圖形 polar、ezpolar 極座標圖形 hist 直角座標直方圖(累計圖) rose 極座標直方圖(累計圖) compass 羅盤圖 feather 羽毛圖 area 面積圖(第五章「特殊圖形」介紹) stairs 階梯圖(第五章「特殊圖形」介紹)

其他平面繪圖範例-1 (I) 已知資料的誤差範圍,用 errorbar 表示 以 y 座標高度 20% 作為做資料的誤差範圍 範例:plotxy17.m x = linspace(0,2*pi,30); % 在 0 到 2 間,等分取 30 個點 y = sin(x); e = y*0.2; errorbar(x,y,e) % 圖形上加上誤差範圍 e

其他平面繪圖範例-1 (II) 誤差範圍

其他平面繪圖範例-2 (I) fplot 指令 對劇烈變化處進行較密集的取樣 範例:plotxy18.m fplot('sin(1/x)', [0.02 0.2]); % [0.02 0.2]是繪圖範圍

其他平面繪圖範例-2 (II) 此區作較精確的取點繪圖

x=([1:0.01:2]; plot(x, cos(tan(x)-tan(sin(x)));

fplot(‘cos(tan(x)-tan(sin(x))', [1 2]); % [1 2]是繪圖範圍

Plotting Polynomials with the polyval Function. To plot the polynomial 3x5 + 2x4 – 100x3 + 2x2 – 7x + 90 over the range –6 £ x £ 6 with a spacing of 0.01, you type >>x = [-6:0.01:6]; >>p = [3,2,-100,2,-7,90]; >>plot(x,polyval(p,x)); >>xlabel(’x’); >>ylabel(’p’);

其他平面繪圖範例-3 (I) polar 指令 產生極座標圖形 範例:plotxy19.m theta = linspace(0, 2*pi); r = cos(4*theta); polar(theta, r); % 進行極座標繪圖

其他平面繪圖範例-3 (II)

範例 某環繞太陽運轉的星球之極座標方程式 r = p / (1 – ε cosθ) ε為離心率 = 0.5 (橢圓) p 為該星球的軌道極圖 = 2 AU AU 為天文單位,代表地球與太陽的距離 theta=[0:pi/90:2*pi]; r=2./(1-0.5*cos(theta)); polar(theta,r),title(‘Orbital Eccentricity = 0.5’);

徑向直線顯示90與270o時,該星球與太陽的距離為2AU 原點為太陽的位置 半徑為1的圓為地球的軌跡 180o時,該星球與太陽的距離為最短,僅1.3AU 0o時,該星球與太陽有最遠的距離,達4AU 徑向直線顯示90與270o時,該星球與太陽的距離為2AU 橢圓為該星球的軌跡,太陽位於其焦點之一

直方圖及hist指令 直方圖(Histogram) hist指令 對大量的資料,顯示資料的分佈情況和統計特性 將資料依大小分成數堆,將每堆的個數畫出

其他平面繪圖範例-4 (I) 將 10000 個由 randn 產生的正規分佈之亂數分成 25 堆 範例:plotxy20.m x = randn(10000, 1); % 產生 10000 個正規分佈亂數 hist(x, 25); % 繪出直方圖,顯示 x 資料的分佈情 %況和統計特性,數字 25 代表資料依 %大小分堆的堆數,即是指方圖內長條 %的個數 set(findobj(gca, ‘type’, ‘patch’), ‘edgecolor’, ‘w’); % 將長條 %圖的邊緣設定成白色

其他平面繪圖範例-4 (II) 直方圖逼近亂數的機率分佈函數,且資料量越大時,逼近程度越高

其他平面繪圖範例-5 (I) rose指令 角度:資料大小 距離:資料個數 以極座標繪製表示 範例:plotxy21.m x = randn(5000, 1); % 產生 5000 個正規分佈的亂數 rose(x); % x 資料大小為角度,x 資料個數為距離, %以繪製類似玫瑰花瓣的極座標直方圖

其他平面繪圖範例-5 (II)

其他平面繪圖範例-6 (I) compass 指令 畫出以原點為起始點的向量圖 稱為「羅盤圖」 範例:plotxy22.m theta = linspace(0, 2*pi, 50); rho = sin(0.5*theta); [x, y] = pol2cart(theta, rho); % 由極座標轉換至直角座標 compass(x, y); % 畫出以原點為向量起始點 % 的羅盤圖

其他平面繪圖範例-6 (II) 若只有一個引數輸入z 將 z 的實部做為 x 座標,將 z 的虛部做為 y 座標,再進行作圖 範例:plotxy23.m 若只有一個引數輸入z 將 z 的實部做為 x 座標,將 z 的虛部做為 y 座標,再進行作圖 compass(z) 即等效於 compass(real(z),imag(z)) 上述四列程式碼可簡化 theta = linspace(0, 2*pi, 50); compass(sin(0.5*theta).*exp(j*theta));

其他平面繪圖範例-7 (I) 羽毛圖 起始點是 (k, 0),k = 1~n ,其中 n 是向量個數 範例:plotxy24.m theta = linspace(0.2*pi,50); rho = 10; [x, y] = pol2cart(theta,rho); % 由極座標轉換至直角座標 feather(x, y); % 繪製羽毛圖

其他平面繪圖範例-7 (II)

練習 試寫一函數 regPoly(n),其功能為畫出一個圓心在 (0, 0)、半徑為 1 的圓,並在圓內畫出一個內接正 n 邊形,其中一頂點位於 (0, 1)。例如 regPoly(8) 可以畫出如下之正八邊型: function regpoly(n) vertices=[1]; for i=1:n step=2*pi/n; vertices=[vertices, exp(i*step*sqrt(-1))]; end plot(vertices, '-o'); axis image % 畫外接圓 hold on theta=linspace(0, 2*pi); plot(cos(theta), sin(theta), '-r'); hold off

Function plotParam t = linspace(0, 4*pi); x = sin(t); 一條參數式的曲線可由下列方程式表示: x = sin(t), y = 1 - cos(t) + t/10 當 t 由 0 變化到 4*pi 時,請寫一個 MATLAB 的腳本 plotParam.m,畫出此曲線在 XY 平面的軌跡。 Function plotParam t = linspace(0, 4*pi); x = sin(t); y = 1-cos(t)+t/10; plot(x, y, '-o');

試寫一函數 regStar(n),其功能為畫出一個圓心在 (0, 0)、半徑為 1 的圓,並在圓內畫出一個內接正 n 星形,其中一頂點位於 (0, 1)。例如 regStar(7) 可以畫出如下之正 7 星型: function regStar(n) vertices=[1]; for i=1:n step=2*pi*floor(n/2)/n; vertices=[vertices, exp(i*step*sqrt(-1))]; end plot(vertices, '-o'); % 畫外接圓 hold on theta=linspace(0, 2*pi); plot(cos(theta), sin(theta), '-r'); hold off axis image

平面上的橢圓可表成:(x/a)2+(y/b)2=1, 橢圓參數式表示成: x = a. cos(t) 與 y = a 平面上的橢圓可表成:(x/a)2+(y/b)2=1, 橢圓參數式表示成: x = a*cos(t) 與 y = a*sin(t) 請利用上述參數式,寫一個函數 ellipse(a, b),其功能是畫出一個橢圓,且橢圓上共有100個點。如 ellipse(7, 2) 可以產生下列圖形: function ellipse(a, b) % 畫橢圓 theta=linspace(0, 2*pi, 101); plot(a*cos(theta), b*sin(theta)); axis image

請用寫一個 MATLAB 腳本 figSolve.m,利用圖解法,說明下列聯立方程式有無窮多組解:y = x; y = sin(1/x) Function figSolve x=linspace(0,1); plot(x, x, x, sin(1./x));

當一個小圓輪在平面上滾動時,輪緣的一點在滾動時所形成的軌跡稱為「擺線」(Cycloid)。請用 MATLAB 畫出一個典型的擺線,其中小圓輪的半徑為 1,而且至少要滾三圈。 clear all; r=1; t1=linspace(0,2*pi,1000); t2=linspace(0,6*pi,1000); x1=r*(t1-sin(t1)); y1=r*(1-cos(t1)); x2=r*(t2-sin(t2)); y2=r*(1-cos(t2)); subplot(2,1,1);plot(x1,y1); title('小圓輪滾一圈'); subplot(2,1,2);plot(x2,y2); title('小圓輪滾三圈');

當一個小圓輪沿著一條曲線行進時,輪緣任一點的軌跡就會產生變化豐富的擺線。假設小圓輪的半徑 r=2。當小圓輪繞著一個大圓(半徑 R=5)的外部滾動時,請畫此「圓輪擺線」或「外花瓣線」。重複上小題,但改成在大圓的內部滾動,請畫出此「內花瓣線」。 R=5; % 大圓半徑 r=2; % 小圓半徑 n=r/gcd(r, R); % 圈數 t=linspace(0, n*2*pi, 1000); c=R*exp(i*t); % 內花瓣線 c1=(R-r)*exp(i*t)+r*exp(i*(-R*t/r+t)); % 外花瓣線 c2=(R+r)*exp(i*t)+r*exp(i*(pi+R*t/r+t)); plot(real(c), imag(c), real(c1), imag(c1), real(c2), imag(c2)); axis image title('內花瓣線(綠色)和外花瓣線(紅色)');

若大圓和小圓的半徑成整數比,當小圓在大圓的內部滾動時,小圓內的任一點 A 的軌跡就會形成一個漂亮無缺的花瓣線。當大圓半徑 R 為 10,小圓半徑 r 為 3,且 A 點離小圓圓心距離 r1 為 2 時,請畫出此完整的花瓣線。 r=3; % 小圓半徑 R=10; % 大圓半徑 d=2; % 筆心離小圓圓心距離 n=r/gcd(r, R); % 圈數 t=linspace(0, n*2*pi, 1000); c=R*exp(i*t); % 內花瓣線 c1=(R-r)*exp(i*t)+d*exp(i*(-R*t/r+t)); % 外花瓣線 c2=(R+r)*exp(i*t)+d*exp(i*(pi+R*t/r+t)); plot(real(c), imag(c), real(c1), imag(c1), real(c2), imag(c2)); axis image title('內花瓣線(綠色)和外花瓣線(紅色)');

HW#2 Snell’s Law n1 sin(θ1)=n2 sin(θ2) 光線由空氣進入水中,請繪圖 plot(x,y) ,其中x為入射角由0~90度,y為R折射角 光線由水進入空氣中,其他同(a)

HW#2