-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvec3demo.m
303 lines (283 loc) · 10.3 KB
/
vec3demo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
function vec3demo(X,Y) %7/14/02; last updated 7/3/03
%VEC3DEMO Display a pair of real three dimensional vectors, their
% sum, diffence and scalar multiples.
%
% The input vectors u and v are displayed in a 3-dimensional
% perspective along with their sum, differnce and selected
% scalar multiples. For visualization purposes a set of
% coordinate 3-D axes are shown.
%
% Use in the form: ==> vec3demo(u,v) <==
% or in the form: ==> vec3demo <== to choose a demo
% or be prompted
% for input
%
% By: David R. Hill, MATH Department, Temple University
% Philadelphia, Pa., 19122 Email: [email protected]
name=[' Original vectors u and v ';
' SUM: u + v ';
' DIFFERENCE: u - v ';
'Scalar Multiple: (-2u) & (1/2)v '];
head=' Vector Demonstration in 3-Space';
s0=' ';
s1='Enter vector u ==> ';
s2='Enter vector v ==> ';
s3='There are four graphs generated in this routine.';
s4=[' << OPTIONS >> ';
' ';
'1. Show all graphs on the same screen.';
' ';
'2. Show graphs on individual screens. ';
' ';
'0. QUIT! '];
s5=' Enter your choice ==> ';
s6='Routine VEC3DEMO is over!';
s7='Invalid choice; select 0, 1, or 2. TRY AGAIN!';
s8='Press ENTER to continue.';
s9=[' << OPTIONS >> ';
' ';
'1. Use a built in demo with vectors ';
' u = [8 2 9] and v = [5 -7 -15] ';
' ';
'2.Enter your pair of vectors u and v. ';
' ';
'0. QUIT! '];
warnmess=['WARNING: In some cases you may need to rotate';
'the coordinate system in order to see the ';
'the vectors in their proper relationship. '];
rotmess=['Select the figure number you want to view from ';
'the list at the bottom; click its number. Once it ';
'is visible proceed as follows. ';
' ';
'To rotate the coordinate system click on Tools, ';
'then click on Rotate 3D. Next click in the figure ';
'and drag the mouse to reorient the system. '];
er='Error: vectors must have three entries. Routine terminated.';
%INPUT routine
if nargin<2
goodch='N';
while goodch=='N'
clc,disp(head),disp(s0),disp(s0)
disp(s9),disp(s0),ch=input(s5);
if ch==1
X=[8 2 9];Y=[5 -7 -15];goodch='Y';
end
if ch==2
X=input(s1);disp(s0),disp(s0)
Y=input(s2);disp(s0),disp(s0)
goodch='Y';
end
if ch==0,clc,disp(s6),return,end
if ch~=1 & ch~=2
disp(s7),disp(s0),disp(s8),pause
end
end
end
[xm xn]=size(X);[ym yn]=size(Y);
if xm*xn~=3 | ym*yn~=3
disp(er)
return
end
if isreal(X)==0 | isreal(Y)==0 | sum(isnan(X))~=0 |sum(isnan(Y))~=0 | sum(isinf(X))~=0 | sum(isinf(Y))~=0
disp('ERROR: vectors must have all real entries. Routine terminated.')
return
end
X=reshape(X,1,3);Y=reshape(Y,1,3);
validch='N';
while(validch=='N')
clc,disp(head),disp(s0),disp(s0)
disp(s3),disp(s0)
disp(s4),disp(s0)
ch=input(s5);
if ch==0,clc,disp(s6),return,end
if ch==1 | ch==2
validch='Y';
else
disp(s7),disp(s0),disp(s8),pause
end
end
%set window
%GRAPHICS WINDOW
figure('units','normal','position',[0 0 1 .95],'color','white');
%START graphs
V=[X;Y;X+Y;X-Y;-2*X;.5*Y];
T1=min(V(:,1))-1;T2=max(V(:,1))+1;
T3=min(V(:,2))-1;T4=max(V(:,2))+1;
T5=min(V(:,3))-1;T6=max(V(:,3))+1;
T=[T1 T2 T3 T4 T5 T6]; %determining graphics box
%plt=[221 222 223 224];
if ch==1
%figure
sub1=subplot('position',[.1 .6 .25 .25]);
sub2=subplot('position',[.6 .6 .25 .25]);
sub3=subplot('position',[.1 .25 .25 .25]);
sub4=subplot('position',[.6 .25 .25 .25]);
for j=1:4
if j==1,subplot(sub1);end
if j==2,subplot(sub2);end
if j==3,subplot(sub3);end
if j==4,subplot(sub4);end
axis(T),hold on
title(name(j,:),'erasemode','none','fontweight','bold')
end
end
%ORIGINALS
if ch==1
subplot(sub1)
else
figure('units','normal','position',[0 0 1 .95],'color','white');
axis(T),hold on
title(name(1,:),'erasemode','none','fontweight','bold')
end
plot3([0 X(1)],[0 X(2)],[0 X(3)],'-b','erasemode','none','linewidth',2)
hold on
plot3([0 Y(1)],[0 Y(2)],[0 Y(3)],'-r','erasemode','none','linewidth',2)
grid on
gca;
plot3(Xlim,[0 0],[0 0],'-k',[0 0],Ylim,[0 0],'-k',[0 0],[0 0],Zlim,'-k','linewidth',2,'erasemode','none');
pause(3)
text(X(1)/2,X(2)/2,X(3)/2,'u','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(1)
text(Y(1)/2,Y(2)/2,Y(3)/2,'v','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
hold off
if ch==2
xlabel('Press ENTER to Continue.','erasemode','none','fontweight','bold')
pause
else
pause(3)
end
%SUM
if ch==1
subplot(sub2)
else
figure('units','normal','position',[0 0 1 .95],'color','white');
axis(T),hold on
title(name(2,:),'erasemode','none','fontweight','bold')
end
plot3([0 X(1)],[0 X(2)],[0 X(3)],'-b','erasemode','none','linewidth',2)
hold on
plot3([0 Y(1)],[0 Y(2)],[0 Y(3)],'-r','erasemode','none','linewidth',2)
grid on
gca;
plot3(Xlim,[0 0],[0 0],'-k',[0 0],Ylim,[0 0],'-k',[0 0],[0 0],Zlim,'-k','linewidth',2,'erasemode','none');
pause(3)
text(X(1)/2,X(2)/2,X(3)/2,'u','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(1)
text(Y(1)/2,Y(2)/2,Y(3)/2,'v','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(1)
Z=X+Y;
plot3([X(1) Z(1)],[X(2) Z(2)],[X(3) Z(3)],':r','erasemode','none','linewidth',2)
pause(2)
text((X(1)+Z(1))/2,(X(2)+Z(2))/2,(X(3)+Z(3))/2,'v','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(2)
plot3([0 Z(1)],[0 Z(2)],[0 Z(3)],'--g','erasemode','none','linewidth',2)
pause(1)
text(Z(1)/2,Z(2)/2,Z(3)/2,'u+v','erasemode','none','color','green',...
'fontsize',14,'fontweight','bold')
hold off
if ch==2
xlabel('Press ENTER to Continue.','fontweight','bold','erasemode','none')
pause
else
pause(3)
end
%DIFERENCE
if ch==1
subplot(sub3)
else
figure('units','normal','position',[0 0 1 .95],'color','white');
axis(T),hold on
title(name(3,:),'erasemode','none','fontweight','bold')
end
plot3([0 X(1)],[0 X(2)],[0 X(3)],'-b','erasemode','none','linewidth',2)
hold on
plot3([0 Y(1)],[0 Y(2)],[0 Y(3)],'-r','erasemode','none','linewidth',2)
grid on
gca;
plot3(Xlim,[0 0],[0 0],'-k',[0 0],Ylim,[0 0],'-k',[0 0],[0 0],Zlim,'-k','linewidth',2,'erasemode','none');
pause(3)
text(X(1)/2,X(2)/2,X(3)/2,'u','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(1)
text(Y(1)/2,Y(2)/2,Y(3)/2,'v','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(1)
%new code to put in natural difference from origin 10/3/97
%
D=X-Y;
plot3([X(1) D(1)],[X(2) D(2)],[X(3) D(3)],':r','erasemode','none','linewidth',2)
text((X(1)+D(1))/2,(X(2)+D(2))/2,(X(3)+D(3))/2,'-v','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(2)
plot3([0 D(1)],[0 D(2)],[0 D(3)],'--g','erasemode','none','linewidth',2)
text(D(1)/2,D(2)/2,D(3)/2,'u-v','erasemode','none','color','green',...
'fontsize',14,'fontweight','bold')
pause(2)
messd=xlabel('Press ENTER to see the standard parallelogram.','fontweight','bold');
pause
delete(messd)
%%%%%%%%%%%%%%%%%%%%%%%%%
plot3([Y(1) Z(1)],[Y(2) Z(2)],[Y(3) Z(3)],'-y','erasemode','none','linewidth',2)
pause(2)
text((Y(1)+Z(1))/2,(Y(2)+Z(2))/2,(Y(3)+Z(3))/2,'u','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
plot3([X(1) Z(1)],[X(2) Z(2)],[X(3) Z(3)],':y','erasemode','none','linewidth',2)
pause(2)
text((X(1)+Z(1))/2,(X(2)+Z(2))/2,(X(3)+Z(3))/2,'-v','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
plot3([Y(1) X(1)],[Y(2) X(2)],[Y(3) X(3)],'--g','erasemode','none','linewidth',2)
pause(2)
text(Z(1)/2,Z(2)/2,Z(3)/2,'u-v','erasemode','none','color','green',...
'fontsize',14,'fontweight','bold')
hold off
if ch==2
xlabel('Press ENTER to Continue.','fontweight','bold','erasemode','none')
pause
else
pause(3)
end
%MULTIPLES
if ch==1
subplot(sub4)
else
figure('units','normal','position',[0 0 1 .95],'color','white');
axis(T),hold on
title(name(4,:),'erasemode','none','fontweight','bold')
end
plot3([0 -2*X(1)],[0 -2*X(2)],[ 0 -2*X(3)],'-b','erasemode','none','linewidth',2)
hold on
plot3([0 .5*Y(1)],[0 .5*Y(2)],[0 .5*Y(3)],'-r','erasemode','none','linewidth',2)
grid on
gca;
plot3(Xlim,[0 0],[0 0],'-k',[0 0],Ylim,[0 0],'-k',[0 0],[0 0],Zlim,'-k','linewidth',2,'erasemode','none');
pause(3)
text(-X(1),-X(2),-X(3),'-2u','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(1)
text(.25*Y(1),.25*Y(2),.25*Y(3),'(1/2)v','erasemode','none','color','magenta',...
'fontsize',14,'fontweight','bold')
pause(1)
if ch==2,xlabel('Press ENTER to Return to the command screen.','erasemode','none','fontweight','bold');end
hold off
if ch==1
%WARNING MESSAGE
warnsub=subplot('position',[.02 .1 .2 .1]);axis('off');
text(0,1,'WARNING: In some cases you may need to rotate','color','black','fontsize',12,'fontweight','bold');
text(0,.7,'the coordinate system in order to see the','color','black','fontsize',12,'fontweight','bold');
text(0,.4,'the vectors in their proper relationship. =====>','color','black','fontsize',12,'fontweight','bold');
%rotation message
messsub=subplot('position',[.5 .1 .2 .1]);axis('off');
text(0,1,'To rotate the coordinate system click on Tools,','color','red','fontsize',12,'fontweight','bold');
text(0,.7,'then click on Rotate 3D. Next click in the figure','color','red','fontsize',12,'fontweight','bold');
text(0,.4,'and drag the mouse to reorient the system.','color','red','fontsize',12,'fontweight','bold');
text(0,0,'PRESS ENTER to return to the command screen.','fontsize',12,'fontweight','bold');
end
clc
if ch==2,disp(warnmess),disp(rotmess),end
disp(s6)
return