星期五, 12月 01, 2006

work9

r95631002 黃聖峰 作業9

What's difference between the commands "line" and "plot", explain by using your own examples.




(1)第一個不同,plot不能一次畫二個圖,因為它會覆蓋第一個圖,必須要用hold on來保持住第一個圖或是用plot(x,y,x,y1)也可以

(2)第二個不同,要是想更改line的顏色和型式,line不能直接在後面接'顏色的代碼'和'型式的代碼'(plot的代碼應用的比較多),要用屬性名稱才能加其代碼

注意一下,要是在程式碼改其'顏色'或'型式'要是一打錯其屬性名稱,則程式就會不會畫出圖.

最好是用set 指令來改變會比較好

程式碼
clear all
close all
clc

x=0:500;x1=500:1000;
y=0.25*exp(-0.05*x);
y1=0.5*exp(-0.05*x);

figure(1);line(x,y)
line(x1,y1,'color','r','linestyle','+') % 必須要用出屬性名稱才能加其代碼
xlabel('x軸')
ylabel('y軸')

figure(2)
hold on % 利用hold on
plot(x,y)
plot(x1,y1,'r*') % 直接用代碼
xlabel('x軸')
ylabel('y軸')
hold off

figure(3)
plot(x,y,x1,y1) % 用plot它每一條線條都會有不同的顏色

星期二, 11月 28, 2006

小程式 月曆產生器



clear all
close all
clc
a=menu('萬年曆','建月曆請按此','查某年某月某日為星期幾請按此');
if a==1
y=inputdlg({'請輸入你要建那一年的月曆'});
y=str2num(char(y));
m=menu('請選擇你要建那一月的月曆','1','2','3','4','5','6','7','8','9','10','11','12');
ans=[];
if rem(y,100)==0 & rem(y,400)==0 rem(y,4)==0
d1=[31 29 31 30 31 30 31 31 30 31 30 31];
else
d1=[31 28 31 30 31 30 31 31 30 31 30 31];
end
s=y-1;c=d1(1:(m-1));
for k=1:d1(m)
c1=sum(c)+k;
ss=s+fix(s/4)-fix(s/100)+fix(s/400)+c1;
ans(k)=rem(ss,7);
end
ans(ans==0)=7;
pp=ans(1);ans1=[];
k1=0;
for n=1:6;
for m1=pp:7
ans1(n,m1)=k1;
k1=k1+1;
end
pp=1;
end
ans1(ans1>d1(m))=0;
s=xlswrite('月曆.xls',{[y],' 年',[m],' 月'});
s=xlswrite('月曆.xls',{'星期日','星期一','星期二','星期三','星期四','星期五','星期六'},...
'a2:g2');
s=xlswrite('月曆.xls',ans1,'a3:g8');
as=sprintf(['你的月曆已經建好了\n\n','請到你的儲存路徑下打開[月曆.xls]這個檔\n\n']);
msgbox(as)
else
y=inputdlg({'請輸入你要查詢那一年'});
y=str2num(char(y));
m=inputdlg({'請輸入你要查詢那一月'});
m=str2num(char(m));
k=inputdlg({'請輸入你要查詢那一日'});
k=str2num(char(k));
ans=[];
if rem(y,100)==0 & rem(y,400)==0 rem(y,4)==0
d1=[31 29 31 30 31 30 31 31 30 31 30 31];
else
d1=[31 28 31 30 31 30 31 31 30 31 30 31];
end
s=y-1;
c=d1(1:(m-1));
c1=sum(c)+k;
ss=s+fix(s/4)-fix(s/100)+fix(s/400)+c1;
dd=rem(ss,7)+1;
di={'日','一','二','三','四','五','六'};
msgbox(['你查詢的',num2str(y),'年',num2str(m),'月',num2str(k),'日','是星期',di(dd)])
end



結果






















星期一, 11月 27, 2006

work8


第一題
Find the multiplication result of two polynorminals,
in which p=133x^5+122x^3+1, q=2x^4+100x^2+1

程式碼
p=[133 0 122 0 0 1];
q=[2 0 100 0 1];
ans1=conv(p,q)
結果
ans1 =
266 0 13544 0 12333 2 122 100 0 1
即為266x^9+13544x^7+12333x^5+2x^4+122x^3+100x^2+1

第二題
A polynorminal is defined as f=100x^3+23x^2+x+45
Find the value f(x) if x is a magic matrix in the order of 5

程式碼
f=[100 23 1 45];
x=magic(5);
ans2=polyvalm(f,x)

結果
ans2 =
5830132 5825724 5059176 5210878 5633875

5824573 5332275 5196252 5496574 5710111

5206104 5074326 5518893 5949340 5811122

5310105 5521572 5826264 5702291 5199553

5388871 5805888 5959200 5200702 5205124

第三題
Using p and q defined in the item 1, find the quotient and residue of p/q

程式碼
p=[133 122 1];
q=[2 100 1];
[s,r]=deconv(p,q)

結果
s =
66.5000 (商)
r =
1.0e+003 *
0 -6.5280 -0.0655 (餘)

第四題
Find the roots of p=0 and q=0, in which both p & q are defined in item 1

