星期四, 11月 16, 2006

MATLAB課堂作業: 作業六

1. Run the function "freebody " in section 6.2 finding the height of the ball in terms of elapsed time in an interval of 0 to 30 seconds with an increment of 0.2 second.

ANS:
程式指令:

function y = freebody(time)
% calculation of height with respect to time
% time: the elapsed time in seconds
% y:the depth of falling, cm
% Example: yy=freebody(1:20)
y =1/2*980*time.*time; % y=(1/2)gt^2


執行結果:
>> freebody(0:0.2:30)

ans =

1.0e+005 *

Columns 1 through 11

0 0.0002 0.0008 0.0018 0.0031 0.0049 0.0071 0.0096 0.0125 0.0159 0.0196

Columns 12 through 22

0.0237 0.0282 0.0331 0.0384 0.0441 0.0502 0.0566 0.0635 0.0708 0.0784 0.0864

Columns 23 through 33

0.0949 0.1037 0.1129 0.1225 0.1325 0.1429 0.1537 0.1648 0.1764 0.1884 0.2007

Columns 34 through 44

0.2134 0.2266 0.2401 0.2540 0.2683 0.2830 0.2981 0.3136 0.3295 0.3457 0.3624

Columns 45 through 55

0.3795 0.3969 0.4147 0.4330 0.4516 0.4706 0.4900 0.5098 0.5300 0.5506 0.5715

Columns 56 through 66

0.5929 0.6147 0.6368 0.6593 0.6823 0.7056 0.7293 0.7534 0.7779 0.8028 0.8281

Columns 67 through 77

0.8538 0.8798 0.9063 0.9332 0.9604 0.9880 1.0161 1.0445 1.0733 1.1025 1.1321

Columns 78 through 88

1.1621 1.1925 1.2232 1.2544 1.2860 1.3179 1.3502 1.3830 1.4161 1.4496 1.4835

Columns 89 through 99

1.5178 1.5525 1.5876 1.6231 1.6589 1.6952 1.7319 1.7689 1.8063 1.8442 1.8824

Columns 100 through 110

1.9210 1.9600 1.9994 2.0392 2.0794 2.1199 2.1609 2.2023 2.2440 2.2861 2.3287

Columns 111 through 121

2.3716 2.4149 2.4586 2.5027 2.5472 2.5921 2.6374 2.6830 2.7291 2.7756 2.8224

Columns 122 through 132

2.8696 2.9173 2.9653 3.0137 3.0625 3.1117 3.1613 3.2113 3.2616 3.3124 3.3636

Columns 133 through 143

3.4151 3.4670 3.5194 3.5721 3.6252 3.6787 3.7326 3.7869 3.8416 3.8967 3.9521

Columns 144 through 151

4.0080 4.0643 4.1209 4.1779 4.2354 4.2932 4.3514 4.4100

2. Draw two circles with radii of 3 and 5 cm each and their centers located at (0,0) and (10, 0) cm.

ANS:
因為欲求的圓心座標與半徑長已知,所以我以一個簡單的敘述指令執行

程式指令:
% drawcircles.m
% Draw circles with radii of r.
x1=0;
y1=0;
r1=3; % 第一個圓心座標為(0,0),半徑長為3的圓
theta=linspace(0,2*pi,60);
xx1=x1+r1*cos(theta);
yy1=y1+r1*sin(theta);
axis equal;
plot(xx1,yy1)
hold on; % 讓兩個圓同時出現在一張圖裡
x2=10;
y2=0;
r2=5; % 第二個圓心座標為(10,0),半徑長為5的圓
theta=linspace(0,2*pi,60);
xx2=x2+r2*cos(theta);
yy2=y2+r2*sin(theta);
axis equal;
plot(xx2,yy2)


執行結果:



3. Using an implicit function(匿名函數)to find the result for the formula y=x²/a²+y²/b², in which a=3, b=5, and y=[12 15 20] for x=[3 4 5]. Draw the graphs.

ANS:
>> a=3;b=5;g=@(x,y) x.^2/a^2+y.^2/b^2;
>> g([3 4 5],[12 15 20])

ans =

6.7600 10.7778 18.7778

4. A plate ring has a thickness of 2 cm, with inner and outer diameters of 3 cm and 5 cm, respectively. Using the above parameters as inputs of a function to find the area of its upper face and side wall as well as the volume.

ANS:
程式指令:
function [area volume]=ring(Do,Di,height)
% Find the area and volume of a plate ring
% Do,Di:outside & inside diameters
area=abs(Do.^2-Di.^2)*pi;
volume=abs(Do.^2-Di.^2).*height*pi;

執行結果:
>> [area volume]=ring(5,3,2)

area =

50.2655


volume =

100.5310

5. Rewrite the function "nest_fun" in the text by moving the nested function polyx(xx) outside of the main function so that the function will still produce the same results.

ANS:
原有之程式指令為:
function [rr_array]=nest_fun(x,a)
%function to find sets of polynormials.
% a: set of constants, [A B C]
% x: variables in array
% Example: rr=nest_fun(2:10,[1 2 4;2 4 8])
n=size(a);
for i=1:n
A=a(i,1);B=a(i,2);C=a(i,3);
rr_array{1,i}=['A=',num2str(A),', B=',...
num2str(B),', C=',num2str(C)];
rr_array{2,i}=polyx(x);
end
function r=polyx(xx)
r=A.*x.^2 + B.*x +C;
end
end

執行結果:
>> nest_fun(2:10,[1 2 4;2 4 8])

ans =

'A=1, B=2, C=4' 'A=2, B=4, C=8'
[1x9 double] [1x9 double]

若要將polyx(xx)移至主函數程式之外,則需要對A、B、C變數加上全域性變數(global)之宣告:
function [rr_array]=nest_fun2(x,a)
%function to find sets of polynormials.
% a: set of constants, [A B C]
% x: variables in array
% Example: rr=nest_fun2(2:10,[1 2 4;2 4 8])
n=size(a);
for i=1:n
global A B C
A=a(i,1);B=a(i,2);C=a(i,3);
rr_array{1,i}=['A=',num2str(A),', B=',...
num2str(B),', C=',num2str(C)];
rr_array{2,i}=polyx(x);
end
end
function r=polyx(x)
global A B C
r=A.*x.^2 + B.*x +C;
end

執行結果:
>> nest_fun2(2:10,[1 2 4;2 4 8])

ans =

'A=1, B=2, C=4' 'A=2, B=4, C=8'
[1x9 double] [1x9 double]

與原程式之結果相同

6. rewrite the function "pillar" to make sure it will run smoothly even the inputs are not sufficient for the function to run.

ANS:
原有程式只適用於完全沒有輸入數值時仍可有結果輸出,
但若輸入數值為一個或兩個時,則執行原程式時會有error發生,
故須將程式改為:
function [volume]=pillar(Do,Di,height)
% Find the volume of a hollow pillar
% ro,ri:outside & inside diameters
if nargin<1,
height=1;
Di=0;
Do=1;
elseif nargin<2,
height=1;
Di=0;
elseif nargin<3,
height=1;
end
volume=abs(Do.^2-Di.^2).*height*pi/4;

執行結果:
>> pillar

ans =

0.7854

>> pillar(2)

ans =

3.1416

>> pillar(1,2)

ans =

2.3562

>> pillar(1,2,2)

ans =

4.7124

如此,不論輸入數值是否小於3個都可以順利執行程式

沒有留言: