1 / 53

其它的資料型態與繪圖型態

其它的資料型態與繪圖型態. 黃聰明 國立臺灣師範大學數學系 min@ntnu.edu.tw http://math.ntnu.edu.tw/~min. 6-1 複數資料. 支援複數資料型態的函式. 範例:一元二次方程式. 輸出二次方程式的根. 輸入係數 a 、 b 、 c. 不同的實數根. 不區分. 重複的實數根. 複 數 根. disp ('This program solves for the roots of a quadratic '); disp ('equation of the form A*X^2 + B*X + C = 0. ');

lanza
Télécharger la présentation

其它的資料型態與繪圖型態

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. 其它的資料型態與繪圖型態 黃聰明國立臺灣師範大學數學系 min@ntnu.edu.tw http://math.ntnu.edu.tw/~min

  2. T.-M.Huang

  3. 6-1 複數資料 T.-M.Huang

  4. 支援複數資料型態的函式 T.-M.Huang

  5. 範例:一元二次方程式 輸出二次方程式的根 輸入係數 a、b、c 不同的實數根 不區分 重複的實數根 複 數 根 T.-M.Huang

  6. disp ('This program solves for the roots of a quadratic '); disp ('equation of the form A*X^2 + B*X + C = 0. '); a = input ('Enter the coefficient A: '); b = input ('Enter the coefficient B: '); c = input ('Enter the coefficient C: '); % Calculate discriminant discriminant = b^2 - 4 * a * c; % Solve for the roots x1 = ( -b + sqrt(discriminant) ) / ( 2 * a ); x2 = ( -b - sqrt(discriminant) ) / ( 2 * a ); % Display results disp ('The roots of this equation are:'); fprintf ('x1 = (%f) +i (%f)\n', real(x1), imag(x1)); fprintf ('x2 = (%f) +i (%f)\n', real(x2), imag(x2)); T.-M.Huang

  7. 複數資料的圖形 t = 0:pi/20:4*pi; y = exp(-0.2*t).*(cos(t)+i*sin(t)); plot(t, y, 'LineWidth', 2); title('\bfPlot of complex function vs time'); xlabel('\bf\it t'); ylabel('\bf\it y(t)'); Warning: Imaginary parts of complex X and/or Y argumentsignored T.-M.Huang

  8. 複數資料的圖形 t = 0:pi/20:4*pi; y = exp(-0.2*t).*(cos(t)+i*sin(t)); plot(t, real(y), 'b-', 'LineWidth', 2); hold on plot(t, imag(y), 'r--', 'LineWidth', 2); title('\bfPlot of complex function vs time'); xlabel('\bf\it t'); ylabel('\bf\it y(t)'); legend('real', 'imaginary'); hold off T.-M.Huang

  9. 複數資料的圖形 t = 0:pi/20:4*pi; y = exp(-0.2*t).*(cos(t)+i*sin(t)); plot(y, 'b-', 'LineWidth', 2); title('\bfPlot of complex function'); xlabel('\bf\it Real part'); ylabel('\bf\it Imaginary part'); T.-M.Huang

  10. 複數資料的圖形 t = 0:pi/20:4*pi; y = exp(-0.2*t).*(cos(t)+i*sin(t)); polar(angle(y), abs(y)); title('\bfPlot of complex function'); T.-M.Huang

  11. 6-2 字串函式 數個字元(Characters)可以構 成一個字串(Strings) 一個字串是被視為一個列向量 (Row Vector)進行儲存 字串中的每一字元(含空白字元),是 以其 ASCII 碼的形式存放於此列向量 中的每一個元素(Element) T.-M.Huang

  12. 字元與字串的基本概念 Matlab 用「單引號」來界定字串變數,多個 字串變數可直接並排,以得到一個新字串變數 str1 = 'I like MATLAB,'; % 建立字串變數str1 str2 = ' JavaScript, and Perl!'; % 建立字串變數str2 str3 = [str1 str2] % 直接並排str1及str2,以建立str3 str3 = I like MATLAB, JavaScript, and Perl! T.-M.Huang

  13. 欲輸入含有單引號的字串,可重覆單引號的使用。欲輸入含有單引號的字串,可重覆單引號的使用。 字元與字串的基本概念(cont) 若要計算字串變數的長度(即組成字元的個數),可用 length 指令。 T.-M.Huang

  14. 字元的儲存 無論是中文或英文,每一個字元都會佔用兩個位元組(2 Bytes), 故字串變數 sentence = 'I''ve got a date!' 總共由 16 個字 元構成,佔用的記憶體總計為三十二個位元組(32 bytes) whos 指令: 檢視字串變數 sentence 所佔用儲存空間(whos 變數) MATLAB 是以兩個位元組來儲存一個字元,所以也可以支援 Big5 的中文碼,而且 Big5 中文的 ASCII 內碼都會大於數字 128 由於 MATLAB 將字串以其相對應之 ASCII 內碼(即數字形式)儲 存成一列向量,故若對此字串直接進行數值運算,MATLAB 會先將 此字串轉成數值,再進行一般數值向量的運算 T.-M.Huang

  15. 字串的判斷 class 或 ischar 指令: 判斷某一個變數是否為字串 >> chinese = '今日事,今日畢'; >> out1 = class(chinese) out1 = char >> chinese = '今日事,今日畢'; >> out1 = ischar(chinese) out1 = 1 >> chinese = '今日事,今日畢'; >> x = chinese+1; >> out2 = ischar(x) out2 = 0 T.-M.Huang

  16. 字串轉換函式 double 指令: 檢視字串變數的儲存內容(即 ASCII 內碼) char 指令: 將 ASCII 內碼轉回字串形式 >> sentence = 'I''ve got a date!'; >> sentenceAscii = double(sentence) sentenceAscii = Columns 1 through 14 73 39 118 101 32 103 111 116 32 97 32 100 97 116 Columns 15 through 16 101 33 >> sentence2 = char(sentenceAscii) sentence2 = I've got a date! T.-M.Huang

  17. 一個變數來儲存多個字串 >> departments = ['ee '; 'cs '; 'econ'] % 注意空白字元的使用 departments = ee cs econ >> departments = char('ee', 'cs', 'econ') % 注意「()」及「,」的使用 departments = ee cs econ T.-M.Huang

  18. 一個變數來儲存多個字串(cont.) 從二維字元陣列抽取出字串時,切記要使用deblank 指令來移除尾部的空白字元 >> departments = char('ee', 'cs', 'econ'); >> dept1 = departments(1,:) dept1 = ee >> len1 = length(dept1) len1 = 4 >> dept2 = deblank(dept1) dept2 = ee >> len2 = length(dept2) len2 = 2 T.-M.Huang

  19. 整 合 字 串 strcat可以水平連接兩個以上的字串。這個函式將清除字串後 面的空白字元,但保留字串間的空白字元。 >> result = strcat(‘String 1 ’, ‘String 2’) result = String 1String 2(注意第一個字串後面之空白字元已清除) FileName1 = 'fz_cn_rate_d6_L'; for Fm = 0:5 FileName = strcat(FileName1, int2str(Fm)); eval(FileName); end T.-M.Huang

  20. 整 合 字 串(cont) strvcat可以垂直連接兩個以上的字串,並自動延展字串長度,使其成為合法的二維陣列。 >> length(result(1,:)) ans = 14 >> length(result(2,:)) ans = 14 >> result = strvcat(‘Long String 1 ’, ‘String 2’) result = Long String 1 String 2 T.-M.Huang

  21. 比 較 字 串 strcmp指令: 用於比較字串內容是否完全相同,不相同回傳0, 相同則回傳1 包含字串前面與字串尾端的空白字元 strcmpi指令: 用於比較字串內容是否完全相同,但忽略大小 寫的差異 strncmp指令:用於比較字串的前 n 個字元是否完全相同 strncmpi指令: 用於比較字串的前 n 個字元是否完全相同,但 忽略大小寫的差異 T.-M.Huang

  22. 比 較 字 串(cont.) >> str1 = 'hello'; >> str2 = 'Hello'; >> str3 = 'hello '; >> c = strcmpi(str1, str2) c = 1 >> c = strncmp(str1, str2, 3) c = 0 >> c = strncmp(str1, str3, 5) c = 1 >> c = strcmp(str1, str2) c = 0 >> c = strcmp(str1, str3) c = 0 T.-M.Huang

  23. 在字串裡分類字元 >> mystring = 'Room 23a'; >> a = isletter(mystring) a = 1 1 1 1 0 0 0 1 >> b = isspace(mystring) b = 0 0 0 0 1 0 0 0 T.-M.Huang

  24. 在字串裡分類字元(cont.) Isstrprop(‘str’,’category’) >> A = isstrprop('abc123def', 'alpha') A = 1 1 1 0 0 0 1 1 1 >> A = isstrprop('abcd1234efgh', 'xdigit') A = 1 1 1 1 1 1 1 1 1 1 0 0 T.-M.Huang

  25. 在字串內搜尋並取代字元 findstr指令:尋找在某一個長字串中的子字串,並傳回其起 始位置 >> s = 'Find the starting indices of the shorter string.'; >> findstr(s, 'the') ans = 6 30 >> findstr('the', s) ans = 6 30 strmatch指令:用來比對字元,可在二維字元陣列裡找出某 列文字的開始字元,並傳回其文字列編號 >> x = strmatch('max', strvcat('max', 'minimax', 'maximum')) x = 1 3 T.-M.Huang

  26. 在字串內搜尋並取代字元(cont.) strrep 指令: 用於字串尋找並代換 >> s = ' This is a simple example.'; >> [token, remain] = strtok(s) token = This remain = is a simple example. >> s1 = 'This is a good example.'; >> str = strrep(s1, 'good', 'great') str = This is a great example. strtok 指令: 根據一給定的分界字元(Delimiting Characters), 將一字串拆解成數個字串,預設分界字元為空白字元 [token, remain] = strtok(string, delim) T.-M.Huang

  27. 在字串內搜尋並取代字元(cont.) input_string = 'eecs econ stat me'; remainder = input_string; parsed = ''; % 建立一空字元陣列 while (any(remainder)) [chopped, remainder] = strtok(remainder); parsed = strvcat(parsed, chopped); end parsed parsed = ee cs econ stat me 使用strtok 將一個句子拆解成幾個字 T.-M.Huang

  28. 大小寫字母轉換 upper及lower可以將字串裡所有的字元, 完全轉換成字母大寫或字母小寫 >> lower('MathWorks') ans = mathworks >> upper('attention!') ans = ATTENTION! T.-M.Huang

  29. 移除字串裡的空白字元 deblank 指令用來移除尾部的空白字元。 strtrim 指令用來移除字串前或字串後的空白字元。 >> test_string_trim2 = strtrim(test_string) test_string_trim2 = This is a test. >> length(test_string_trim2) ans = 15 >> test_string = ' This is a test. '; >> length(test_string) ans = 21 >> test_string_trim1 = deblank(test_string) test_string_trim1 = This is a test. >> length(test_string_trim1) ans = 18 T.-M.Huang

  30. 數字轉換成字串 int2str 指令: 將整數型態的資料轉換成字串資料 >> int2str(eye(3)) ans = 1 0 0 0 1 0 0 0 1 >> n = 6; >> y = ['case number ' int2str(n)] y = case number 6 >> int2str(2+3) ans = 5 num2str 指令: 將實數轉為字串 A = 9.50034e+003 4.85934e+003 4.56422e+003 2.31115e+003 8.91210e+003 1.85018e+002 >> x = rand(3) * 9999; % Create a 2-by-3 matrix. >> x(3,:) = []; >> A = num2str(x, '%10.5e\n') % Convert to string array. T.-M.Huang

  31. 數字轉換成字串(cont.) mat2str 指令可將矩陣轉換為字串,此字串若再 經由 eval指令的使用,可再變回原先的矩陣 >> A = [1 2 1; 3 5 6 ]; >> B = mat2str(A) % 將矩陣A轉成字串B B = [1 2 1;3 5 6] >> A2 = eval(B) % 再將字串 B 轉回矩陣 A2 A2 = 1 2 1 3 5 6 >> isequal(A, A2) % 測試 A 和 A2 是否相等 ans = 1 T.-M.Huang

  32. 字串轉換成數字 >> s = '2.7183 3.1416'; A = sscanf(s,'%f') A = 2.7183 3.1416 >> a = str2double('123 + 45i') a = 1.2300e+002 +4.5000e+001i T.-M.Huang

  33. 範例:字串比較函式 比較兩個字串,如果第一個字串在字典式順序裡小於第二個字串 ,則傳回值-1,如果兩個字串順序相等,則傳回0,如果第一個字 串在字典式順序裡大於第二個字串,則傳回值+1 驗證輸入字串 函式需有兩個輸入字元格式的引數 步驟 將字串延展成相等的長度 使用strvcat將兩個字串變成一個二維陣列 strings = strvcat( str1, str2 ); 從頭到尾比較兩字串的不同之 處,並找出第一個不同的地方 使用關係運算子比對兩字串, 產生含0與1的陣列 尋找陣列中第一個1,此對應 兩字串第一個差異 根據第一個不同處,傳回對 應的值 T.-M.Huang

  34. function result = c_strcmp(str1,str2) % Check for a legal number of input arguments. narginchk(2,2); % Check to see if the arguments are strings if ~(ischar(str1) && ischar(str2)) error('Both str1 and str2 must both be strings!') else % Pad strings strings = char(str1,str2); % Compare strings diff = strings(1,:) ~= strings(2,:); if sum(diff) == 0 % Strings match, so return a zero! result = 0; else % Find first difference between strings ival = find(diff); if strings(1,ival(1)) > strings(2,ival(1)) result = 1; else result = -1; end end end 驗證輸入字串 將字串延展成 相等的長度 從頭到尾比較兩字串的 不同之處,並找出第一 個不同的地方 locates all nonzero elements of array diff T.-M.Huang

  35. 測試結果 >> result = c_strcmp('String 1', 'String 1') result = 0 >> result = c_strcmp('String 1', 'String 1 ') result = 0 >> result = c_strcmp('String 1', 'String 2') result = -1 >> result = c_strcmp('String 1', 'String 0') result = 1 >> result = c_strcmp('String 1', 'str') result = -1 T.-M.Huang

  36. 6-4 其它的資料型態(整數) 8、16 、32 、64位元的正負整數 8、16 、32 、64位元的正整數 T.-M.Huang

  37. 其它的資料型態(cont.) >> var = int8(3) var = 3 >> whos Name Size Bytes Class Attributes var 1x1 1 int8 >> int8(100)+int8(50) ans = 127 150 ??? 假設整數運算結果 比該資料型態的最 大值還要大,則產 生的結果便是該最 大值,而不是實際 計算得出的值。 >> b = 7.3; >> c = var * b c = 22 >> whos Name Size Bytes Class Attributes b 1x1 8 double c 1x1 1 int8 var 1x1 1 int8 計算中同時有整數值與雙倍精度值,將只產生整數型態的結果。 適用於影像資料處理。 T.-M.Huang

  38. 6-5 其它的二維圖形 subplot(2,2,3) barh(Y,'stack') title 'Stack' subplot(2,2,4) bar(Y,1.5) title 'Width = 1.5' bar(x,y), barh(x,y) draws a bar for each element in y at locations specified in x, where x is a vector defining the x-axis intervals for the vertical bars Y = round(rand(5,3)*10); subplot(2,2,1) bar(Y,'group') title 'Group' subplot(2,2,2) bar(Y,'stack') title 'Stack' T.-M.Huang

  39. 其它的二維圖形(cont.) compass(x,y) 產生極座標圖,並畫出從座標原點到資料點(x,y)的箭頭。圖形 內的資料點位置,是用直角座標表示,而不是用極座標表示。 Z = eig(randn(20,20)); compass(Z) Z = 1.8023 + 3.7533i 1.8023 - 3.7533i -0.8068 + 3.8624i -0.8068 - 3.8624i -3.5723 + 2.1144i -3.5723 - 2.1144i 2.7566 -0.2786 + 2.6347i -0.2786 - 2.6347i T.-M.Huang

  40. 其它的二維圖形(cont.) pie(x), pie(x, explode) 產生圓形圖,計算每個x值相對於全部的比例,並依此比例畫出 對等大小的扇形區域。選項陣列explode可以決定是否要把個別 的扇形區域,與其它區域隔開顯示。 x = [1 3 0.5 2.5 2]; explode = [0 1 0 0 0]; pie(x,explode) T.-M.Huang

  41. 其它的二維圖形(cont.) stairs(x,y) 產生階梯圖,每個階梯的中心落在資料點(x,y)上。 x = linspace(-2*pi,2*pi,40); stairs(x,sin(x)) T.-M.Huang

  42. 其它的二維圖形(cont.) stem(x,y) 產生長桿圖,每個資料點(x,y)上有個標記,並且由該點上垂直 畫一條‘桿子’連接到x軸上。 t = linspace(-2*pi,2*pi,10); h = stem(t,cos(t),'fill','--'); T.-M.Huang

  43. 其它的二維圖形(cont.) ezplot(fun) ezplot(fun,[min,max]) plots the expression fun(x) over the default domain -2 < x < 2 plots the expression fun(x) over the domain min < x < max >> ezplot('sin(x)/x',[-4*pi 4*pi]) >> title('Plot of sin(x) / x'); >> grid on; T.-M.Huang

  44. 其它的二維圖形(cont.) fplot(@(x)[tan(x),sin(x),cos(x)], 2*pi*[-1 1 -1 1]) fplot(fun,limits) plots fun between the limits specified by limits. limits is a vector specifying the x-axis limits ([xminxmax]), or the x- and y-axes limits, ([xminxmaxyminymax]). T.-M.Huang

  45. 6-6 三維圖形 plot3(x,y,z) 二維力學系統的阻尼振盪,x、y表示 在任何給定時間t之系統位置。 產生三維曲線圖形 t = 0:0.01:10; x = exp(-0.2*t) .* cos(2*t); y = exp(-0.2*t) .* sin(2*t); plot(x,y,'LineWidth',2); title('\bfTwo-Dimensional Line Plot'); xlabel('\bfx'); ylabel('\bfy'); axis square; grid on; 無法清楚地顯示時間對系統行為的重要性 T.-M.Huang

  46. t = 0:0.01:10; x = exp(-0.2*t) .* cos(2*t); y = exp(-0.2*t) .* sin(2*t); plot3(x,y,t,'LineWidth',2); title('\bfThree-Dimensional Line Plot'); xlabel('\bfx'); ylabel('\bfy'); zlabel('\bftime'); grid on; T.-M.Huang

  47. mesh 和 surf surf:可畫出立體的「曲面圖」(Surface Plots) mesh:可畫出立體的「網狀圖」(Mesh Plots) z = [0 2 1; 3 2 4; 4 4 4; 7 6 8]; mesh(z); xlabel('X 軸 = column index'); ylabel('Y 軸 = row index'); T.-M.Huang

  48. meshgrid 的作用是產生 x 及 y (均為向量) 為基準的格子點 (Grid Points),其輸出為 xx 及 yy(均為矩陣),分別代表格子點的 [x, y] = meshgrid(xstart:xinc:xend, ystart:yinc:yend) xx = 3 4 5 6 3 4 5 6 3 4 5 6 3 4 5 6 3 4 5 6 yy = 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 x = 3:6; y = 5:9; [xx, yy] = meshgrid(x, y) T.-M.Huang

  49. 使用 linspace 來產生較密集的資料,以便畫出由 函數形成的立體網狀圖。 x = linspace(-2, 2, 25); % 在 x 軸 [-2,2] 之間取 25 點 y = linspace(-2, 2, 25); % 在 y 軸 [-2,2] 之間取 25 點 [xx, yy] = meshgrid(x, y); % xx 和 yy 都是 25×25 的矩陣 zz = xx.*exp(-xx.^2-yy.^2); % 計算函數值,zz 也是 25×25 的矩陣 mesh(xx, yy, zz); % 畫出立體網狀圖 T.-M.Huang

  50. surf 和 mesh 指令的用法類似。 x = linspace(-2, 2, 25); % 在 x 軸 [-2,2] 之間取 25 點 y = linspace(-2, 2, 25); % 在 y 軸 [-2,2] 之間取 25 點 [xx, yy] = meshgrid(x, y); % xx 和 yy 都是 25×25 的矩陣 zz = xx.*exp(-xx.^2-yy.^2); % 計算函數值,zz 也是 25×25 的矩陣 surf(xx, yy, zz); T.-M.Huang

More Related