TEXT   22

# CS

Guest on 25th November 2021 03:38:27 AM

1. %Open loop response of a DC Motor
2. clearvars
3. J = .01;
4. b = .1;
5. K = .01;
6. R = 1;
7. L = .5;
8. num = K;
9. den = [(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];
10. open_sys = tf(num,den);
11. lim = 0:.01:10;
12. S = stepinfo(open_sys);
13. plot(lim,step(open_sys,lim),'linewidth',1.6,'color',[1 1 1])
14. title("Max Speed "+getfield(S,'Peak')+" gained at "+getfield(S,'PeakTime')+ "s",'color',[1 1
15. 1])
16. ylabel('Speed of Rotation')
17. xlabel('Time (s)')
18.
19. %when R and V is varied:
20. clc;
21. clear all;
22. close all;
23. J=0.01;
24. b=0.1;
25. K=0.01;
26. R=1:1:5;
27. L=0.5;
28. num=K;
29. count = 0
30. for i=R
31. count = count+1;
32. den=[(J*L) ((J*i)+(L*b)) ((b*i)+K^2)];
33. open_sys = tf(num,den);
34. subplot(5,1,i);
35. step (open_sys,0:0.1:5);
36. stepinfo(open_sys)
37. Settle_time(count) = getfield(S,'SettlingTime');
39. end
40.
41. %Part2(1): Proportional Control
42. kp = 100;
43. closed_sys = feedback(open_sys*kp,1);
44. figure(2)
45. P_info=stepinfo(closed_sys)
46. step (closed_sys,0:0.01:3);
47. title ('Step Response Proportional Control')
48.
49. %Part2(2): Proportional-Integral Control
50. ki=200;
51. b=[kp ki];
52. a=[1 0];
53. kpki=tf(b,a);
54. closed_sys1 = feedback(open_sys*kpki,1);
55. figure(3)
56. PI_info=stepinfo(closed_sys1)
57. step (closed_sys1,0:0.01:3);
58. title ('Step Response Proportional Integral Control')
59.
60. %Part2(3): Proportional-Derivative Control
61. ki2=0;
62. kd=10;
63. b1=[kd kp ki2];
64. a1=[1 0];
65. kdkpki2=tf(b1,a1);
66. closed_sys2 = feedback(open_sys*kdkpki2,1);
67. figure(4)
68. PD_info=stepinfo(closed_sys2)
69. step (closed_sys2,0:0.01:3);
70. title ('Step Response Proportional Derivative Control')
71.
72. %Part2(4): Proportional-Derivative-Integral Control
73. ki3=200;
74. kd=10;
75. b2=[kd kp ki3];
76. a2=[1 0];
77. kdkpki3=tf(b2,a2);
78. closed_sys3 = feedback(open_sys*kdkpki3,1);
79. figure(5)
80. PID_info=stepinfo(closed_sys3)
81. step (closed_sys3,0:0.01:3);
82. title ('Step Response Proportional Derivative-Integral Control')
83.
84. 5. Is it possible to redesign the above PID controller with another set of Kp, Ki and Kd ? If possible, design the controller?
85. J=0.01;
86. b=0.1;
87. K=0.01;
88. R=1;
89. L=0.5;
90. num=K;
91. den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];
92. open_sys = tf(num,den);
93. KP = input('Enter the Proportional Gain, KP: ');
94. KI = input('Enter the Integral Gain, KI: ');
95. KD = input('Enter the Derivative Gain, KD: ');
96. b=[KD KP KI];
97. a=[1 0];
98. KPKDKI=tf(b,a);
99. closed_sys = feedback(open_sys*KPKDKI,1);
100. PID_info=stepinfo(closed_sys)
101. figure(1)
102. step (open_sys,0:0.01:3);
103. hold on;
104. step(closed_sys,0:0.01:3);
105. hold off
106. legend('Open loop response','PID Controller response')
107. title ('Step Response of Open Loop vs. Proportional Derivative-Integral Control');
108.
109. %%exp4
110. clc
111. clear all;
112. close all
113. J=3.2284E-6;
114. b=3.5077E-6;
115. K=0.0274;
116. R=4;
117. L=2.75E-6;
118. lob=K;
119. hor=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];
120. figure()
121. sys= tf(lob,hor);
122. feedbk_sys=feedback(K*sys,1);
123. num_pid=conv([1 5],[1 200]);
124. den_pid=[1 0];
125. pid_c=tf(num_pid,den_pid);
126. sys_pid=pid_c*sys;
127. figure()
128. rlocus(sys_pid,0:0.01:1);
129. axis([-500 50 -250 250]);
130. z = -log(2.5/100)/sqrt(pi^2 + (log(2.5/100))^2);
131. sgrid(z,0);
132. [k, poles]=rlocfind(sys_pid);
133. feedbk_sys_pid=feedback(sys_pid,1);
134. figure()
135. subplot(212),step(feedbk_sys_pid,0: 0.00001: 0.01),hold on;
136. title('Compensated System after pidcontroller'),ylim([0 1.5]);
137. subplot(211),step(feedbk_sys,0: 0.001: 10)
138. title('Uncompensated System before pidcontroller'),ylim([0 1.5]);
139. figure()
140. dis_sys=feedbk_sys_pid/(k*pid_c);
141. step(dis_sys,0: 0.001: 0.1);
142. title('Disturbance')
143. feedbk_info=stepinfo(k*feedbk_sys_pid)
144.
145. %% lab 5
146. clc
147. clear all;
148. close all;
149. %% lab report 1
150. num = [];
151. den = [-1 -2 -10];
152. sys = zpk(num,den,1);
153. figure(1)
154. rlocus(sys);
155. sgrid(.174,0);
156. [k1 ,poles1]=rlocfind(sys);
157. feedbk_sys=feedback(k1*sys,1);
158. figure(2)
159. step(feedbk_sys,0: .001: 10);
160. info1 = stepinfo(feedbk_sys);
161. Zc = 0.5;
162. num_pi = [1 Zc];
163. den_pi = [1 0];
164. PIcontroller=tf(num_pi,den_pi);
165. PI_sys=PIcontroller*sys;
166. figure(3)
167. rlocus(PI_sys);
168. sgrid(.174,0);
169. [k2 ,poles2]=rlocfind(PI_sys);
170. feedbk_PI_sys=feedback(k2*PI_sys,1);
171. figure(4)
172. step(feedbk_PI_sys,0: .001: 10);
173. info2 = stepinfo(feedbk_PI_sys);%pi controller
174. figure(5);
175. step(feedbk_sys,0: .001: 10)
176. hold on;
177. step(feedbk_PI_sys,0: .001: 10)
178. a=step(feedbk_sys,0: .001: 10);
179. b=step(feedbk_PI_sys,0: .001: 10);
180. error_old = 1-a(end)
181. new_error=1-b(end)
182.
183. clc
184. clear all;
185. close all;
186. %
187. num = [];
188. den = [-1 -2 -10];
189. sys = zpk(num,den,1);
190. figure(1)
191. rlocus(sys);
192. sgrid(.174,0);
193. [k1 ,poles1]=rlocfind(sys);
194. feedbk_sys=feedback(k1*sys,1);
195. %% lab report 2
196. pc3 = 0.01;
197. factor = 10;
198. p = step(feedbk_sys,0:10000);
199. error_old = 1-p(end);
200. kp_old = (1-error_old)/error_old;
201. error_new = error_old/factor;
202. kp_new = (1-error_new)/error_new;
203. zc3 = pc3 * kp_new/kp_old;
204. num_lag = [1 zc3];
205. den_lag = [1 pc3];
206. lagcontroller=tf(num_lag,den_lag);
207. lag_sys=lagcontroller*sys;
208. figure(5)
209. rlocus(lag_sys);
210. sgrid(.174,0);
211. [k3 ,poles3]=rlocfind(lag_sys);
212. feedbk_lag_sys=feedback(k3*lag_sys,1);
213. figure(6)
214. step(feedbk_sys,0: .001: 50);hold on;
215. step(feedbk_lag_sys,0: .001: 50);
216. info3 = stepinfo(feedbk_lag_sys);
217. info3
218.
219. %% %% 3 no: PD compensator
220. clc
221. clearvars
222. clear all;
223. close all;
224. t = 0:0.001:25;
225. num = [];
226. den = [0 -4 -6];
227. sys = zpk(num,den,1);% This is Open loop system
228. figure()
229. rlocus(sys),title('Uncompensated system');
230. axis([-8 8 -8 8]);
231. z = -log(16/100)/sqrt(pi^2 + (log(16/100))^2);
232. sgrid(z,0);
233. [k ,poles]=rlocfind(sys);
234. feedbk_sys=feedback(k*sys,1);%This is Close loop system
235. Zc = 3.0058987;% Location of zero for three-fold reduction in settling time
236. num_pd = [1 Zc];
237. den_pd = [0 1];
238. pd_controller=tf(num_pd,den_pd);
239. pd_sys = pd_controller*sys;
240. figure()
241. rlocus(pd_sys);
242. title('Compensated system');
243. axis([-7 1 -8 8]);
244. sgrid(z,0);
245. [k_pd ,poles_pd]=rlocfind(pd_sys);
246. feedbk_pd_sys=feedback(k_pd*pd_sys,1);
247. figure()
248. step(feedbk_sys,t);hold on;
249. step(feedbk_pd_sys,t);
250. legend('Uncompensated system','Compensated system');
251. uncompensated_data = stepinfo(feedbk_sys);
252. compensated_data = stepinfo(feedbk_pd_sys);
253.
254. %% %% 4 no: Lead Compensator-%% %%
255. clc;
256. clearvars
257. clear all;
258. close all;
259. t = 0:0.001:25;
260. num = [];
261. den = [0 -4 -6];
262. sys = zpk(num,den,1);% Open loop system
263. figure()
264. rlocus(sys),title('Uncompensated system before "lead c" used');
265. axis([-10 10 -8 8]);
266. z = -log(30/100)/sqrt(pi^2 + (log(30/100))^2);
267. sgrid(z,0);
268. [k ,poles]=rlocfind(sys);
269. feedbk_sys=feedback(k*sys,1);% Close loop system
270. Zc = 5;% Location of zero for reduction in settling time by a factor of 2
271. Pc = 42.96;% Location of pole for reduction in settling time by a factor of 2
276. figure()
278. axis([-50 50 -20 20]);sgrid(z,0);
281. figure()
282. step(feedbk_sys,t);hold on;
285. uncompensated_info = stepinfo(feedbk_sys);
287.
288. %% %% 5 ------Lead Compensator-------%% %%
289. clc
290. clearvars
291. clear all;
292. close all
293. t = 0:0.001:35;
294. num = [];
295. den = [0 -3 -6];
296. sys = zpk(num,den,1);% Open loop
297. figure()
298. rlocus(sys),title('Uncompensated system before lead being used');
299. axis([-10 10 -8 8]);
300. z = -log(20/100)/sqrt(pi^2 + (log(20/100))^2);
301. sgrid(z,0);
302. [k ,p]=rlocfind(sys);
303. feedbk_sys=feedback(k*sys,1);% Close loop system
304. Zc = 5;% Location of zero for reduction in settling time by a factor of 2
305. Pc = 140% Location of pole for settling time 2sec
310. figure()
312. axis([-10 10 -5 5]);
313. sgrid(z,0);
316. figure()
317. step(feedbk_sys,t);hold on;
319. legend('Uncompensated system','Compensated system after lead compensation');
320. uncompensated_info = stepinfo(feedbk_sys);
322. td = 0:0.1:900;
323. figure(4)
324. subplot(331);
325. step(sys,td);
326. stp1 = step(sys,td);% For Uncompensated
327. title('uncompensated');
328. SteadyStateError_step1 = 1 - stp1(end)
329. s = tf('s');
330. subplot(334);
331. step(feedbk_sys/s,td);
332. title('ramp for compensated system');
333. rmp1 = step(feedbk_sys/s,td);
334. SteadyStateError_ramp1 = td(end) - rmp1(end)
335. subplot(332);
336. step(feedbk_sys,td);
337. title('for compensated');
338. stp2 = step(feedbk_sys,td);% For Compensated
339. SteadyStateError_step2 = 1 - stp2(end)
340. s = tf('s');
341. subplot(335);
342. step(feedbk_sys/s,td);
343. rmp2 = step(feedbk_sys/s,td);
344. title('ramp response for compensated system');
345. SteadyStateError_ramp2 = td(end) - rmp2(end)
346.
347. clc
348. clearvars
349. close all
350. clear all;
351. t = 0:0.001:33;
352. num = [];
353. den = [0 -3 -6];
354. sys = zpk(num,den,1);
355. figure()
356. rlocus(sys),title('Uncompensated system');
357. axis([-10 10 -8 8]);
358. z = -log(20/100)/sqrt(pi^2 + (log(20/100))^2);
359. sgrid(z,0);
360. [k ,p]=rlocfind(sys);
361. f_backk_system=feedback(k*sys,1);% Close loop system
362. s = tf('s');
363. td = 0:0.1:900;
364. stp1 = step(sys,td);% For Uncompensated
365. SteadyStateError_step1 = 1 - stp1(end)
366. rmp1 = step(f_backk_system/s,td);
367. SteadyStateError_ramp1 = td(end) - rmp1(end)
368. Zc = [2 3.5];% Location of zero after reduction in settling time by a factor of 2
369. Pc = [8 20]% Location of pole when settling time is 2sec
370. uncompensated_info = stepinfo(f_backk_system)
371. for i = 1:2
376. figure()
378. sgrid(z,0);
381. figure()
382. step(f_backk_system,t);hold on;
384. legend('Uncompensated system response','Compensated system response');
386. stp2 = step(f_backk_system,td);% For Compensated
387. SteadyStateError_step2 = 1 - stp2(end)
388. rmp2 = step(f_backk_system/s,td);
389. SteadyStateError_ramp2 = td(end) - rmp2(end)
390. end
391.
392. %% %% 7------
393. clc
394. clear all;
395. close all;
396. %% Open loop
397. t = 0:0.001:50;
398. num = [];
399. den = [0 -3 -6];
400. sys = zpk(num,den,1);% Open loop system
401. figure()
402. rlocus(sys),title('before compensation');
403. axis([-10 10 -8 8]);
404. z = -log(20/100)/sqrt(pi^2 + (log(20/100))^2);
405. sgrid(z,0);
406. [k ,p]=rlocfind(sys);
407. feedbk_sys=feedback(k*sys,1);% Close loop system
409. Zc = 5;
410. Pc = 140
415. figure()
417. axis([-10 10 -5 5]);
418. sgrid(z,0);
422. Zc_pi = 5.9;
423. num_pi = [1 Zc];
424. den_pi = [1 0];
425. pi = tf(num_pi/den_pi);
427. figure()
429. axis([-10 10 -5 5]);
430. sgrid(z,0);
433. figure()
435. s = tf('s');
436. figure()
439. td = 0:10000;
441. SteadyStateError_step = 1 - stp(end)
443. SteadyStateError_ramp = td(end) - rmp(end)
444.
445. %% question:8
446. clc
447. clearvars
448. close all
449. t = 0:0.001:55;
450. num = [];
451. den = [0 -3 -6];
452. sys = zpk(num,den,1);% Open loop system
453. figure()
454. rlocus(sys),title('Uncompensated system');
455. axis([-10 10 -8 8]);
456. z = -log(20/100)/sqrt(pi^2 + (log(20/100))^2);
457. sgrid(z,0);
458. [k ,p]=rlocfind(sys);
459. feedbk_sys=feedback(k*sys,1);% Close loop system
461. Zc = 5;% Location of zero for reduction in settling time by a factor of 2
462. Pc = 140;% Location of pole for settling time 2sec
467. figure()
469. axis([-10 10 -5 5]);
470. sgrid(z,0);
474. Zc_pi = [6.1 2.9]
475. for i = 1:2
476. fprintf('When Zc = ',Zc_pi(i));
477. num_pi = [1 Zc];
478. den_pi = [1 0];
479. pi = tf(num_pi/den_pi);
481. figure()
483. axis([-10 10 -5 5]);
484. sgrid(z,0);
487. figure()
489. s = tf('s');
490. figure()