forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcspr.m
139 lines (99 loc) · 2.26 KB
/
cspr.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
function cspr(c,mode)
%
% cspr(c,mode)
%
% Show oscillations of a linear one-dimensional mass-spring system
%
% c = vector indicating which combination of modes to be shown
%
% mode = 'f' -- both ends fixed
% 'd' -- one free and one fixed end (default if omitted)
% 'u' -- both ends free
%
% See also DYST, DSPR, GSPR, MSPR
if nargin ==1, mode = 'd'; end
n = size(c,2); C = diag(c);
global buttonid;
buttonid = 0;
icons = ['text(.5,.5,''pause'' ,''Horiz'',''center'')'
'text(.5,.5,''input'' ,''Horiz'',''center'')'
'text(.5,.5,''stop'' ,''Horiz'',''center'')'];
callbacks = ['button(1)'
'button(2)'
'button(3)'];
presstype = ['toggle'
'flash '
'flash '];
hf = figure(1);
clf;
bg = btngroup(hf,'GroupID','b','ButtonId',['p';'i';'s'],...
'IconFunctions',icons,'GroupSize',[1 3],...
'CallBack',callbacks,'Position',[.3 0 .4 .07],...
'PressType',presstype);
pa = 50; tol = .00001;
dt = .5;
A = diag(ones(n+1,1)) - diag(ones(n,1),-1);
A(:,n+1) = [];
switch mode
case 'd'
A(1,:) = [];
Z0 = []; Zn = n+1;
nn = n+1; n0 = 0; n1 = n+2;
case 'u'
A(n+1,:) = []; A(1,:) = [];
Z0 = []; Zn = [];
nn = n; n0 = -3; n1 = n+4;
otherwise
Z0 = 0; Zn = n+1;
nn = n+2; n0 = -1; n1 = n+2;
end
Z = [Z0,1:n,Zn];
n2 = n/2; U = n2 * ones(1,nn);
vn = [1:n]';
K = A' * A;
[E,D] = eig(K);
d = diag(D);
[d,I] = sort(d);
D = diag(d);
E = E(:,I);
l = sqrt(d);
L = diag(l);
zerol = abs(l) < tol;
zeroL = diag(zerol);
r = .5;
ax = axes('Position',[.15 .15 .75 .75]);
hold on;
sprs = plot([n2 n2],[Z(1),Z(nn)],'erasemode','xor');hold off;
hold off;
hold on;
plt = plot(U,Z,'.','erasemode','xor','markersize',24);
hold off;
axis([0 n n0 n1])
s = 0;
stopit = 0;
while stopit == 0
switch buttonid
case 1
buttonid = 0;
while buttonid == 0, pause(.2); end
buttonid = 0;
case 2
rst = 1;
disp(['Currently r = ',num2str(r),' dt = ',num2str(dt)]);
keyboard
buttonid = 0;
case 3
stopit = 1;
buttonid = 0;
end
s = s + dt;
X = E * C * (sin(L*s) + zeroL*s);
M = vn + sum(X,2);
Z = [Z0,M',Zn];
%keyboard
set(sprs,'ydata',[Z(1),Z(nn)]);
set(plt,'ydata',Z);
drawnow
k = 0;
for i=1:1000*pa, k = 1-k; end
end