程式碼
p=[133 0 122 0 0 1]
q=[2 0 100 0 1]
p=roots(p)
q=roots(q)

結果
p =
-0.0045 + 0.9578i
-0.0045 - 0.9578i
0.1039 + 0.1744i
0.1039 - 0.1744i
-0.1988
q=
0 + 7.0704i
0 - 7.0704i
0 + 0.1000i
0 - 0.1000i

第五題
Fit a polynorminal curve to following data to an order of 3. x=[1:9];
y=[1210, 1866, 2301, 2564, 2724, 2881, 2879, 2915, 3010]

程式碼
x=[1:9]; %應該只有到9
y=[1210 1866 2301 2564 2724 2881 2879 2915 3010] ;
pans=polyfit(x,y,3)

結果
pans =
6.3047 -134.4603 994.3540 350.9127


星期五, 11月 17, 2006

work-7

<\pre>
第一題
Write a program to input the name, age,
sex and email address of a student and store the data as a structural format.
程式碼
clear all
close all
clc
n=1;
m=0;
while n;
m=n+m;
disp('這是一個建個人檔案的資料庫')
a=input('請輸入你的名字\n','s');
b=input('請輸入你的年齡\n');
c=input('請輸入你的性別\n','s');
d=input('請輸入你的電址郵件地址\n','s');
student(m)=struct('name',{a},'age',{b},'sex',{c},'mail',{d});

e=input('還要建下一筆嗎,請輸入n or y\n','s');
if e=='y';
continue
else
break
end
end

fprintf('你總共建了%d筆資料',m)
o=input('你想看你所建的資料嗎 y or n\n','s');
if o=='y'
for bb=1:m
fprintf('你所建的第%d筆資料',bb)
student(bb)
end
else
disp('bye')
end

% 結果 %
這是一個建個人檔案的資料庫
請輸入你的名字
jen
請輸入你的年齡
18
請輸入你的性別
man
請輸入你的電址郵件地址
jen@nt.com
還要建下一筆嗎,請輸入n or y
y
這是一個建個人檔案的資料庫
請輸入你的名字
王大毛
請輸入你的年齡
24
請輸入你的性別

請輸入你的電址郵件地址
mm@tu.com
還要建下一筆嗎,請輸入n or y
n
你總共建了2筆資料你想看你所建的資料嗎 y or n
y
你所建的第1筆資料
ans =

name: 'jen'
age: 18
sex: 'man'
mail: 'jen@nt.com'

你所建的第2筆資料
ans =

name: '王大毛'
age: 24
sex: '男'
mail: 'mm@tu.com'

第二題
Use the "menu" and "switch" commands incorporated in a program
that enables a selection of Apple, Microsoft, IBM, Acer and Asus computers.
% 程式碼 %
close all
clear all
clc
k=menu('請選擇你想看的品牌口號:','Apple','Microsoft','IBM','Acer','Asus');
switch k;
case 1
disp('Apple的口號 "不同凡想"(Think Different)')
case 2
disp('Microsoft沒有口號,地下版口號:本程式執行無效,請洽系統管理員')
case 3
disp('IBM的口號 "隨需而變"(A Business on Demand)')
case 4
disp('Acer的口號 "不斷創新,因為用心"')
case 5
disp('Asus的口號"華碩品質,堅若磐石"')
end
% 結果 %

Microsoft沒有口號,地下版口號:本程式執行無效,請洽系統管理員

第四題
Write a program to retrieve data from an excell sheet.

程式碼
clear all
close all
clc
[a,b,c]=xlsread('so.xls') % 讀取excell
結果
原始圖

a =
90 85 90
86 98 95
78 75 78
b =
'name' '國文' '英文' '數學'
'Jan' '' '' ''
'Peter' '' '' ''
'Eba' '' '' ''
c =
'name' '國文' '英文' '數學'
'Jan' [ 90] [ 85] [ 90]
'Peter' [ 86] [ 98] [ 95]
'Eba' [ 78] [ 75] [ 78]


% 發現有錯從command window直接修改 %
c1={'Jan',80,75,60;} % 改 jan的分數
c1 =
'Jan' [80] [75] [60]


status=xlswrite('so.xls',c1,'a2:d2')

status = 1 % 表存成功
修改後的圖

work6


第一題
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.
% 第一題程式碼 %
clc
clear all
close all
time=input('這是一個自由落体的公式,請輸入時間\n');
free(time); %呼叫free function%

% free function %

function y=free(time);
a=length(time);
for n=1:a;
y=1/2*980*time(n).*time(n);
fprintf('當第%0.1f秒時,落下%4.3f公尺\n',time(n),y/100);
end

% 第一題執行結果 %
這是一個自由落体的公式,請輸入時間
[0:0.2:30]
當第0.0秒時,落下0.000公尺 當第0.2秒時,落下0.196公尺 當第0.4秒時,落下0.784公尺
當第0.6秒時,落下1.764公尺 當第0.8秒時,落下3.136公尺 當第1.0秒時,落下4.900公尺
當第1.2秒時,落下7.056公尺 當第1.4秒時,落下9.604公尺 當第1.6秒時,落下12.544公尺
當第1.8秒時,落下15.876公尺 當第2.0秒時,落下19.600公尺 當第2.2秒時,落下23.716公尺
當第2.4秒時,落下28.224公尺 當第2.6秒時,落下33.124公尺 當第2.8秒時,落下38.416公尺
當第3.0秒時,落下44.100公尺 當第3.2秒時,落下50.176公尺 當第3.4秒時,落下56.644公尺
當第3.6秒時,落下63.504公尺 當第3.8秒時,落下70.756公尺 當第4.0秒時,落下78.400公尺
當第4.2秒時,落下86.436公尺 當第4.4秒時,落下94.864公尺 當第4.6秒時,落下103.684公尺
當第4.8秒時,落下112.896公尺 當第5.0秒時,落下122.500公尺 當第5.2秒時,落下132.496公尺
當第5.4秒時,落下142.884公尺 當第5.6秒時,落下153.664公尺 當第5.8秒時,落下164.836公尺
當第6.0秒時,落下176.400公尺 當第6.2秒時,落下188.356公尺 當第6.4秒時,落下200.704公尺
當第6.6秒時,落下213.444公尺 當第6.8秒時,落下226.576公尺 當第7.0秒時,落下240.100公尺
當第7.2秒時,落下254.016公尺 當第7.4秒時,落下268.324公尺 當第7.6秒時,落下283.024公尺
當第7.8秒時,落下298.116公尺 當第8.0秒時,落下313.600公尺 當第8.2秒時,落下329.476公尺
當第8.4秒時,落下345.744公尺 當第8.6秒時,落下362.404公尺 當第8.8秒時,落下379.456公尺
當第9.0秒時,落下396.900公尺 當第9.2秒時,落下414.736公尺 當第9.4秒時,落下432.964公尺
當第9.6秒時,落下451.584公尺 當第9.8秒時,落下470.596公尺 當第10.0秒時,落下490.000公尺
當第10.2秒時,落下509.796公尺 當第10.4秒時,落下529.984公尺 當第10.6秒時,落下550.564公尺
當第10.8秒時,落下571.536公尺 當第11.0秒時,落下592.900公尺 當第11.2秒時,落下614.656公尺
當第11.4秒時,落下636.804公尺 當第11.6秒時,落下659.344公尺 當第11.8秒時,落下682.276公尺
當第12.0秒時,落下705.600公尺 當第12.2秒時,落下729.316公尺 當第12.4秒時,落下753.424公尺
當第12.6秒時,落下777.924公尺 當第12.8秒時,落下802.816公尺 當第13.0秒時,落下828.100公尺
當第13.2秒時,落下853.776公尺 當第13.4秒時,落下879.844公尺 當第13.6秒時,落下906.304公尺
當第13.8秒時,落下933.156公尺 當第14.0秒時,落下960.400公尺 當第14.2秒時,落下988.036公尺
當第14.4秒時,落下1016.064公尺 當第14.6秒時,落下1044.484公尺 當第14.8秒時,落下1073.296公尺
當第15.0秒時,落下1102.500公尺 當第15.2秒時,落下1132.096公尺 當第15.4秒時,落下1162.084公尺
當第15.6秒時,落下1192.464公尺 當第15.8秒時,落下1223.236公尺 當第16.0秒時,落下1254.400公尺
當第16.2秒時,落下1285.956公尺 當第16.4秒時,落下1317.904公尺 當第16.6秒時,落下1350.244公尺
當第16.8秒時,落下1382.976公尺 當第17.0秒時,落下1416.100公尺 當第17.2秒時,落下1449.616公尺
當第17.4秒時,落下1483.524公尺 當第17.6秒時,落下1517.824公尺 當第17.8秒時,落下1552.516公尺
當第18.0秒時,落下1587.600公尺 當第18.2秒時,落下1623.076公尺 當第18.4秒時,落下1658.944公尺
當第18.6秒時,落下1695.204公尺 當第18.8秒時,落下1731.856公尺 當第19.0秒時,落下1768.900公尺
當第19.2秒時,落下1806.336公尺 當第19.4秒時,落下1844.164公尺 當第19.6秒時,落下1882.384公尺
當第19.8秒時,落下1920.996公尺 當第20.0秒時,落下1960.000公尺 當第20.2秒時,落下1999.396公尺
當第20.4秒時,落下2039.184公尺 當第20.6秒時,落下2079.364公尺 當第20.8秒時,落下2119.936公尺
當第21.0秒時,落下2160.900公尺 當第21.2秒時,落下2202.256公尺 當第21.4秒時,落下2244.004公尺
當第21.6秒時,落下2286.144公尺 當第21.8秒時,落下2328.676公尺 當第22.0秒時,落下2371.600公尺
當第22.2秒時,落下2414.916公尺 當第22.4秒時,落下2458.624公尺 當第22.6秒時,落下2502.724公尺
當第22.8秒時,落下2547.216公尺 當第23.0秒時,落下2592.100公尺 當第23.2秒時,落下2637.376公尺
當第23.4秒時,落下2683.044公尺 當第23.6秒時,落下2729.104公尺 當第23.8秒時,落下2775.556公尺
當第24.0秒時,落下2822.400公尺 當第24.2秒時,落下2869.636公尺 當第24.4秒時,落下2917.264公尺
當第24.6秒時,落下2965.284公尺 當第24.8秒時,落下3013.696公尺 當第25.0秒時,落下3062.500公尺
當第25.2秒時,落下3111.696公尺 當第25.4秒時,落下3161.284公尺 當第25.6秒時,落下3211.264公尺
當第25.8秒時,落下3261.636公尺 當第26.0秒時,落下3312.400公尺 當第26.2秒時,落下3363.556公尺
當第26.4秒時,落下3415.104公尺 當第26.6秒時,落下3467.044公尺 當第26.8秒時,落下3519.376公尺
當第27.0秒時,落下3572.100公尺 當第27.2秒時,落下3625.216公尺 當第27.4秒時,落下3678.724公尺
當第27.6秒時,落下3732.624公尺 當第27.8秒時,落下3786.916公尺 當第28.0秒時,落下3841.600公尺
當第28.2秒時,落下3896.676公尺 當第28.4秒時,落下3952.144公尺 當第28.6秒時,落下4008.004公尺
當第28.8秒時,落下4064.256公尺 當第29.0秒時,落下4120.900公尺 當第29.2秒時,落下4177.936公尺
當第29.4秒時,落下4235.364公尺 當第29.6秒時,落下4293.184公尺 當第29.8秒時,落下4351.396公尺
當第30.0秒時,落下4410.000公尺
>>
第二題
Draw two circles with radii of 3 and 5 cm each and their centers located at (0,0)and(10,0)cm
% 第二題程式碼 %
function drawcircles(r) % 主函數
global I
n=length(r)
hold on;
for I=1:n
[X,Y]=circ(r(I));
plot(X,Y);
end
hold off;
axis equal;
end
function [x,y]=circ(rr)%次函數
% Calculate the points of a circle.
[xx,yy]=randxy;
theta=linspace(0,2*pi,60);
x=xx+rr*cos(theta);
y=yy+rr*sin(theta);
end
function [xx,yy]=randxy%次函數
global I
A=[0 10]
B=[0 0]
xx=A(I)
yy=B(I)
end
% 第二題執行結果 %


第三題
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.

% 第三題程式碼 %

a=3;b=5;y=[12 15 20];fplot(@(x) ((x.^2)./a^2+(y.^2)./b^2),[3 5])

% 第三題結果 %


第四題
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.

% 第四題程式碼 %
clc
clear all
close all
D=input('這是求圓柱體積和表面積的程式,請輸入外徑\n');
d=input('這是求圓柱體積和表面積的程式,請輸入內徑\n');
h=input('這是求圓柱體積和表面積的程式,請輸入圓柱高\n');
cr(D,d,h); % 呼叫function cr %

% function cr %
function [v,s]=cr(D,d,h);
v=(D/2-d/2)^2*pi*h %體積
s=v*2+(D/2-d/2)^2*pi*2 %表面積

% 第四題結果 %

這是求圓柱體積和表面積的程式,請輸入外徑
5
這是求圓柱體積和表面積的程式,請輸入內徑
3
這是求圓柱體積和表面積的程式,請輸入圓柱高
2
v =
6.2832
s =
18.8496

第五題
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.

% 第五題程式碼 %
function [rr_array]=nest_fun1(x,a)% 主程式
n=size(a);
global A;global B;global C;;
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
end % 主程式結束

function r=polyx(xx) % 次程式...把它移出主程式了
global A;global B;global C;;
r=A.*xx.^2+B.*xx+C;
end % 次程式結束

% 第五題結果 %

rr=nest_fun1(2:10,[1 2 4;2 4 8]) %在command window 下執行

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


celldisp(rr) %在command window下執行

rr{1,1} =
A=1, B=2, C=4

rr{2,1} =
12 19 28 39 52 67 84 103 124

rr{1,2} =
A=2, B=4, C=8

rr{2,2} =
24 38 56 78 104 134 168 206 248

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

% 第六題程式碼 %
function [volume]=pillar(Do,Di,height)
if nargin<1 % 當你什麼都沒輸入時會執行這個
height=1;
Di=0;
Do=1;
end
if nargin<2 % 當你只輸入一個值時會執行這個
Di=0;
height=1;
end
if nargin<3 % 當你只輸入二個值時會執行這個
height=1;
end
volume=abs(Do.^2-Di.^2).*height*pi/4;

星期二, 11月 14, 2006

I dare you

Hello, let me introduce you to
The characters in the show
One says yes, one says no
Decide - which voice in your head you can keep alive

Even in madness, I know you still believe
Paint me your canvas so I become
What you could never be

I dare you to tell me to walk through the fire
wear my soul and call me a liar
I dare you to tell me to walk through the fire
I dare you to tell me
I dare you to

Hello, are you still chasing
The memories in shadows
Some stay young, some grow old
Come alive, there are thoughts unclear
You can never hide

Even in madness, I know you still believe
Paint me your canvas so I become
What you could never be

I dare you to tell me to walk through the fire
wear my soul and call me a liar
I dare you to tell me to walk through the fire
I dare you to tell me
I dare you to

