其它的資料型態與繪圖型態. 黃聰明 國立臺灣師範大學數學系 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. ');
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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
複數資料的圖形 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
複數資料的圖形 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
複數資料的圖形 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
複數資料的圖形 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
整 合 字 串 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
整 合 字 串(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
比 較 字 串 strcmp指令: 用於比較字串內容是否完全相同,不相同回傳0, 相同則回傳1 包含字串前面與字串尾端的空白字元 strcmpi指令: 用於比較字串內容是否完全相同,但忽略大小 寫的差異 strncmp指令:用於比較字串的前 n 個字元是否完全相同 strncmpi指令: 用於比較字串的前 n 個字元是否完全相同,但 忽略大小寫的差異 T.-M.Huang
比 較 字 串(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
在字串裡分類字元 >> 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
在字串裡分類字元(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
在字串內搜尋並取代字元 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
在字串內搜尋並取代字元(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
在字串內搜尋並取代字元(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
大小寫字母轉換 upper及lower可以將字串裡所有的字元, 完全轉換成字母大寫或字母小寫 >> lower('MathWorks') ans = mathworks >> upper('attention!') ans = ATTENTION! T.-M.Huang
移除字串裡的空白字元 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
數字轉換成字串 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
數字轉換成字串(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
字串轉換成數字 >> 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
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
測試結果 >> 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
其它的資料型態(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
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
其它的二維圖形(cont.) stairs(x,y) 產生階梯圖,每個階梯的中心落在資料點(x,y)上。 x = linspace(-2*pi,2*pi,40); stairs(x,sin(x)) T.-M.Huang
其它的二維圖形(cont.) stem(x,y) 產生長桿圖,每個資料點(x,y)上有個標記,並且由該點上垂直 畫一條‘桿子’連接到x軸上。 t = linspace(-2*pi,2*pi,10); h = stem(t,cos(t),'fill','--'); T.-M.Huang
其它的二維圖形(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
其它的二維圖形(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
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
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