disp This program solves for the roots of

  • Slides: 53
Download presentation

disp ('This program solves for the roots of a quadratic '); disp ('equation of

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 = b^2 - 4 * a * c; % Solve for the roots x 1 = ( -b + sqrt(discriminant) ) / ( 2 * a ); x 2 = ( -b - sqrt(discriminant) ) / ( 2 * a ); % Display results disp ('The roots of this equation are: '); fprintf ('x 1 = (%f) +i (%f)n', real(x 1), imag(x 1)); fprintf ('x 2 = (%f) +i (%f)n', real(x 2), imag(x 2)); 其它的資料型態與繪圖型態 T. -M. Huang 5

複數資料的圖形 t = 0: pi/20: 4*pi; y = exp(-0. 2*t). *(cos(t)+i*sin(t)); plot(t, y, 'Line.

複數資料的圖形 t = 0: pi/20: 4*pi; y = exp(-0. 2*t). *(cos(t)+i*sin(t)); plot(t, y, 'Line. Width', 2); title('bf. Plot of complex function vs time'); xlabel('bfit t'); ylabel('bfit y(t)'); Warning: Imaginary parts of complex X and/or Y arguments ignored 其它的資料型態與繪圖型態 T. -M. Huang 6

複數資料的圖形 t = 0: pi/20: 4*pi; y = exp(-0. 2*t). *(cos(t)+i*sin(t)); plot(t, real(y), 'b-',

複數資料的圖形 t = 0: pi/20: 4*pi; y = exp(-0. 2*t). *(cos(t)+i*sin(t)); plot(t, real(y), 'b-', 'Line. Width', 2); hold on plot(t, imag(y), 'r--', 'Line. Width', 2); title('bf. Plot of complex function vs time'); xlabel('bfit t'); ylabel('bfit y(t)'); legend('real', 'imaginary'); hold off 其它的資料型態與繪圖型態 T. -M. Huang 7

複數資料的圖形 t = 0: pi/20: 4*pi; y = exp(-0. 2*t). *(cos(t)+i*sin(t)); plot(y, 'b-', 'Line.

複數資料的圖形 t = 0: pi/20: 4*pi; y = exp(-0. 2*t). *(cos(t)+i*sin(t)); plot(y, 'b-', 'Line. Width', 2); title('bf. Plot of complex function'); xlabel('bfit Real part'); ylabel('bfit Imaginary part'); 其它的資料型態與繪圖型態 T. -M. Huang 8

複數資料的圖形 t = 0: pi/20: 4*pi; y = exp(-0. 2*t). *(cos(t)+i*sin(t)); polar(angle(y), abs(y)); title('bf.

複數資料的圖形 t = 0: pi/20: 4*pi; y = exp(-0. 2*t). *(cos(t)+i*sin(t)); polar(angle(y), abs(y)); title('bf. Plot of complex function'); 其它的資料型態與繪圖型態 T. -M. Huang 9

字串轉換函式 double 指令: 檢視字串變數的儲存內容(即 ASCII 內碼) >> sentence = 'I''ve got a date!'; >>

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

一個變數來儲存多個字串(cont. ) >> departments = char('ee', 'cs', 'econ'); >> dept 1 = departments(1, :

一個變數來儲存多個字串(cont. ) >> departments = char('ee', 'cs', 'econ'); >> dept 1 = departments(1, : ) dept 1 = ee >> len 1 = length(dept 1) len 1 = 4 其它的資料型態與繪圖型態 T. -M. Huang >> dept 2 = deblank(dept 1) dept 2 = ee >> len 2 = length(dept 2) len 2 = 2 17

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

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

比 較 字 串(cont. ) >> str 1 = 'hello'; >> str 2 =

比 較 字 串(cont. ) >> str 1 = 'hello'; >> str 2 = 'Hello'; >> str 3 = 'hello '; >> c = strcmp(str 1, str 2) c= 0 >> c = strcmp(str 1, str 3) c= 0 其它的資料型態與繪圖型態 >> c = strcmpi(str 1, str 2) c= 1 >> c = strncmp(str 1, str 2, 3) c= 0 >> c = strncmp(str 1, str 3, 5) c= 1 T. -M. Huang 21

在字串裡分類字元(cont. ) Category Isstrprop(‘str’, ’category’ ) Description alpha True for those elements of str

在字串裡分類字元(cont. ) Category Isstrprop(‘str’, ’category’ ) Description alpha True for those elements of str that are alphanumeric(字母) alphanum True for those elements of str that are alphanumeric(字母或數字) cntrl True for those elements of str that are control characters digit True for those elements of str that are numeric digits lower True for those elements of str that are lowercase letters wspace True for those elements of str that are white-space characters(空白字元) upper True for those elements of str that are uppercase letters xdigit True for those elements of str that are valid hexadecimal digits >> A = isstrprop('abc 123 def', 'alpha') A= 111000111 其它的資料型態與繪圖型態 >> A = isstrprop('abcd 1234 efgh', 'xdigit') A= 1111100 T. -M. Huang 23

在字串內搜尋並取代字元(cont. ) strrep 指令: 用於字串尋找並代換 >> s 1 = 'This is a good example.

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

在字串內搜尋並取代字元(cont. ) 使用strtok 將一個句子拆解成幾個字 input_string = 'ee cs econ stat me'; remainder = input_string;

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

移除字串裡的空白字元 deblank 指令用來移除尾部的空白字元。 strtrim 指令用來移除字串前或字串後的空白字元。 >> test_string_trim 2 = >> test_string = ' This

移除字串裡的空白字元 deblank 指令用來移除尾部的空白字元。 strtrim 指令用來移除字串前或字串後的空白字元。 >> test_string_trim 2 = >> test_string = ' This is a test. '; strtrim(test_string) >> length(test_string) test_string_trim 2 = ans = This is a test. 21 >> length(test_string_trim 2) ans = >> test_string_trim 1 = deblank(test_string) 15 test_string_trim 1 = This is a test. >> length(test_string_trim 1) ans = 18 其它的資料型態與繪圖型態 T. -M. Huang 28

數字轉換成字串 int 2 str 指令 : 將整數型態的資料轉換成字串資料 >> >> int 2 str(2+3 ) ans

數字轉換成字串 int 2 str 指令 : 將整數型態的資料轉換成字串資料 >> >> int 2 str(2+3 ) ans = 5 >> n = 6; >> y = ['case number ' int 2 str(n)] y= case number 6 num 2 str 指令: 將實數轉為字串 >> x = rand(3) * 9999; % Create a 2 -by-3 matrix. >> x(3, : ) = []; >> A = num 2 str(x, '%10. 5 en') % Convert to string array. 其它的資料型態與繪圖型態 T. -M. Huang int 2 str(eye(3) ) ans = 1 0 0 0 1 A= 9. 50034 e+003 4. 85934 e+003 4. 56422 e+003 2. 31115 e+003 8. 91210 e+003 1. 85018 e+002 29

function result = c_strcmp(str 1, str 2) % Check for a legal number of

function result = c_strcmp(str 1, str 2) % Check for a legal number of input arguments. msg = nargchk(2, 2, nargin); error(msg); 驗證輸入字串 % Check to see if the arguments are strings if ~(isstr(str 1) & isstr(str 2)) 將字串延展成 error('Both str 1 and str 2 must both be strings!') else 相等的長度 % Pad strings = strvcat(str 1, str 2); 從頭到尾比較兩字串的 % 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 locates all nonzero ival = find(diff); elements of array diff if strings(1, ival(1)) > strings(2, ival(1)) result = 1; else result = -1; end end 其它的資料型態與繪圖型態 T. -M. Huang 33

測試結果 >> result = c_strcmp('String 1', 'String 1') result = >> result = c_strcmp('String

測試結果 >> result = c_strcmp('String 1', 'String 1') result = >> result = c_strcmp('String 1', 'String 1 ') 0 result = 0 >> result = c_strcmp('String 1', 'String 2') result = >> result = c_strcmp('String 1', 'String 0') -1 result = 1 >> result = c_strcmp('String 1', 'str') result = -1 其它的資料型態與繪圖型態 T. -M. Huang 34

6 -5 其它的二維圖形 subplot(2, 2, 3) barh(Y, 'stack') draws bar for each element in

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

其它的二維圖形(cont. ) ezplot(fun) plots the expression fun(x) over the default domain -2 <x<2 ezplot(fun,

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

其它的二維圖形(cont. ) fplot(fun, limits) fplot(@(x)[tan(x), sin(x), cos(x)], 2*pi*[-1 1]) plots fun between the limits

其它的二維圖形(cont. ) fplot(fun, limits) fplot(@(x)[tan(x), sin(x), cos(x)], 2*pi*[-1 1]) plots fun between the limits specified by limits is a vector specifying the x-axis limits ([xmin xmax]), or the x- and y-axes limits, ([xmin xmax ymin ymax]). 其它的資料型態與繪圖型態 T. -M. Huang 43

t = 0: 0. 01: 10; x = exp(-0. 2*t). * cos(2*t); y =

t = 0: 0. 01: 10; x = exp(-0. 2*t). * cos(2*t); y = exp(-0. 2*t). * sin(2*t); plot 3(x, y, t, 'Line. Width', 2); title('bf. Three-Dimensional Line Plot'); xlabel('bfx'); ylabel('bfy'); zlabel('bftime'); grid on; 其它的資料型態與繪圖型態 T. -M. Huang 45

mesh 和 surf mesh:可畫出立體的「網狀圖」(Mesh Plots) surf:可畫出立體的「曲面圖」(Surface Plots) z = [0 2 1; 3 2

mesh 和 surf mesh:可畫出立體的「網狀圖」(Mesh Plots) surf:可畫出立體的「曲面圖」(Surface 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 46