Hello
Hello...
I dare you to tell me to walk through the fire
Brand my soul and call me a liar
I dare you to tell me to walk through the fire
I dare you to tell me
I dare you to
Hello...
Hello...
I dare you to tell me
I dare you to
I dare you to tell me
I dare you to

Friday Night Lights

Be My Escape


"Be My Escape"

I’ve given up on giving up slowly, I’m blending in so
You won’t even know me apart from this whole world that shares my fate
This one last bullet you mention is my one last shot at redemption
because I know to live you must give your life away
And I’ve been housing all this doubt and insecurity and
I’ve been locked inside that house all the while You hold the key
And I’ve been dying to get out and that might be the death of me
And even though, there’s no way in knowing where to go, promise I’m going because
I gotta get outta here
I’m stuck inside this rut that I fell into by mistake
I gotta get outta here
And I’m begging You, I’m begging You, I’m begging You to be my escape.

I’m giving up on doing this alone now
Cause I’ve failed and I’m ready to be shown how
He’s told me the way and I’m trying to get there
And this life sentence that I’m serving
I admit that I’m every bit deserving
But the beauty of grace is that it makes life not fair

Cause I’ve been housing all this doubt and insecurity and
I’ve been locked inside that house all the while You hold the key
And I’ve been dying to get out and that might be the death of me
And even though, there’s no way in knowing where to go, promise I’m going because
I gotta get outta here
Cause I’m afraid that this complacency is something I can’t shake
I gotta get outta here
And I’m begging You, I’m begging You, I’m begging You to be my escape.

I am a hostage to my own humanity
Self detained and forced to live in this mess I’ve made
And all I’m asking is for You to do what You can with me
But I can’t ask You to give what You already gave

Cause I’ve been housing all this doubt and insecurity and
I’ve been locked inside that house all the while you hold the key
And I’ve been dying to get out and that might be the death of me
And even though, there’s no way in knowing where to go, promise I’m going because
I’ve gotta get outta here
I’m stuck inside this rut that I fell into by mistake
I’ve gotta get outta here
And I’m begging You, I’m begging You, I’m begging
You to be my escape.

I fought You for so long
I should have let You in
Oh how we regret those things we do
And all I was trying to do was save my own skin
But so were You

So were You

work-5

第一題Create a 3X3X2 matrix A which contains a matrix of magic(3)
and another rand(3,3)*10.

程式碼
a=cat(3,magic(3),rand(3,3)*10)
結果
a(:,:,1) =
8 1 6
3 5 7
4 9 2

a(:,:,2) =
8.4622 6.7214 6.8128
5.2515 8.3812 3.7948
2.0265 0.1964 8.3180


第二題 Assume that matrices A=[45 89 99; 12 34 55], B=[15 25 45; 65 50 30].
Find matrices that join A & B both in vertical and horizontal directions.
Also, find a two-page matrix with A & B stored in separate pages.
Use commands horizcat and verticat to check the results with those from above


程式碼
(1)

A=[45 89 99;12 34 55];
B=[15 25 45;65 50 30];

disp('用[A;B]組合成vertical')
v=[A;B] % 組合成vertical
disp('用cat(1,A,B)組成的vertica')
V=cat(1,A,B) % 利用cat funcation 組成的vertical

disp('證明')
v==V % 證明是否一樣

結果
(1)
用[A;B]組合成vertical
v =
45 89 99
12 34 55
15 25 45
65 50 30
用cat(1,A,B)組成的vertica
V =
45 89 99
12 34 55
15 25 45
65 50 30
證明
ans =
1 1 1
1 1 1
1 1 1
1 1 1
所以是用這二種方式結果是一樣的

程式碼
(2)

disp('用[A B]組合成horizontal')
h=[A B] % 組合成horizontal
disp('cat(2,A,B)')
H=cat(2,A,B) % 利用cat funcation 組成的horizontal

disp('證明')
H==h % 證明是否一樣
結果(2)
用[A B]組合成horizontal
h =
45 89 99 15 25 45
12 34 55 65 50 30

cat(2,A,B)
H =
45 89 99 15 25 45
12 34 55 65 50 30

證明
ans =
1 1 1 1 1 1
1 1 1 1 1 1
所以是用這二種方式結果是一樣的

程式碼
(3)
disp('p(:,:,1)=A')
p(:,:,1)=A; % 組合成二頁,第一頁
disp('p(:,:,2)=B')
p(:,:,2)=B; % 組合成二頁,第二頁
p
disp('cat(3,A,B)')
P=cat(3,A,B) % 利用cat funcation 組成頁

disp('證明')
p==P% 證明是否一樣

結果(3)
p(:,:,1)=A
p(:,:,2)=B

p(:,:,1) =
45 89 99
12 34 55

p(:,:,2) =
15 25 45
65 50 30

cat(3,A,B)
P(:,:,1) =
45 89 99
12 34 55

P(:,:,2) =
15 25 45
65 50 30

證明
ans(:,:,1) =
1 1 1
1 1 1
ans(:,:,2) =
1 1 1
1 1 1
所以是用這二種方式結果是一樣的

