About Pushbutton Stop
Transcript of About Pushbutton Stop
-
8/12/2019 About Pushbutton Stop
1/23
Lab 1, EME 121, UC Davis, Spring 2011
Nasser M. Abbasi
Contents
1 Problem description 2
2 Mathematical model 4
3 Result of simulation 5
4 Discussion of results 8
5 Appendix 8
5.1 Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1
-
8/12/2019 About Pushbutton Stop
2/23
1 Problem description
We are asked to write a program to simulation the equation of motion of the following springpendulum shown in the textbook on page 42
Notice that the r generalized coordinate is measured from the corner where the springis attached to and not from the static equilibrium position. This diagram below helps to
illustrate this more.
2
-
8/12/2019 About Pushbutton Stop
3/23
Now the equations of motion are found and then they are converted to state form descriptionand then ODE solver was used to integrate them.
3
-
8/12/2019 About Pushbutton Stop
4/23
2 Mathematical model
This system is 2 degree of freedom system, since two generalized coordinates are required touniquely locate the massmat any point of time. Hence, two EQMs (equation of motions)are required.
ApplyingF =ma in the radial spring direction results in the following EQM
T+mg cos = m(r r ()2)
Rearranging, and noting thatT =k (r l0) we obtain
r =r ()2
+g cos k
m(r l0) (1)
ApplyingF = ma in the perpendicular direction to the spring results in the followingEQM
mg sin = m (r + 2r)
= g
rsin
2r
r (2)
To use the ODE solver, the above second order ODEs, Eqs., (1) and (2), need to beconverted to first order ODE. This is done by introducing state variables as follows
x1= r
x2= r
x3=
x4=
d
dt
x1
= x2
x2
= x1x2
4+g cos x3
km
(x1 l0)
x3
= x4
x4
= grsin x3
2x2x4x1
Therefore, the state space form is
x1
x2
x3
x4
=
x2
x1x2
4+g cos x3
km
(x1 l0)
x4
g
rsin x3
2x2x4x1
(3)
The above system is solved using ODE solver and the result are shown in the followingsection.
4
-
8/12/2019 About Pushbutton Stop
5/23
3 Result of simulation
Using the following parameters to generate figure (3.2) and figure 3.3 for the very stiff spring
m f l0
r (0) r
(0) (0)
(0)
5kg 1000 Hz 1 m 1 m 0 450 0
5
-
8/12/2019 About Pushbutton Stop
6/23
Using the following parameters to generate figure (3.4)
m f l0 r (0) r (0) (0) (0)
5kg 1 Hz 1 m 1 m 0 450 0
6
-
8/12/2019 About Pushbutton Stop
7/23
Using the following parameters to generate figure (3.5)
m f l0 r (0) r (0) (0) (0)
5kg 1 Hz 1 m 2 m 0 550 0
7
-
8/12/2019 About Pushbutton Stop
8/23
4 Discussion of results
When the spring is made very stiff, then the change of the pendulum length can be seen fromthe figure 3.3 result to be very small. This is the same result as a normal pendulum will have.This is also verified by figure 3.2 which shows a simple harmonic motion as shown in figure
3.2 (pendulum angle vs. time).When the spring stiffness is reduced (f= 1Hz), then we can see that the motion is no
longer a simple harmonic motion as can be seen in figure (3.3 and 3.4). Changing the lengthof the initial pendulum also resulted is completely different profile of the motion.
5 Appendix
5.1 Source code
1 function varargout = eme_121_lab1(varargin)
2 % EME_121_LAB1 Mfile for eme_121_lab1.fig
3 % EME_121_LAB1, by itself, creates a new EME_121_LAB1 or raises the existing
4 % singleton*.
5 %
6 % H = EME_121_LAB1 returns the handle to a new EME_121_LAB1 or the handle to
7 % the existing singleton*.
8 %
9 % EME_121_LAB1('CALLBACK',hObject,eventData,handles,...) calls the local
10 % function named CALLBACK in EME_121_LAB1.M with the given input arguments.
11 %
12 % EME_121_LAB1('Property','Value',...) creates a new EME_121_LAB1 or raises
13
% existing singleton*. Starting from the left, property value pairs are14 % applied to the GUI before eme_121_lab1_OpeningFcn gets called. An
15 % unrecognized property name or invalid value makes property application
16 % stop. All inputs are passed to eme_121_lab1_OpeningFcn via varargin.
17 %
18 % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
19 % instance to run (singleton)".
20 %
21 % See also: GUIDE, GUIDATA, GUIHANDLES
22
23 % Edit the above text to modify the response to help eme_121_lab1
24
25 % Last Modified by GUIDE v2.5 10Apr2011 23:32:56
26
27 % By Nasser M. Abbasi
28
29 % Begin initialization code DO NOT EDIT
30 gui_Singleton = 1;
31 gui_State = struct('gui_Name', mfilename, ...
32 'gui_Singleton', gui_Singleton, ...
33 'gui_OpeningFcn', @eme_121_lab1_OpeningFcn, ...
34 'gui_OutputFcn', @eme_121_lab1_OutputFcn, ...
35 'gui_LayoutFcn', [] , ...
8
-
8/12/2019 About Pushbutton Stop
9/23
36 'gui_Callback', []);
37 if nargin && ischar(varargin{1})
38 gui_State.gui_Callback = str2func(varargin{1});
39 end
40
41 if nargout
42 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});43 else
44 gui_mainfcn(gui_State, varargin{:});
45 end
46 % End initialization code DO NOT EDIT
47
48
49 % Executes just before eme_121_lab1 is made visible.
50 function eme_121_lab1_OpeningFcn(hObject, eventdata, handles, varargin)
51 % This function has no output args, see OutputFcn.
52 % hObject handle to figure
53 % eventdata reserved to be defined in a future version of MATLAB
54 % handles structure with handles and user data (see GUIDATA)
55 % varargin command line arguments to eme_121_lab1 (see VARARGIN)56
57 % Choose default command line output for eme_121_lab1
58 handles.output = hObject;
59
60 set(handles.figure1, 'UserData',[]);
61 set(handles.figure1,'Name','EME 121 first lab by Nasser M. Abbasi');
62
63 data.stop = false;
64 set(handles.figure1, 'UserData',data);
65
66 % Update handles structure
67 guidata(hObject, handles);
68
69 % UIWAIT makes eme_121_lab1 wait for user response (see UIRESUME)
70 % uiwait(handles.figure1);
71
72
73 % Outputs from this function are returned to the command line.
74 function varargout = eme_121_lab1_OutputFcn(hObject, eventdata, handles)
75 % varargout cell array for returning output args (see VARARGOUT);
76 % hObject handle to figure
77 % eventdata reserved to be defined in a future version of MATLAB
78 % handles structure with handles and user data (see GUIDATA)
79
80 % Get default command line output from handles structure
81 varargout{1} = handles.output;
82
83
84
85 function mass_tag_Callback(hObject, eventdata, handles)
86 % hObject handle to mass_tag (see GCBO)
87 % eventdata reserved to be defined in a future version of MATLAB
88 % handles structure with handles and user data (see GUIDATA)
89
9
-
8/12/2019 About Pushbutton Stop
10/23
90 % Hints: get(hObject,'String') returns contents of mass_tag as text
91 % str2double(get(hObject,'String')) returns contents of mass_tag as a doub
92
93
94 % Executes during object creation, after setting all properties.
95 function mass_tag_CreateFcn(hObject, eventdata, handles)
96 % hObject handle to mass_tag (see GCBO)97 % eventdata reserved to be defined in a future version of MATLAB
98 % handles empty handles not created until after all CreateFcns called
99
100 % Hint: edit controls usually have a white background on Windows.
101 % See ISPC and COMPUTER.
102 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
103 set(hObject,'BackgroundColor','white');
104 end
105
106
107
108 function f_tag_Callback(hObject, eventdata, handles)
109 % hObject handle to f_tag (see GCBO)110 % eventdata reserved to be defined in a future version of MATLAB
111 % handles structure with handles and user data (see GUIDATA)
112
113 % Hints: get(hObject,'String') returns contents of f_tag as text
114 % str2double(get(hObject,'String')) returns contents of f_tag as a double
115
116
117 % Executes during object creation, after setting all properties.
118 function f_tag_CreateFcn(hObject, eventdata, handles)
119 % hObject handle to f_tag (see GCBO)
120 % eventdata reserved to be defined in a future version of MATLAB
121 % handles empty handles not created until after all CreateFcns called
122
123 % Hint: edit controls usually have a white background on Windows.
124 % See ISPC and COMPUTER.
125 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
126 set(hObject,'BackgroundColor','white');
127 end
128
129
130
131 function length_tag_Callback(hObject, eventdata, handles)
132 % hObject handle to length_tag (see GCBO)
133 % eventdata reserved to be defined in a future version of MATLAB
134 % handles structure with handles and user data (see GUIDATA)
135
136 % Hints: get(hObject,'String') returns contents of length_tag as text
137 % str2double(get(hObject,'String')) returns contents of length_tag as a dou
138
139
140 % Executes during object creation, after setting all properties.
141 function length_tag_CreateFcn(hObject, eventdata, handles)
142 % hObject handle to length_tag (see GCBO)
143 % eventdata reserved to be defined in a future version of MATLAB
10
-
8/12/2019 About Pushbutton Stop
11/23
144 % handles empty handles not created until after all CreateFcns called
145
146 % Hint: edit controls usually have a white background on Windows.
147 % See ISPC and COMPUTER.
148 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
149 set(hObject,'BackgroundColor','white');
150 end151
152
153 function angle_zero_tag_Callback(hObject, eventdata, handles)
154 % hObject handle to angle_zero_tag (see GCBO)
155 % eventdata reserved to be defined in a future version of MATLAB
156 % handles structure with handles and user data (see GUIDATA)
157
158 % Hints: get(hObject,'String') returns contents of angle_zero_tag as text
159 % str2double(get(hObject,'String')) returns contents of angle_zero_tag as a
160
161
162 % Executes during object creation, after setting all properties.
163 function angle_zero_tag_CreateFcn(hObject, eventdata, handles)164 % hObject handle to angle_zero_tag (see GCBO)
165 % eventdata reserved to be defined in a future version of MATLAB
166 % handles empty handles not created until after all CreateFcns called
167
168 % Hint: edit controls usually have a white background on Windows.
169 % See ISPC and COMPUTER.
170 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
171 set(hObject,'BackgroundColor','white');
172 end
173
174 function angle_speed_zero_Callback(hObject, eventdata, handles)
175 % hObject handle to angle_speed_zero (see GCBO)
176 % eventdata reserved to be defined in a future version of MATLAB
177 % handles structure with handles and user data (see GUIDATA)
178
179 % Hints: get(hObject,'String') returns contents of angle_speed_zero as text
180 % str2double(get(hObject,'String')) returns contents of angle_speed_zero a
181
182
183 % Executes during object creation, after setting all properties.
184 function angle_speed_zero_CreateFcn(hObject, eventdata, handles)
185 % hObject handle to angle_speed_zero (see GCBO)
186 % eventdata reserved to be defined in a future version of MATLAB
187 % handles empty handles not created until after all CreateFcns called
188
189 % Hint: edit controls usually have a white background on Windows.
190 % See ISPC and COMPUTER.
191 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
192 set(hObject,'BackgroundColor','white');
193 end
194
195 function r_zero_tag_Callback(hObject, eventdata, handles)
196 % hObject handle to r_zero_tag (see GCBO)
197 % eventdata reserved to be defined in a future version of MATLAB
11
-
8/12/2019 About Pushbutton Stop
12/23
198 % handles structure with handles and user data (see GUIDATA)
199
200 % Hints: get(hObject,'String') returns contents of r_zero_tag as text
201 % str2double(get(hObject,'String')) returns contents of r_zero_tag as a dou
202
203
204 % Executes during object creation, after setting all properties.205 function r_zero_tag_CreateFcn(hObject, eventdata, handles)
206 % hObject handle to r_zero_tag (see GCBO)
207 % eventdata reserved to be defined in a future version of MATLAB
208 % handles empty handles not created until after all CreateFcns called
209
210 % Hint: edit controls usually have a white background on Windows.
211 % See ISPC and COMPUTER.
212 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
213 set(hObject,'BackgroundColor','white');
214 end
215
216
217
218 function r_speed_zero_Callback(hObject, eventdata, handles)
219 % hObject handle to r_speed_zero (see GCBO)
220 % eventdata reserved to be defined in a future version of MATLAB
221 % handles structure with handles and user data (see GUIDATA)
222
223 % Hints: get(hObject,'String') returns contents of r_speed_zero as text
224 % str2double(get(hObject,'String')) returns contents of r_speed_zero as a d
225
226
227 % Executes during object creation, after setting all properties.
228 function r_speed_zero_CreateFcn(hObject, eventdata, handles)
229 % hObject handle to r_speed_zero (see GCBO)
230 % eventdata reserved to be defined in a future version of MATLAB
231 % handles empty handles not created until after all CreateFcns called
232
233 % Hint: edit controls usually have a white background on Windows.
234 % See ISPC and COMPUTER.
235 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
236 set(hObject,'BackgroundColor','white');
237 end
238
239
240 % Executes on button press in run_btn.
241 function run_btn_Callback(hObject, eventdata, handles)
242 % hObject handle to run_btn (see GCBO)
243 % eventdata reserved to be defined in a future version of MATLAB
244 % handles structure with handles and user data (see GUIDATA)
245
246 [data,status] = parse_input(handles);
247 if not(status)
248 return;
249 end
250
251 data.handles = handles;
12
-
8/12/2019 About Pushbutton Stop
13/23
252 data.g = 9.8;
253
254 enable_all(handles,'off');
255 userData = get(handles.figure1, 'UserData');
256 userData.stop = false;
257 set(handles.figure1, 'UserData',userData);
258
259 [g_msg,g_status]=process_eme_121_lab1(data);
260 if not(g_status)
261 uiwait(errordlg(sprintf('Processing terminated: %s',g_msg),...
262 'Bad Input', 'modal'));
263 uicontrol(handles.f_tag);
264 end
265 enable_all(handles,'on');
266
267 %
268 function [data,status]= parse_input(handles)
269
270 [data.mass,status]=verify_valid_positive_numeric...
271 (get(handles.mass_tag,'String'),handles.mass_tag,...272 'Mass must be positive number');
273 if not(status)
274 return;
275 end
276
277 [data.f,status]=verify_valid_positive_numeric...
278 (get(handles.f_tag,'String'),handles.f_tag,...
279 'natural frequency must be positive number');
280 if not(status)
281 return;
282 end
283
284
285 [data.L,status]=verify_valid_positive_numeric...
286 (get(handles.length_tag,'String'),handles.length_tag,...
287 'pendulum length must be positive number');
288 if not(status)
289 return;
290 end
291
292 [data.angle_zero,status]=verify_valid_numeric...
293 (get(handles.angle_zero_tag,'String'),handles.angle_zero_tag,...
294 'Initial angle must be numerical value');
295 if not(status)
296 return;
297 end
298
299 if abs(data.angle_zero)>180
300 uiwait(errordlg('Initial angle must be between 0 and 180 degrees only',...
301 'Bad Input', 'modal'));
302 status = 0;
303 uicontrol(handles.angle_zero);
304 return
305 else
13
-
8/12/2019 About Pushbutton Stop
14/23
306 data.angle_zero = data.angle_zero*pi/180;
307 end
308
309 [data.r_zero,status]=verify_valid_positive_numeric...
310 (get(handles.r_zero_tag,'String'),handles.r_zero_tag,...
311 'initial r must be numerical value');
312 if not(status)313 return;
314 end
315
316
317 [data.r_speed_zero,status]=verify_valid_numeric...
318 (get(handles.r_speed_zero,'String'),handles.r_speed_zero,...
319 'initial r speed must be numerical value');
320 if not(status)
321 return;
322 end
323
324
325 [data.time_step,status]=verify_valid_numeric...326 (get(handles.time_Step_tag,'String'),handles.time_Step_tag,...
327 'time step must be zero or larger');
328 if not(status)
329 return;
330 end
331
332 [data.angle_speed_zero,status]=verify_valid_numeric...
333 (get(handles.angle_speed_zero,'String'),handles.angle_speed_zero,...
334 'initial angle speed must be numerical value');
335 if not(status)
336 return;
337 end
338
339 [data.max_t,status]=verify_valid_positive_numeric...
340 (get(handles.max_t_tag,'String'),handles.max_t_tag,...
341 'maximum simulation time must be positive number');
342 if not(status)
343 return;
344 end
345
346 contents = cellstr(get(handles.ode_solver_tag,'String'));
347 data.solver = contents{get(handles.ode_solver_tag,'Value')};
348
349
350 data.k = data.mass*
(2*
pi*
data.f)^2;
351
352 %
353 function enable_all(handles,to)
354 set(handles.mass_tag,'Enable',to);
355 set(handles.f_tag,'Enable',to);
356 set(handles.length_tag,'Enable',to);
357 set(handles.angle_zero_tag,'Enable',to);
358 set(handles.r_speed_zero,'Enable',to);
359 set(handles.angle_speed_zero,'Enable',to);
14
-
8/12/2019 About Pushbutton Stop
15/23
360 set(handles.r_zero_tag,'Enable',to);
361 set(handles.max_t_tag,'Enable',to);
362 set(handles.ode_solver_tag,'Enable',to);
363 set(handles.time_Step_tag,'Enable',to);
364
365 function max_t_tag_Callback(hObject, eventdata, handles)
366 % hObject handle to max_t_tag (see GCBO)367 % eventdata reserved to be defined in a future version of MATLAB
368 % handles structure with handles and user data (see GUIDATA)
369
370 % Hints: get(hObject,'String') returns contents of max_t_tag as text
371 % str2double(get(hObject,'String')) returns contents of max_t_tag as a doub
372
373
374 % Executes during object creation, after setting all properties.
375 function max_t_tag_CreateFcn(hObject, eventdata, handles)
376 % hObject handle to max_t_tag (see GCBO)
377 % eventdata reserved to be defined in a future version of MATLAB
378 % handles empty handles not created until after all CreateFcns called
379
380 % Hint: edit controls usually have a white background on Windows.
381 % See ISPC and COMPUTER.
382 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
383 set(hObject,'BackgroundColor','white');
384 end
385
386
387 % Executes on button press in reset_tag.
388 function reset_tag_Callback(hObject, eventdata, handles)
389 % hObject handle to reset_tag (see GCBO)
390 % eventdata reserved to be defined in a future version of MATLAB
391 % handles structure with handles and user data (see GUIDATA)
392
393
394 data = get(handles.figure1, 'UserData');
395 data.stop = true;
396 set(handles.figure1, 'UserData',data);
397 enable_all(handles,'on');
398
399
400 % Executes on selection change in ode_solver_tag.
401 function ode_solver_tag_Callback(hObject, eventdata, handles)
402 % hObject handle to ode_solver_tag (see GCBO)
403 % eventdata reserved to be defined in a future version of MATLAB
404 % handles structure with handles and user data (see GUIDATA)
405
406 % Hints: contents = cellstr(get(hObject,'String')) returns ode_solver_tag content
407 % contents{get(hObject,'Value')} returns selected item from ode_solver_tag
408
409 % Executes during object creation, after setting all properties.
410 function ode_solver_tag_CreateFcn(hObject, eventdata, handles)
411 % hObject handle to ode_solver_tag (see GCBO)
412 % eventdata reserved to be defined in a future version of MATLAB
413 % handles empty handles not created until after all CreateFcns called
15
-
8/12/2019 About Pushbutton Stop
16/23
414
415 % Hint: popupmenu controls usually have a white background on Windows.
416 % See ISPC and COMPUTER.
417 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
418 set(hObject,'BackgroundColor','white');
419 end
420
421 function time_Step_tag_Callback(hObject, eventdata, handles)
422 % hObject handle to time_Step_tag (see GCBO)
423 % eventdata reserved to be defined in a future version of MATLAB
424 % handles structure with handles and user data (see GUIDATA)
425
426 % Hints: get(hObject,'String') returns contents of time_Step_tag as text
427 % str2double(get(hObject,'String')) returns contents of time_Step_tag as a
428
429
430 % Executes during object creation, after setting all properties.
431 function time_Step_tag_CreateFcn(hObject, eventdata, handles)
432 % hObject handle to time_Step_tag (see GCBO)
433 % eventdata reserved to be defined in a future version of MATLAB434 % handles empty handles not created until after all CreateFcns called
435
436 % Hint: edit controls usually have a white background on Windows.
437 % See ISPC and COMPUTER.
438 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgrou
439 set(hObject,'BackgroundColor','white');
440 end
1 function [g_msg,g_status]=process_eme_121_lab1(data)
2 % This function is called by the Matlab GUI to solve
3 % the equation of motion for Lab 1 problem, EME 121,4 % spring 2011, UC Davis.
5 %
6 % INPUT: data, a record which contains the problem parameters
7 %
8 % The function will call ode45 solver and plot the solution
9 %
10 % by Nasser M. Abbasi
11 % 4/12/2011
12
13
14 % Reset axes for plotting
15 cla(data.handles.axes,'reset');
16 cla(data.handles.time_angle_axes,'reset');
17 cla(data.handles.time_r_axes,'reset');
18 cla(data.handles.angle_r_axes,'reset');
19
20 %Check if using auto step size of an explicit step size. see
21 %GUI for input choice.
22 if data.time_step==0
23 time_span=[0 data.max_t];
24 else
16
-
8/12/2019 About Pushbutton Stop
17/23
25 time_span=0:data.time_step/1000:data.max_t;
26 end
27
28 %set the yaxis for plotting
29 max_y = data.L;
30
31 %set the initial conditions for ode4532 IC = [data.r_zero;data.r_speed_zero;data.angle_zero;data.angle_speed_zero];
33 options = odeset('OutputFcn',@output);
34
35 g_status = true;
36 g_msg='';
37
38 %call the solver
39 [t,x] = feval(data.solver,@rhs,time_span,IC,options,data);
40
41
42 %plot the result
43 N=length(t);
44 picks=1:N;45
46 set(data.handles.figure1, 'CurrentAxes',data.handles.time_angle_axes);
47 angle=x(:,3);
48 plot(t(picks),angle(picks)*180/pi,'');
49 title('pendulum angle vs time');
50 xlabel('Time (sec)');
51 ylabel('angle (degree)');
52 grid;
53 drawnow;
54
55 set(data.handles.figure1, 'CurrentAxes',data.handles.time_r_axes);
56 r=x(:,1);
57 stairs(t(picks),r(picks),'');
58 title('pendulum length vs time');
59 xlabel('Time (sec)'); ylabel('length (m)');
60 grid;
61 drawnow;
62
63 set(data.handles.figure1, 'CurrentAxes',data.handles.angle_r_axes);
64
65 plot(angle(picks)*180/pi,r(picks),'');
66 title('pendulum angle vs length');
67 xlabel('Angle (degree)'); ylabel('length (m)');
68 grid;
69 drawnow;
70
71
72 %
73 %
74 %
75 function dx=rhs(,x,data)
76 %the ode45 function
77 temp = x(1)data.L;
78
17
-
8/12/2019 About Pushbutton Stop
18/23
79 if abs(tempmax_y
91 max_y = x(1)+0.2*x(1);
92 end
93 end
94
95 %
96 %
97 %
98 function status= output(t,x,,data)99 %called by ode45 after each step. Plot the current
100 %pendulum position for simulation
101
102 userData = get(data.handles.figure1, 'UserData');
103 if userData.stop
104 status=true;
105 g_status =true;
106 else
107 status = false;
108 if not(isempty(t))
109 plot_solution(x(1,end),x(3,end),t(1),data);
110 %pause(0.1);
111 if x(1,end)>10*data.L %spring stretched too far
112 status = true;
113 g_status = false;
114 g_msg ='spring over stretched';
115 end
116 end
117 end
118 end
119
120 %
121 %
122 %
123 function plot_solution(r,theta,t,data)
124 %plot the final solution
125 set(0,'CurrentFigure',data.handles.figure1);
126 set(data.handles.figure1, 'CurrentAxes',data.handles.axes);
127
128 [x_local,y] = pol2cart(thetapi/2,r);
129 plot(x_local,y,'o','MarkerSize',20,'MarkerEdgeColor','r',...
130 'MarkerFaceColor','r','LineWidth',2);
131 [x_local,y] = nma_spring.make(r,theta(pi/2),30);
132 hold on;
18
-
8/12/2019 About Pushbutton Stop
19/23
133 line(x_local,y);
134 plot(0,0,'o','MarkerSize',5,'MarkerEdgeColor','k',...
135 'MarkerFaceColor','k','LineWidth',1);
136 title(sprintf(...
137 'time = %3.4f, angle(degree) = %3.3f, r = %3.3f, k=%3.4f',...
138 t,theta*180/pi,r,data.k));
139
140 xlim([max_y max_y]);
141 ylim([max_y max_y]);
142 %refreshdata
143 grid;
144 drawnow;
145 hold off;
146 end
147
148 end
1 classdef nma_spring2 %
3 %static class to make spring for plotting animations
4 %by Nasser M. Abbasi
5
6
7 properties
8 end
9
10 methods(Static)
11
12 %
13 %
14 %15 function [x,y] = make(r,theta,N)
16 %r: total length of spring
17 %theta: in radians, anticlock wise is positive,
18 % theta zero is position xaxis
19 %N: number of twists in spring
20 %
21 %OUTPUT:
22 % x,y coordinates of line to use to plot spring
23 len = (4/6)*r;
24 p = zeros(N,2);
25 delr = len/N;
26
27 r0 = (1/6)*r;
28 p(2,1) = r0;
29 p(2,2) = theta;
30
31 for n=3:N2
32 p(n,1)=r0+delr*n;
33 z=atan(2*delr/p(n,1));
34 p(n,2)=theta+(1)^n*z;
35 end
19
-
8/12/2019 About Pushbutton Stop
20/23
36 p(end1,1)=(5/6)*r; p(end1,2)=theta;
37 p(end,1)=r; p(end,2)=theta;
38
39 [x,y] = pol2cart(p(:,2),p(:,1));
40 end
41
42 %43 function [x,y] = makeBox(w,h)
44 x = zeros(5,1);
45 y = zeros(5,1);
46 x(1)= w/2; y(1)=0;
47 x(2)= w/2; y(2)=h/2;
48 x(3)= w/2; y(3)=h/2;
49 x(4)=w/2; y(4)=0;
50 x(5)=x(1); y(5)=y(1);
51
52 end
53 %
54 %
55 %
56 function [x,y] = makeV2(len,N,w)
57 %len: total length of spring
58 %N: number of twists in spring
59 %
60 %OUTPUT:
61 % x,y coordinates of line to use to plot spring
62
63 nCoordinates = 2*N+4;
64 p = zeros(nCoordinates,2);
65 h = (1/6)*len;
66 del=(len2*h)/(N+1);
67 done = false;
68 n = 1;
69 while not(done)
70
71 if n==1
72 p(1,1)=0;
73 p(1,2)=0;
74 n = n + 1;
75 elseif n==2
76 p(2,1)=0;
77 p(2,2)=h;
78 n = n + 1;
79 elseif n
-
8/12/2019 About Pushbutton Stop
21/23
90 else
91 p(n,1) = 0;
92 p(n,2)=p(n1,2)+h;
93 done = true;
94 end
95 end
96 x=p(:,1);97 y=p(:,2);
98
99 end
100
101
102
103 %
104 %
105 %
106 function test(theta)
107
108 close all;
109
110 theta = theta*pi/180;
111 figure;
112 set(gcf,'Units','normalized');
113 delr=0.3;
114 for r=1:.1:3
115 cla;
116 [x,y] = nma_spring.make(r,theta,30);
117 line(x,y);
118 hold on;
119
120 %baseX = [2*delr*cos((pi/2)theta) 2*delr*sin(theta)];
121 %baseY = [2*delr*sin((pi/2)theta) 2*delr*cos(theta)];
122
123 baseX = [2*delr*sin(abs(theta)) 2*delr*sin(abs(theta))];
124 baseY = [2*delr*cos(theta) 2*delr*cos(theta)];
125 line(baseX,baseY);
126
127 triX=[baseX(1) ...
128 baseX(1) ...
129 30*delr*cos(theta)abs(baseX(1))...
130 baseX(1)];
131
132 triY=[baseY(1) ...
133 (30*delr*sin(abs(theta))+abs(baseY(1)))...
134 (30*
delr*
sin(abs(theta))+abs(baseY(1)))...
135 baseY(1)];
136
137 line(triX,triY);
138
139 L=sqrt(x(end)^2+y(end)^2);
140 xx0=L*cos(theta);
141 yy0=L*sin(abs(theta));
142 massX=[xx0 ...
143 xx0+2*delr*sin(abs(theta)) ...
21
-
8/12/2019 About Pushbutton Stop
22/23
144 xx0+2*delr*sin(abs(theta))+4*delr*cos(theta) ...
145 xx0+2*delr*sin(abs(theta))+4*delr*cos(theta)4*delr*sin(abs(th
146 xx0+2*delr*sin(abs(theta))+4*delr*cos(theta)4*delr*sin(abs(th
147 xx0];
148
149 massY=[ yy0 ...
150 yy0+2*delr*cos(theta) ...151 yy0+2*delr*cos(theta)4*delr*sin(abs(theta)) ...
152 yy0+2*delr*cos(theta)4*delr*sin(abs(theta))4*delr*cos(theta
153 yy0+2*delr*cos(theta)4*delr*sin(abs(theta))4*delr*cos(theta
154 yy0];
155
156 line(massX,massY);
157
158 xlim([3,10]); ylim([10,3]);
159
160 set(gca,'DataAspectRatioMode','manual','DataAspectRatio',[1 1 1])
161 drawnow;
162 pause(.1);
163 end164
165 end
166
167 %
168 %
169 %
170 function testV2(len)
171
172 close all;
173
174 theta = 0;
175 figure;
176 set(gcf,'Units','normalized');
177 delr=0.3;
178 for r=1:len/3:len
179 cla;
180 [x,y] = nma_spring.makeV2(r,10,1);
181 line(x,y);
182 hold on;
183
184 %baseX = [2*delr*cos((pi/2)theta) 2*delr*sin(theta)];
185 %baseY = [2*delr*sin((pi/2)theta) 2*delr*cos(theta)];
186
187 baseX = [2*delr*sin(abs(theta)) 2*delr*sin(abs(theta))];
188 baseY = [2*
delr*
cos(theta) 2*
delr*
cos(theta)];
189 line(baseX,baseY);
190
191 triX=[baseX(1) ...
192 baseX(1) ...
193 30*delr*cos(theta)abs(baseX(1))...
194 baseX(1)];
195
196 triY=[baseY(1) ...
197 (30*delr*sin(abs(theta))+abs(baseY(1)))...
22
-
8/12/2019 About Pushbutton Stop
23/23
198 (30*delr*sin(abs(theta))+abs(baseY(1)))...
199 baseY(1)];
200
201 line(triX,triY);
202
203 L=sqrt(x(end)^2+y(end)^2);
204 xx0=L*cos(theta);205 yy0=L*sin(abs(theta));
206 massX=[xx0 ...
207 xx0+2*delr*sin(abs(theta)) ...
208 xx0+2*delr*sin(abs(theta))+4*delr*cos(theta) ...
209 xx0+2*delr*sin(abs(theta))+4*delr*cos(theta)4*delr*sin(abs(th
210 xx0+2*delr*sin(abs(theta))+4*delr*cos(theta)4*delr*sin(abs(th
211 xx0];
212
213 massY=[ yy0 ...
214 yy0+2*delr*cos(theta) ...
215 yy0+2*delr*cos(theta)4*delr*sin(abs(theta)) ...
216 yy0+2*delr*cos(theta)4*delr*sin(abs(theta))4*delr*cos(theta
217 yy0+2*delr*cos(theta)4*delr*sin(abs(theta))4*delr*cos(theta218 yy0];
219
220 line(massX,massY);
221
222 xlim([3,10]); ylim([10,3]);
223
224 set(gca,'DataAspectRatioMode','manual','DataAspectRatio',[1 1 1])
225 drawnow;
226 pause(.3);
227 end
228
229 end
230
231 end
232 end