Matlab基本運算習題解答(第二章)

格式
pdf
大小
233.62 KB
頁數
10
作者
Kitty
收藏 ⬇️ 下載檔案
提示: 文件格式为 pdf,轉換可能會出現排版或格式的些許差異,請以實際檔案為準。
此檔案建立於 2013-09-09,离现在 12 47 天,建議確認內容是否仍然適用。
PDF 加载中...
background image

Matlab  程式設計 第二版 洪維恩編著 
旗標出版

   

第二章 

Matlab 基本運算 習題參考答案 

 

2.1  簡單的運算   

1.  試計算下列各式: 

(a) 45 53.2

 (b) 

3.1416 2

  (c) 

 

3

6.25

(d) 

 

0.3

( 1.25)

(e) 

 

1.6

( 4.83)

(f) 

 

0.5

( 2)

Ans: 

 

(a)   >> 45+53.2 

ans = 
    98.2000 
 

(b) 

>> 3.1416*2 
ans = 
    6.2832 
 

(c) 

>> 6.25^3 
ans = 
    244.1406 
 

(d) 
 

>> (-1.25)^0.3 
ans = 
   0.6285 + 0.8650i 
 

(e) 

>> (-4.83)^(-1.6) 
ans = 
     0.0249 + 0.0765i 
 

(f) 

>> (-2)^0.5 
ans = 

0.0000 + 1.4142i 

 

 

 

background image

2-2  Matlab 程式設計 

2.  試計算下列各式,並說明所得之結果所代表的意義: 

(a) 

 

2

 

(b) 12 /

  (c) 

10 realmax

 

(d) 

 

realmin+1

(e)  2*pi 

(f)  0/0 

Ans: 

 

(a)   >> Inf*2  

 

% 無限大與任何正數相乘,得到的結果都是無限大 

ans = 
    Inf 
 

(b) 

>> 12/Inf   % 任何數除以無限大,其結果都是 0 
ans = 
    0 

 

(c) 

>> 10*realmax  

% 超過 realmax 的數,Matlab 都會視為無限大 

ans = 
    Inf 
 

(d) 

>> realmin+1  

% realmin 這個數為 2.2251e-308,比 1 小得太多, 

ans = 

 

 

% 加上 1 之後會被捨位,因此結果為 1 


 

(e)  >> 2*pi  

 

% 計算 2

,得到的結果為

6.2832 

ans = 

    6.2832 
 

(f) 

>> 0/0     % 計算 0/0,Matlab 回應常數 NaN, 

 % 代表計算結果並非一個數值。 

ans = 
    NaN 
 

 

 

 

 

 

background image

   

Matlab 基本運算  2-3 

2.2  常用的數學函數   

3.  試計算下列各式: 

(a) 

sin

/ 4

 

(b) 

 

1

cos 0.5

(c) 

 

log1.2

(d) 

 

4

log 12

(e)  (4

3) /(5

2)

i

i

 

(f) 

i

e

 

(g)  36  

(h)  (4

3) /(5

2)

i

i

 

(i) 

3

101

 

Ans: 

 

(a)   >> sin(pi/4) 

ans = 
   0.7071 
 

(b) 

>> acos(0.5) 
ans = 
   1.0472 
 

(c) 

>> log(1.2) 
ans = 
   0.1823 
 

(d) 
 

>> log(12)/log(4) 
ans = 
   1.7925 
 

(e) 

>> (4i+3)/(5i+2) 
ans = 
   0.8966 - 0.2414i 
 

(f) 

>> exp(i*pi) 
ans = 
  -1.0000 + 0.0000i 
 

(g) 
 

>> sqrt(36) 
ans = 
     6 
 

background image

2-4  Matlab 程式設計 

(h) 

>> log10(1000) 
ans = 
   3 
 

(i) 

>> exp(i*pi) 
ans = 
  -1.0000 + 0.0000i 

 

4.  試計算 12, 35 與 73 的最大公因數與最小公倍數。 

Ans: 

 

>> gcd(12,gcd(35,73))   % 12,35 與 73 的最大公因數 
 
ans = 


 

>> lcm(12,lcm(35,73))   % 12,35 與 73 的最小公倍數 
 
ans = 

30660 
 

5.  試判別 512767 是否為質數,並找出其所有的質因數。 

Ans: 

 

>> isprime(512767)    % Matlab 回應 1,代表它是質數。 
ans = 
     1 
 
>> factor(512767)     % 512767 的質因數也是 512767,所以它是質數。 
ans = 
      512767 
 

6.  試找出所有小於 100 的質數。這些質數共有幾個?請用 length()  指令來計算。 

Ans: 

 

>> primes(100) 
ans = 
  Columns 1 through 10 

background image

   

Matlab 基本運算  2-5 

 
     2     3     5     7    11    13    17    19    23    29 
 
  Columns 11 through 20 
 
    31    37    41    43    47    53    59    61    67    71 
 
  Columns 21 through 25 
 
    73    79    83    89    97 
 