第三題Find a matrix M which contains A as the first and third pages
and B the second and fourth
pages
程式碼
close all
clear all
clc
A=[45 89 99;12 34 55];
B=[15 25 45;65 50 30];
M(:,:,1)=A;% 第一頁
M(:,:,3)=A;% 第三頁
M(:,:,2)=B;% 第二頁
M(:,:,4)=B;% 第四頁
M
cat(3,A,B,A,B)%用cat組成四頁
結果
M(:,:,1) =
45 89 99
12 34 55
M(:,:,2) =
15 25 45
65 50 30
M(:,:,3) =
45 89 99
12 34 55
M(:,:,4) =
15 25 45
65 50 30

ans(:,:,1) =
45 89 99
12 34 55
ans(:,:,2) =
15 25 45
65 50 30
ans(:,:,3) =
45 89 99
12 34 55
ans(:,:,4) =
15 25 45
65 50 30
由止可知上面二種方法都可以
>>


第四題Construct a 2X2 cell array
that contains 'Eric' [90 100]; 'Peter' [35 60]; 'Jan' [77 67]

程式碼
A(1,1)={'name'};%自己加的因為是2X2所以把空格給填滿
A(1,2)={'score'};%自己加的
A(2,1)={{'Eric';'Jan';'Peter' }};
A(2,2)={[90 100;35 60;77 67]};
celldisp(A)
cellplot(A)

結果
A{1,1} =
name
A{2,1}{1} =
Eric
A{2,1}{2} =
Jan
A{2,1}{3} =
Peter
A{1,2} =
score
A{2,2} =
90 100
35 60
77 67


第五題Construct a structural Array that contains the following data:
Name: 'Philip', 'Peter','Roberts', 'Roe'
Age: 35, 45, 55, 60
Salary: 50,000 40,000 80,000 120,000


程式碼
DATA=struct('Name',{'philip','Peter','Roberts','Roe'},'Age',{35,45,55,60},'Salary',{50000,40000,80000,120000})
for N=1:4 %show 出內容
DATA(N)
end
結果
DATA =
1x4 struct array with fields:
Name
Age
Salary
ans =
Name: 'philip'
Age: 35
Salary: 50000
ans =
Name: 'Peter'
Age: 45
Salary: 40000
ans =
Name: 'Roberts'
Age: 55
Salary: 80000
ans =
Name: 'Roe'
Age: 60
Salary: 120000

星期五, 11月 10, 2006

Explosions In The Sky - Six Days At The Bottom Of The Ocean

應該是Friday_night_lights的片頭曲

Explosions in the Sky-First Breath After Coma

無意間聽到~覺得好聽就把它連過來了

Friday_Night_ Lights

Hope comes alive on Friday nights
when there is hope, there is life elsewhere

Friday_night_lights

最近在看的熱血影集~就想到在嘉義求學期間和學長,同學一起瘋壘球的日子呵


星期三, 11月 08, 2006

work4


第一題
有一向量A=3i +5j +10k和向量B=5i -6j +2k 都通過原點,請
找出兩者相交的單位向量

% 第一題程式碼 %
close all
clear all
clc
a1=[3 5 10];
b1=[5 -6 2];
c1=cross(a1,b1) % 在這利用cross即可求出

第一題結果

c1 =
70 44 -43
二者相交的向量為70i+44j-43k


第二題
利用A=randn(10,10)求出mean(A),median(A,2),std(A)
std(A,1,2)

% 第二題程式碼 %

a=randn(10,10);

disp('平均值')
m=mean(a)% 求平均值(在此是指每一行的平均)
mean(a(:))% 對整個矩陣做平均

disp('各列之中間值')
me=median(a,2)% 求各列之中間值(在此2是指每一列中間值,要是把2給去掉則是對每行運算)

disp('分母為n-1的標準差')
s=std(a)% 對行向量做標準差
s1=std(a(:))% 對整個矩陣做標準差

disp('分母為n的標準差,並對列向量來運算')
s1=std(a,1,2) % 在這1指分母為n的標準差,2指對列向量做標準差

第二題結果

平均值
m =(每一行為平均值)
-0.1643 0.3951 -0.6530 0.0288 0.0838 -0.1272 -0.2105 -0.7598 0.6833 -0.2402

ans =(整個矩陣的平均值)
-0.0964


各列之中間值
me =
-0.4912
-0.7159
-0.2354
0.2010
0.4158
0.3165
0.2103
-0.2451
-0.8788
-0.2415


分母為n-1的標準差
s =(對行向量做標準差)
0.9348 0.9574 0.8332 0.7537 1.3951 0.8999 1.0765 0.5953 0.7629 1.1109

s1 =(對整個矩陣做標準差)
1.0006


分母為n的標準差,並對列向量來運算

s1 =
0.6429
0.8450
1.0733
0.9973
0.6032
0.9794
1.2223
0.9861
0.7078
0.8956


第三題
Prove that exp(i*theta)=cos(theta)+i*sin(theta) using matlab commands
此題我利用判斷式來判斷此公式是否相等,當結果為1為相等,0為不相等

% 第三題程式碼 %
theta=[10 20 30 40 50 60]/180*pi;%把角度變弧度,在此我用10~60度下去判斷
x=((exp(i.*theta))==(cos(theta)+i.*sin(theta)))

第三題結果
x =
1 1 1 1 1 1
所以可證明此式是相等的


