Presentation is loading. Please wait.

Presentation is loading. Please wait.

分支宣告與程式設計 黃聰明 國立臺灣師範大學數學系

Similar presentations


Presentation on theme: "分支宣告與程式設計 黃聰明 國立臺灣師範大學數學系"— Presentation transcript:

1 分支宣告與程式設計 黃聰明 國立臺灣師範大學數學系 min@math.ntnu.edu.tw
黃聰明 國立臺灣師範大學數學系 分支宣告與程式設計 T.-M.Huang T.-M.Huang

2 3-1 由上而下的設計方法 開始 結束 陳述嘗試解決的問題 測試完成的MATLAB程式 定義輸入與輸出 把演算法轉換成MATLAB宣告式
由上而下的設計流程 開始 結束 陳述嘗試解決的問題 測試完成的MATLAB程式 定義輸入與輸出 分解成小問題 把演算法轉換成MATLAB宣告式 設計演算法 撰寫虛擬碼 T.-M.Huang

3 3-3 邏輯資料型態 邏輯資料型態 關係運算子 邏輯運算子 true false 運算子 說明 == 等於 ~= 不等於 > 大於
>= 大於或等於 < 小於 <= 小於或等於 邏輯運算子 運算子 說明 & AND && 快速求值的AND | OR || 快速求值的OR xor 互斥OR ~ NOT 分支宣告與程式設計 T.-M.Huang

4 真 值 表 & && | || xor ~ F T 分支宣告與程式設計 T.-M.Huang

5 3-4 分支 (Branching Command)
條件指令 if-else switch switch_expr case case_expr, statements case {case_expr1, case_expr2,...} statements ... otherwise, statements end switch - case - otherwise if expression statements elseif expression statements else statements end 分支宣告與程式設計 T.-M.Huang

6 範例:一元二次方程式 輸出二次方程式的根 輸入係數a、b、c 不同的實數根 重複的實數根 複數根 分支宣告與程式設計 T.-M.Huang

7 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, depending on the value of the discriminant if discriminant > 0 % there are two real roots, so... x1 = ( -b + sqrt(discriminant) ) / ( 2 * a ); x2 = ( -b - sqrt(discriminant) ) / ( 2 * a ); disp ('This equation has two real roots:'); fprintf ('x1 = %f\n', x1); fprintf ('x2 = %f\n', x2); elseif discriminant == 0 % there is one repeated root, so... x1 = ( -b ) / ( 2 * a ); disp ('This equation has two identical real roots:'); fprintf ('x1 = x2 = %f\n', x1); else % there are complex roots, so ... real_part = ( -b ) / ( 2 * a ); imag_part = sqrt ( abs ( discriminant ) ) / ( 2 * a ); disp ('This equation has complex roots:'); fprintf('x1 = %f +i %f\n', real_part, imag_part ); fprintf('x1 = %f -i %f\n', real_part, imag_part ); end

8 範例:根據月份來判斷其季別(1) for month = 1:12 switch month case {3,4,5}
season = 'Spring'; case {6,7,8} season = 'Summer'; case {9,10,11} season = 'Autumn'; case {12,1,2} season = 'Winter'; end fprintf('Month %d ===> %s.\n', month, season); 分支宣告與程式設計 T.-M.Huang

9 範例:根據月份來判斷其季別(2) month = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep'}; for i = 1:length(month) switch month{i} case {'Mar','Apr','May'} season = 'Spring'; case {'Jun','Jul','Aug'} season = 'Summer'; case {'Sep','Oct','Nov'} season = 'Autumn'; case {'Dec','Jan','Feb'} season = 'Winter'; end fprintf('%s is %s.\n', month{i}, season); 分支宣告與程式設計 T.-M.Huang

10 額外的繪圖功能 x = -2.5*pi:pi/100:2.5*pi; y1 = sin(2*x); y2 = 2*cos(2*x);
plot(x, y1,'ro-.'); hold on plot(x, y2, 'gx:'); hold off legend('sin(2x)','2cos(2x)','Location','NorthWest'); text(1.5, 0.5, 'sin(2x)') text(3.3, 1.5, '2cos(2x)') xlabel('x'); ylabel('y'); title('Plot figure of sin(2x) and 2cos(2x)'); set(gca,'xtick',[-2*pi -pi 0 pi 2*pi]) set(gca,'xticklabel',{'-2 pi','- pi','0','pi','2 pi'}) 分支宣告與程式設計 T.-M.Huang

11 額外的繪圖功能 axis([-2*pi 2*pi ]) 分支宣告與程式設計 T.-M.Huang

12 額外的繪圖功能 axis equal axis square axis normal 分支宣告與程式設計 T.-M.Huang

13 額外的繪圖功能 axis off axis on 分支宣告與程式設計 T.-M.Huang

14 x = -2.5*pi:pi/100:2.5*pi; y1 = sin(2*x); y2 = 2*cos(2*x); figure(1)
plot(x, y1,'ro-.'); title('plot sin(2x)'); figure(2) plot(x, y2, 'gx:'); title('plot 2 cos(2x)'); 分支宣告與程式設計 T.-M.Huang

15 subplot(m,n,p) x = -2.5*pi:pi/100:2.5*pi;
y1 = sin(2*x); y2 = 2*cos(2*x); z1 = exp(x/10); z1 = z1.*y2; z2 = y1.*y2; subplot(2,2,1) plot(x, y1,'r'); title('plot sin(2x)'); subplot(2,2,2) plot(x, y2, 'g'); title('plot 2 cos(2x)'); subplot(2,2,3) plot(x, z1, 'b'); title('plot 2cos(2x)exp(x/10)'); subplot(2,2,4) plot(x, z2, 'm'); title('plot 2cos(2x)sin(2x)'); 分支宣告與程式設計 T.-M.Huang


Download ppt "分支宣告與程式設計 黃聰明 國立臺灣師範大學數學系"

Similar presentations


Ads by Google