2007年3月28日 星期三

作業四(b94202029物理二張哲輔)


請將以下之程式碼存成M-files後放至work資料夾下,並執行欲檢視的作業的敘述檔。

--------------------------------------------------------------------------------------
作業4-1(以下含敘述檔homework4-1的M-file程式碼):
--------------------------------------------------------------------------------------
% homework4-1

% myNO=29, length=myNO+10=39
% radius=(myNO+10)/(3^0.5)
axis equal
axis off
axis([-30 30 -30 30])

myNO=29
radius=(myNO+10)/(3^0.5)
vertex1=[0;radius]
vertex2=[cosd(120) sind(120);-sind(120) cosd(120)]*vertex1
vertex3=[cosd(-120) sind(-120);-sind(-120) cosd(-120)]*vertex1
vertices=[vertex1';vertex2';vertex3';vertex1']
x=vertices(:,1)
y=vertices(:,2)
triangle=line(x,y,'erasemode','xor');
drawnow;

deltaTheta=pi/180
rotationMatrix=[cos(deltaTheta) sin(deltaTheta); -sin(deltaTheta) cos(deltaTheta)]

while 1,

pause(0.01);
vertices=vertices';
vertices=rotationMatrix*vertices;
vertices=vertices';
x=vertices(:,1);
y=vertices(:,2);
set(triangle,'xdata',x,'ydata',y);
drawnow;

end;

--------------------------------------------------------------------------------------
作業4-2(以下含函數檔linkpoints及drawlink的M-file程式碼,及敘述檔homework4-2的M-file程式碼):
--------------------------------------------------------------------------------------
%linkpoints

% Input the positions of the two ends & width , output points of the link.
function y=linkpoints(A,B,d) % A=[x1 y1] B=[x2 y2]

L=((B(1,1)-A(1,1))^2+(B(1,2)-A(1,2))^2)^0.5
COStheta=(B(1,1)-A(1,1))/L
SINtheta=(B(1,2)-A(1,2))/L
rotationMatrix=[COStheta -SINtheta; SINtheta COStheta]

p0=linspace(0.5*pi,2.5*pi,12)
p1=linspace(0.5*pi,1.5*pi,6)
p2=linspace(1.5*pi,2.5*pi,6)
X=[(d/4)*cos(p0) 0 (d/2)*cos(p1) L L+(d/2)*cos(p2) L L+(d/4)*cos(p0) L 0 0 ]
Y=[(d/4)*sin(p0) (d/2) (d/2)*sin(p1) (-d/2) (d/2)*sin(p2) (d/4) (d/4)*sin(p0) (d/2) (d/2) (d/4)]
positions=[X;Y]
positions=rotationMatrix*positions
X=positions(1,:)+A(1,1)
Y=positions(2,:)+A(1,2)
y=[X;Y]

--------------------------------------------------------------------------------------
% drawlink

% Input the positions of the two ends & width , output a link figure.
function drawlink(A,B,d) % A=[x1 y1] B=[x2 y2]

L=((B(1,1)-A(1,1))^2+(B(1,2)-A(1,2))^2)^0.5

COStheta=(B(1,1)-A(1,1))/L
SINtheta=(B(1,2)-A(1,2))/L
rotationMatrix=[COStheta -SINtheta; SINtheta COStheta]

p0=linspace(0.5*pi,2.5*pi,12)
p1=linspace(0.5*pi,1.5*pi,6)
p2=linspace(1.5*pi,2.5*pi,6)
X=[(d/4)*cos(p0) 0 (d/2)*cos(p1) L L+(d/2)*cos(p2) L L+(d/4)*cos(p0) L 0 0 ]
Y=[(d/4)*sin(p0) (d/2) (d/2)*sin(p1) (-d/2) (d/2)*sin(p2) (d/4) (d/4)*sin(p0) (d/2) (d/2) (d/4)]
positions=[X;Y]
positions=rotationMatrix*positions
X=positions(1,:)+A(1,1)
Y=positions(2,:)+A(1,2)

X=X'
Y=Y'
axis equal
line(X,Y,'erasemode','xor')

--------------------------------------------------------------------------------------
% homework4-2

A=[0 0] % position of one end fixed at the origin
B=[10 0] % position of the other end connected with a spring
springEnd=[15 0] % the fixen end of the spring
d=4 % width

axis([-20 20 -12 12])
axis equal

rotationMatrix=[cosd(10) -sind(10); sind(10) cosd(10)]

y=linkpoints(A,B,d)
y=y'
X=y(:,1)
Y=y(:,2)
figure1=line(X,Y,'erasemode','xor')

M=[B(1,1);springEnd(1,1)]
N=[B(1,2);springEnd(1,2)]
figure2=line(M,N,'erasemode','xor')

drawnow;

while 1
pause(0.01)

B=B'
B=rotationMatrix*B
B=B'

y=linkpoints(A,B,d)
y=y'
X=y(:,1)
Y=y(:,2)
set(figure1,'xdata',X,'ydata',Y)

M=[B(1,1);springEnd(1,1)]
N=[B(1,2);springEnd(1,2)]
set(figure2,'xdata',M,'ydata',N)

drawnow;

end;

--------------------------------------------------------------------------------------
作業4-3(以下含敘述檔homework4-3的M-file程式碼):
--------------------------------------------------------------------------------------
%homework4-3

ptA=[0 0]
ptB=[3 4]
ptC=[13 4]
ptD=[10 0]

dAB=3
dBC=1.5
dCD=2
dAD=2

rotationMatrix=[cosd(30) -sind(30); sind(30) cosd(30)]

drawlink(ptA,ptD,dAD)

yAB=linkpoints(ptA,ptB,dAB)
yAB=yAB'
XAB=yAB(:,1)
YAB=yAB(:,2)
linkAB=line(XAB,YAB,'erasemode','xor')

yBC=linkpoints(ptB,ptC,dBC)
yBC=yBC'
XBC=yBC(:,1)
YBC=yBC(:,2)
linkBC=line(XBC,YBC,'erasemode','xor')

yCD=linkpoints(ptC,ptD,dCD)
yCD=yCD'
XCD=yCD(:,1)
YCD=yCD(:,2)
linkCD=line(XCD,YCD,'erasemode','xor')

axis([-10 20 -6 6])
drawnow;

while 1

pause(0.01)

ptB=ptB'
ptB=rotationMatrix*ptB
ptB=ptB'
ptC=[ptB(1,1)+10 ptB(1,2)]

yAB=linkpoints(ptA,ptB,dAB)
yAB=yAB'
XAB=yAB(:,1)
YAB=yAB(:,2)

yBC=linkpoints(ptB,ptC,dBC)
yBC=yBC'
XBC=yBC(:,1)
YBC=yBC(:,2)

yCD=linkpoints(ptC,ptD,dCD)
yCD=yCD'
XCD=yCD(:,1)
YCD=yCD(:,2)

set(linkAB,'xdata',XAB,'ydata',YAB)
set(linkBC,'xdata',XBC,'ydata',YBC)
set(linkCD,'xdata',XCD,'ydata',YCD)

drawnow;

end;


4 則留言:

不留白老人 提到...

沒有說明,沒有執行結果,不知道作業中到底做了什麼

nano 提到...

我只是來留言的

不留白老人 提到...

為什麼沒有任何執行結果?

張哲輔 提到...

抱歉之前沒有把結果執行影片放上,現在已放上,給老師添麻煩不好意思!