第四題
建立一個R=eye(3)*5+4*ones(3)i求其abs(R), angle(R), real(R) and imag(R)

% 第四題程式碼 %

R=eye(3)*5+(4*ones(3))*i % 為一向量所組矩陣
ab=abs(R)% 求?對值
an=angle(R).*180./pi% 求其夾角
re=real(R)% 求向量之實數部份
im=imag(R)% 求向量之虛數部份

第四題結果

R =

5.0000 + 4.0000i 0 + 4.0000i 0 + 4.0000i
0 + 4.0000i 5.0000 + 4.0000i 0 + 4.0000i
0 + 4.0000i 0 + 4.0000i 5.0000 + 4.0000i


ab =(?對值)

6.4031 4.0000 4.0000
4.0000 6.4031 4.0000
4.0000 4.0000 6.4031


an =(其夾角)

38.6598 90.0000 90.0000
90.0000 38.6598 90.0000
90.0000 90.0000 38.6598


re =(其實數)

5 0 0
0 5 0
0 0 5


im =(其虛數)

4 4 4
4 4 4
4 4 4

星期五, 11月 03, 2006

Work_3-1


% 第一題程式碼 %

b=(reshape((1:50),5,10)).^2 % 可以直接把1:50放入,再直接開平方

% 第一題結果 %

b =

1 36 121 256 441 676 961 1296 1681 2116
4 49 144 289 484 729 1024 1369 1764 2209
9 64 169 324 529 784 1089 1444 1849 2304
16 81 196 361 576 841 1156 1521 1936 2401
25 100 225 400 625 900 1225 1600 2025 2500


% 第二題程式碼 %

a1=magic(5); % 把大於10的變NaN
a1(a1>10)=NaN


% 第三題程式碼 %

a1=magic(5); % 把大於10的變0
a1(a1>10)=0


% 第二題和第三題結果 %

a1 =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

a1 =
NaN NaN 1 8 NaN
NaN 5 7 NaN NaN
4 6 NaN NaN NaN
10 NaN NaN NaN 3
NaN NaN NaN 2 9

a1 =
0 0 1 8 0
0 5 7 0 0
4 6 0 0 0
10 0 0 0 3
0 0 0 2 9



%第四題程式碼%

close all
clc
clear
x=input('請輸入你要抽的次數');
draw_number(x);

% function draw_number %

function [C]=draw_number(no_of_draw) % 因為要比較所以我用FOR把資料帶入
no_of_draw
o=length(no_of_draw);
% draw ball numbers within ndraw times
for m=1:o;
C=zeros(1,5);
n=1;
while n<=no_of_draw(m)
ball=fix(rand*10);
if ball<2
C(1)=C(1)+1;
elseif ball<4,
C(2)=C(2)+1;
elseif ball<6,
C(3)=C(3)+1;
elseif ball<8,
C(4)=C(4)+1;
else
C(5)=C(5)+1;
end
n=n+1;
end
fprintf('抽%d次的結果為\n',n-1)
disp('第一類 第二類 第三類 第四類 第五類')
C
end


% 第四題結果 %

請輸入你要抽的次數
[100:100:1000]

no_of_draw =
100 200 300 400 500 600 700 800 900 1000

抽100次的結果為
第一類 第二類 第三類 第四類 第五類
C =
25 16 20 15 24

抽200次的結果為
第一類 第二類 第三類 第四類 第五類
C =
38 41 42 42 37

抽300次的結果為
第一類 第二類 第三類 第四類 第五類
C =
55 62 51 59 73

抽400次的結果為
第一類 第二類 第三類 第四類 第五類
C =
77 76 97 76 74

抽500次的結果為
第一類 第二類 第三類 第四類 第五類
C =
95 102 110 106 87

抽600次的結果為
第一類 第二類 第三類 第四類 第五類
C =
117 120 120 118 125

抽700次的結果為
第一類 第二類 第三類 第四類 第五類
C =
145 162 122 144 127

抽800次的結果為
第一類 第二類 第三類 第四類 第五類
C =
157 151 172 169 151

抽900次的結果為
第一類 第二類 第三類 第四類 第五類
C =
176 191 187 161 185

抽1000次的結果為
第一類 第二類 第三類 第四類 第五類
C =
192 210 189 231 178


%第五題程式碼%

clear all
close all
clc
s=input('請輸入妳要的二位數號碼\n');
n1=1;
while n1<=20
m=0;
while 1
a1=fix(rand*100);%抽號碼
m=m+1;
if a1==s
fprintf('\n電腦抽出的結果%d\n',a1)
fprintf('電腦抽了第%d次才把你輸入的%d給抽出來',m,s)
% 花了多少次才把妳輸入的號碼給抽出來%
break
end
end
n1=n1+1;
fprintf('\n總共有20個循環,這是你第%d個循環\n\n',n1-1)%抽第幾次
end


% 第五題結果 %
請輸入妳要的二位數號碼
14

電腦抽出的結果14
電腦抽了第133次才把你輸入的14給抽出來
總共有20個循環,這是你第1個循環

電腦抽出的結果14
電腦抽了第506次才把你輸入的14給抽出來
總共有20個循環,這是你第2個循環