>> length(primes(100)) 
ans = 
    25 
 

7.  試求出小於等於

5.2

的最大整數。

 

Ans: 

 

>> floor(exp(5.2)) 
ans = 
   181 
 

8.  試求出 564/73 的餘數。 

Ans: 

 

>> rem(564,73) 
ans = 
     53 
 

9.  試計算 12 的階乘。 

Ans: 

 

>> factorial(12) 
ans = 
     479001600 
 
 
 

background image

2-6  Matlab 程式設計 

2.3  陣列   

10.  試計算列向量 [1,5,12,19,23] 的總和與平均。 

Ans: 

 

>> v1=[1,5,12,19,23]    
v1 = 
     1     5    12    19    23 
 
>> sum(v1) 
ans = 
    60 
 
>> sum(v1)/length(v1) 
ans = 
    12 
 

11.  試計算行向量 [1;4;6;8;9] 的乘積。 

Ans: 

 

>> v2=[1;4;6;8;9] 
v2 = 
     1 
     4 
     6 
     8 
     9 
 
>> prod(v2) 
ans = 
        1728 
 

12.  試找出列向量  [4, 8, 9, 3, 6]  裡最小的元素與其位置。 

Ans: 

 

>> v3=[4, 8, 9, 3, 6] 
v3 = 
     4     8     9     3     6 
 
>> [val,ind]=min(v3) 

background image

   

Matlab 基本運算  2-7 

val = 
     3 
 
ind = 
     4 
 

13.  試建立一個 1~100,間距為 1 的向量,並計算其總和。 

Ans: 

 

>> sum(1:1:100) 
ans = 
        5050 
 

14.  試將向量 [2,7,9,3,1] 由大到小排列。 

Ans: 

 

>> v4=[2,7,9,3,1] 
v4 = 
     2     7     9     3     1 
 
>> sort(v4,'descend') 
ans = 
     9     7     3     2     1 
 

15.  試建立一個具有 12 個元素,範圍為 0~2

的向量,並將所有的元素進行

sin 運算。 

Ans: 

 

>> sin(linspace(0,2*pi,12)) 
ans = 
  Columns 1 through 6 
 
         0    0.5406    0.9096    0.9898    0.7557    0.2817 
 
  Columns 7 through 12 
 
   -0.2817   -0.7557   -0.9898   -0.9096   -0.5406   -0.0000 
 
 
 

background image

2-8  Matlab 程式設計 

16.  設向量  v=[0, 12, 17, 21, 13,67,88,61],試依序作答下列的問題: 

(a)  試問 是一個行向量還是列向量? 

(b)  試找出向量 的大小與維度。 

(c)  將列向量 轉置成行向量。 

(d)  將向量 由大到小排序。 

(e)  找出向量 的最大值與最小值。 

(f)  計算向量 的累加。 

Ans:  

>> v=[0,12,17,21,13,67,88,61] 
v = 
     0    12    17    21    13    67    88    61 
 
 

(a)   列向量 

 

(b) 

>> size(v) 
ans = 
     1     8 
 
>> ndims(v) 
ans = 
     2 
 

(c) 

>> v' 
ans = 
     0 
    12 
    17 
    21 
    13 
    67 
    88 

    61 

(d) 

>> sort(v,'descend') 

background image

   

Matlab 基本運算  2-9 

ans = 
    88    67    61    21    17    13    12     0 
 

(e) 
 

>> max(v) 
ans = 
    88 
 
>> min(v) 
ans = 
     0 
 

(f) 

>> cumsum(v) 
 
ans = 
     0    12    29    50    63   130   218   279 

 

17.  設矩陣 = [ 0   3   7   2 ;   2   4   6   1 ;   9   4   1   6 ] ,試依序作答下列的問題: 

(a)  試找出矩陣 的大小與維度。 

(b)  試找出矩陣 元素個數的總數。 

(c)  計算矩陣 所有元素的總和。 

(d)  找出矩陣 最大的元素值。 

Ans:  

>> m=[0 3 7 2; 2 4 6 1; 9 4 1 6] 
m = 
     0     3     7     2 
     2     4     6     1 
     9     4     1     6 
 
 
 
 

(a)   >> size(m) 

ans = 
     3     4 
 

background image

2-10  Matlab 程式設計 

>> ndims(m) 
ans = 
     2 
 

(b)  >> numel(m) 

ans = 
    12 
 

(c)  >> sum(sum(m)) 

ans = 
    45 
 

(d) 
 

>> max(max(m)) 
ans = 
     9 

 

 

版權說明: 檔案資源由用戶上傳,僅供學習交流使用,尊重著作權。 若您認為內容涉及侵權,請點擊「侵權舉報」提交相關資料,我們將儘快核實並處理。