電腦抽出的結果14
電腦抽了第90次才把你輸入的14給抽出來
總共有20個循環,這是你第3個循環

電腦抽出的結果14
電腦抽了第22次才把你輸入的14給抽出來
總共有20個循環,這是你第4個循環

電腦抽出的結果14
電腦抽了第175次才把你輸入的14給抽出來
總共有20個循環,這是你第5個循環

電腦抽出的結果14
電腦抽了第44次才把你輸入的14給抽出來
總共有20個循環,這是你第6個循環

電腦抽出的結果14
電腦抽了第61次才把你輸入的14給抽出來
總共有20個循環,這是你第7個循環

電腦抽出的結果14
電腦抽了第33次才把你輸入的14給抽出來
總共有20個循環,這是你第8個循環

電腦抽出的結果14
電腦抽了第61次才把你輸入的14給抽出來
總共有20個循環,這是你第9個循環

電腦抽出的結果14
電腦抽了第38次才把你輸入的14給抽出來
總共有20個循環,這是你第10個循環

電腦抽出的結果14
電腦抽了第77次才把你輸入的14給抽出來
總共有20個循環,這是你第11個循環

電腦抽出的結果14
電腦抽了第1次才把你輸入的14給抽出來
總共有20個循環,這是你第12個循環

電腦抽出的結果14
電腦抽了第13次才把你輸入的14給抽出來
總共有20個循環,這是你第13個循環

電腦抽出的結果14
電腦抽了第48次才把你輸入的14給抽出來
總共有20個循環,這是你第14個循環

電腦抽出的結果14
電腦抽了第74次才把你輸入的14給抽出來
總共有20個循環,這是你第15個循環

電腦抽出的結果14
電腦抽了第11次才把你輸入的14給抽出來
總共有20個循環,這是你第16個循環

電腦抽出的結果14
電腦抽了第22次才把你輸入的14給抽出來
總共有20個循環,這是你第17個循環

電腦抽出的結果14
電腦抽了第84次才把你輸入的14給抽出來
總共有20個循環,這是你第18個循環

電腦抽出的結果14
電腦抽了第263次才把你輸入的14給抽出來
總共有20個循環,這是你第19個循環

電腦抽出的結果14
電腦抽了第153次才把你輸入的14給抽出來
總共有20個循環,這是你第20個循環

花蓮的包心粉圓



我只能說好吃啊~在花蓮三天~每天都去報到...太 Q了有機會再去花蓮一定要再去吃

在花蓮還看到清心~南部最好喝的飲料之一
也是我包心粉圓的另一種吃法

突然想到一件事~去花蓮吃包心粉時還遇到拍蠻牛廣告的男主角
他那時還帶著他(他住花蓮)的兒女去吃,好像是去練國術回來...本來想和他合照
最後想說不要 打擾人家的私生活~最後就沒拍了...
呵~好玩的花蓮~有空會陸續把去花蓮的照片給PO上來

星期四, 10月 26, 2006

存錢問題二

若採用定存的方式,每個月存放一定的數額(例如每個月五百元),銀行年息5%

close all
clear all
clc
n1=0;
sum=0;
while sum<10000000 sum="sum+sum*(5/100/12);" sum="sum+500;" n1="n1+1;" style="color: rgb(51, 204, 0);">Ans:每月存五佰元存到10,000,000要花89年

星期五, 10月 20, 2006

淡水~

上個星期去淡水,感覺還真是不一樣啊~^__^...以前都搭 metro...

第一次在台北騎車騎那麼遠...還經過關渡大橋...呵別問我為什麼經過...因為是要回去時騎錯車道
呵~~不過路上車還真多啊

這次去淡水,多了些沒看過的店(水煮魷魚和 "魚吻"仔魚),也覺得冰的味道變淡了...只有大吉祥的味道都沒變呵~~真是難以忘懷在淡水散步和吃塩酥菇的情景!!

problem-存錢

有一個很好玩的數字遊戯是:假設第一天到銀行存一分錢,第二天存二分錢,第三天三分錢,如此累積,問何時可以累積到10,000,000元?(設銀行的年息為5%)
  1. 撰寫一程式,試算上述之過程,並將其每達1000元倍數之日數同時印出,直到達到所期望之金額。
  2. 若每天所存改為五分錢、十分錢、十五分錢,。。。,試比較達到期望金額之日數。
  3. 實際生活上能否執行此項累積方式,試說明你的想法。


%第一小題程式碼%
close all
clear
clc
n=0;
sum=0;

d1=1;
while sum<100000000 n="n+1;%每一天增加一元" sum="sum+sum*(5/100/365);%每天的利息" sum="sum+n;%每天的本金" style="color: rgb(0, 153, 0);">Ans:每天增加一元存到10,000,000要花10726天


%第二小題程式碼%
close all
clear
clc
n=0;
sum=0;

d1=1;
while sum<100000000
n=n+5;%每一天增加五元
sum=sum+sum*(5/100/365);%每天的利息
sum=sum+n;%每天的本金
end
fprintf('每天增加五元存到10,000,000要花%d天\n',n/5)

Ans:每天增加五元存到10,000,000要花5529天


花蓮的海豚

花蓮的海豚