-
Notifications
You must be signed in to change notification settings - Fork 4
/
Standard_Analysis.m
723 lines (722 loc) · 35.1 KB
/
Standard_Analysis.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
%%Delete the results of previous simulation, if any
if(exist('Results.txt'))
delete('Results.txt');
elseif(exist('Results.xls'))
delete('Results.xls');
if(exist('MC_values_ode.txt'))
delete('MC_values_ode.txt');
end
delete('Voltages_graph.fig');
if(exist('Currents_graph.fig'))
delete('Currents_graph.fig');
end
end
%%-----------------------------------------------------------------------------
prompt='Enter the file name - ';
fname=input(prompt,'s');
netlist_fileID=fopen(fname);
netlist=textscan(netlist_fileID,'%s %s %s %s %s %s');
fclose(netlist_fileID);
fileID1=fopen('Element_indep.txt','wt+'); %Create an empty text file Element_indep.txt for passive elements and independent sources
fileID2=fopen('VCVS.txt','wt+'); %Create an empty text file VCVS.txt for voltage controlled voltage sources
fileID3=fopen('VCCS.txt','wt+'); %Create an empty text file VCCS.txt for voltage controlled current sources
fileID4=fopen('CCVS.txt','wt+'); %Create an empty text file CCVS.txt for current controlled voltage sources
fileID5=fopen('CCCS.txt','wt+'); %Create an empty text file CCCS.txt for current controlled current sources
%%-----------------------------------------------------------------------------
%%Initialize
num_Elements=0; %Number of passive elements
num_V=0; %Number of independent voltage sources
num_I=0; %Number of independent current sources
num_Nodes=0; %Number of nodes, excluding ground (node 0)
num_VCVS=0; %Number of voltage controlled voltage sources
num_VCCS=0; %Number of voltage controlled current sources
num_CCVS=0; %Number of current controlled voltage sources
num_CCCS=0; %Number of current controlled current sources
num_L=0; %Number of inductors
%%-----------------------------------------------------------------------------
for i=1:length(netlist{1})
s=netlist{1}{i};
switch(s(1))
case{'R','L','C','V','I'} %For passive elements and independent sources
fprintf(fileID1,[netlist{1}{i} ' ' netlist{2}{i} ' ' ...
netlist{3}{i} ' ' netlist{4}{i} '\n']);
case{'E'} %For voltage controlled voltage sources
fprintf(fileID2,[netlist{1}{i} ' ' netlist{2}{i} ' ' ...
netlist{3}{i} ' ' netlist{4}{i} ' ' netlist{5}{i} ' ' ...
netlist{6}{i} '\n']);
case{'G'} %For voltage controlled current sources
fprintf(fileID3,[netlist{1}{i} ' ' netlist{2}{i} ' ' ...
netlist{3}{i} ' ' netlist{4}{i} ' ' netlist{5}{i} ' ' ...
netlist{6}{i} '\n']);
case{'H'} %For current controlled voltage sources
fprintf(fileID4,[netlist{1}{i} ' ' netlist{2}{i} ' ' ...
netlist{3}{i} ' ' netlist{4}{i} ' ' netlist{5}{i} '\n']);
case{'F'} %For current controlled current sources
fprintf(fileID5,[netlist{1}{i} ' ' netlist{2}{i} ' ' ...
netlist{3}{i} ' ' netlist{4}{i} ' ' netlist{5}{i} '\n']);
end
end
%%-----------------------------------------------------------------------------
%%Read the data from Element_indep.txt text file
[Name,N1,N2,value]=textread('Element_indep.txt','%s %s %s %s');
for i=1:length(Name)
switch(Name{i}(1))
case{'R','L','C'}
num_Elements=num_Elements+1;
Element(num_Elements).Name=Name{i};
Element(num_Elements).Node1=str2num(N1{i});
Element(num_Elements).Node2=str2num(N2{i});
Element(num_Elements).Value=str2double(value{i});
if(Name{i}(1)=='L')
num_L=num_L+1;
Inductor(num_L).Name=Name{i};
Inductor(num_L).N1=str2num(N1{i});
Inductor(num_L).N2=str2num(N2{i});
Inductor(num_L).Value=str2double(value{i});
end
case{'V'}
num_V=num_V+1;
Volt_source(num_V).Name=Name{i};
Volt_source(num_V).Node1=str2num(N1{i});
Volt_source(num_V).Node2=str2num(N2{i});
Volt_source(num_V).Value=str2double(value{i});
case{'I'}
num_I=num_I+1;
Current_source(num_I).Name=Name{i};
Current_source(num_I).Node1=str2num(N1{i});
Current_source(num_I).Node2=str2num(N2{i});
Current_source(num_I).Value=str2double(value{i});
end
num_Nodes=max(str2num(N1{i}),max(str2num(N2{i}),num_Nodes));
end
%%-----------------------------------------------------------------------------
%%Read the data from VCVS.txt text file
[Name,N1,N2,NC1,NC2,Gain]=textread('VCVS.txt','%s %s %s %s %s %s');
num_VCVS=length(Name);
for i=1:num_VCVS
VCVS(i).Name=Name{i};
VCVS(i).N1=str2num(N1{i});
VCVS(i).N2=str2num(N2{i});
VCVS(i).NC1=str2num(NC1{i});
VCVS(i).NC2=str2num(NC2{i});
VCVS(i).Gain=str2double(Gain{i});
num_Nodes=max(str2num(N1{i}),max(str2num(N2{i}),num_Nodes));
end
%%-----------------------------------------------------------------------------
%%Read the data from VCCS.txt text file
[Name,N1,N2,NC1,NC2,Transconductance]=textread('VCCS.txt','%s %s %s %s %s %s');
num_VCCS=length(Name);
for i=1:num_VCCS
VCCS(i).Name=Name{i};
VCCS(i).N1=str2num(N1{i});
VCCS(i).N2=str2num(N2{i});
VCCS(i).NC1=str2num(NC1{i});
VCCS(i).NC2=str2num(NC2{i});
VCCS(i).Transconductance=str2double(Transconductance{i});
num_Nodes=max(str2num(N1{i}),max(str2num(N2{i}),num_Nodes));
end
%%-----------------------------------------------------------------------------
%%Read the data from CCVS.txt text file
[Name,N1,N2,Vcontrol,Transresistance]=textread('CCVS.txt','%s %s %s %s %s');
num_CCVS=length(Name);
for i=1:num_CCVS
CCVS(i).Name=Name{i};
CCVS(i).N1=str2num(N1{i});
CCVS(i).N2=str2num(N2{i});
CCVS(i).Vcontrol=Vcontrol{i};
CCVS(i).Transresistance=str2double(Transresistance{i});
num_Nodes=max(str2num(N1{i}),max(str2num(N2{i}),num_Nodes));
end
%%-----------------------------------------------------------------------------
%%Read the data from CCCS.txt text file
[Name,N1,N2,Vcontrol,Gain]=textread('CCCS.txt','%s %s %s %s %s');
num_CCCS=length(Name);
for i=1:num_CCCS
CCCS(i).Name=Name{i};
CCCS(i).N1=str2num(N1{i});
CCCS(i).N2=str2num(N2{i});
CCCS(i).Vcontrol=Vcontrol{i};
CCCS(i).Gain=str2double(Gain{i});
num_Nodes=max(str2num(N1{i}),max(str2num(N2{i}),num_Nodes));
end
%%-----------------------------------------------------------------------------
%%Close the no longer required text files and then delete them
fclose(fileID1);
fclose(fileID2);
fclose(fileID3);
fclose(fileID4);
fclose(fileID5);
delete('Element_indep.txt');
delete('VCVS.txt');
delete('VCCS.txt');
delete('CCVS.txt');
delete('CCCS.txt');
%%-----------------------------------------------------------------------------
%%Create the equations for the independent voltage sources and apply KCL at the nodes
node_equation=cell(num_Nodes,1);
volt_equation=cell(num_V,1);
for i=1:num_V
switch((Volt_source(i).Node1==0)||(Volt_source(i).Node2==0))
case{1}
if(Volt_source(i).Node1==0)
volt=['v_' num2str(Volt_source(i).Node2) '=' '-' num2str(Volt_source(i).Value)];
node_equation{Volt_source(i).Node2}=[node_equation{Volt_source(i).Node2} ...
'-' 'i_' Volt_source(i).Name];
else
volt=['v_' num2str(Volt_source(i).Node1) '=' num2str(Volt_source(i).Value)];
node_equation{Volt_source(i).Node1}=[node_equation{Volt_source(i).Node1} ...
'+' 'i_' Volt_source(i).Name];
end
volt_equation{i}=volt;
case{0}
volt=['v_' num2str(Volt_source(i).Node1) '-' ...
'v_' num2str(Volt_source(i).Node2) '=' num2str(Volt_source(i).Value)];
volt_equation{i}=volt;
node_equation{Volt_source(i).Node1}=[node_equation{Volt_source(i).Node1} ...
'+' 'i_' Volt_source(i).Name];
node_equation{Volt_source(i).Node2}=[node_equation{Volt_source(i).Node2} ...
'-' 'i_' Volt_source(i).Name];
end
end
%%-----------------------------------------------------------------------------
%%Create the equations for the voltage controlled voltage sources and apply KCL at the nodes
VCVS_equation=cell(num_VCVS,1);
for i=1:num_VCVS
switch((VCVS(i).N1==0)||(VCVS(i).N2==0))
case{1}
if(VCVS(i).N1==0)
switch((VCVS(i).NC1==0)||(VCVS(i).NC2==0))
case{1}
if(VCVS(i).NC1==0)
volt=['-' 'v_' num2str(VCVS(i).N2) '-' num2str(VCVS(i).Gain) ...
'*' '(' '-' 'v_' num2str(VCVS(i).NC2) ')'];
else
volt=['-' 'v_' num2str(VCVS(i).N2) '-' num2str(VCVS(i).Gain) ...
'*' '(' 'v_' num2str(VCVS(i).NC1) ')'];
end
case{0}
volt=['-' 'v_' num2str(VCVS(i).N2) '-' num2str(VCVS(i).Gain) ...
'*' '(' 'v_' num2str(VCVS(i).NC1) '-' 'v_' num2str(VCVS(i).NC2) ')'];
end
node_equation{VCVS(i).N2}=[node_equation{VCVS(i).N2} '-' 'i_' VCVS(i).Name];
else
switch((VCVS(i).NC1==0)||(VCVS(i).NC2==0))
case{1}
if(VCVS(i).NC1==0)
volt=['v_' num2str(VCVS(i).N1) '-' num2str(VCVS(i).Gain) ...
'*' '(' '-' 'v_' num2str(VCVS(i).NC2) ')'];
else
volt=['v_' num2str(VCVS(i).N1) '-' num2str(VCVS(i).Gain) ...
'*' '(' 'v_' num2str(VCVS(i).NC1) ')'];
end
case{0}
volt=['v_' num2str(VCVS(i).N1) '-' num2str(VCVS(i).Gain) ...
'*' '(' 'v_' num2str(VCVS(i).NC1) '-' 'v_' num2str(VCVS(i).NC2) ')'];
end
node_equation{VCVS(i).N1}=[node_equation{VCVS(i).N1} '+' 'i_' VCVS(i).Name];
end
case{0}
switch((VCVS(i).NC1==0)||(VCVS(i).NC2==0))
case{1}
if(VCVS(i).NC1==0)
volt=['v_' num2str(VCVS(i).N1) '-' 'v_' num2str(VCVS(i).N2) '-' ...
num2str(VCVS(i).Gain) '*' '(' '-' 'v_' num2str(VCVS(i).NC2) ')'];
else
volt=['v_' num2str(VCVS(i).N1) '-' 'v_' num2str(VCVS(i).N2) '-' ...
num2str(VCVS(i).Gain) '*' '(' 'v_' num2str(VCVS(i).NC1) ')'];
end
case{0}
volt=['v_' num2str(VCVS(i).N1) '-' 'v_' num2str(VCVS(i).N2) '-' ...
num2str(VCVS(i).Gain) '*' '(' 'v_' num2str(VCVS(i).NC1) '-' 'v_' num2str(VCVS(i).NC2) ')'];
end
node_equation{VCVS(i).N1}=[node_equation{VCVS(i).N1} '+' 'i_' VCVS(i).Name];
node_equation{VCVS(i).N2}=[node_equation{VCVS(i).N2} '-' 'i_' VCVS(i).Name];
end
VCVS_equation{i}=volt;
end
%%-----------------------------------------------------------------------------
%%Create the equations for the current controlled voltage sources and apply KCL at the nodes
CCVS_equation=cell(num_CCVS,1);
for i=1:num_CCVS
switch((CCVS(i).N1==0)||(CCVS(i).N2==0))
case{1}
if(CCVS(i).N1==0)
volt=['v_' num2str(CCVS(i).N2) '+' '(' num2str(CCVS(i).Transresistance) '*' 'i_' CCVS(i).Vcontrol ')'];
node_equation{CCVS(i).N2}=[node_equation{CCVS(i).N2} ...
'-' 'i_' CCVS(i).Name];
else
volt=['v_' num2str(CCVS(i).N1) '-' '(' num2str(CCVS(i).Transresistance) '*' 'i_' CCVS(i).Vcontrol ')'];
node_equation{CCVS(i).N1}=[node_equation{CCVS(i).N1} ...
'+' 'i_' CCVS(i).Name];
end
CCVS_equation{i}=volt;
case{0}
volt=['v_' num2str(CCVS(i).N1) '-' ...
'v_' num2str(CCVS(i).N2) '-' '(' num2str(CCVS(i).Transresistance) '*' 'i_' CCVS(i).Vcontrol ')'];
CCVS_equation{i}=volt;
node_equation{CCVS(i).N1}=[node_equation{CCVS(i).N1} ...
'+' 'i_' CCVS(i).Name];
node_equation{CCVS(i).N2}=[node_equation{CCVS(i).N2} ...
'-' 'i_' CCVS(i).Name];
end
end
%%-----------------------------------------------------------------------------
solver_flag=0; %A flag used for deciding which solver to finally use
%solver_flag=0 => Purely resistive circuit, use solve for the equations
%solver_flag=1 => Pure C, pure L, LC, RC, RL or RLC circuit, use ode15i for the equations
%%-----------------------------------------------------------------------------
%%Add the passive element currents using KCL to the node equations, and make the equations for inductors
L_equation=cell(num_L,1);
L_ctr=0;
for i=1:num_Elements
switch(Element(i).Name(1))
case{'R'}
switch((Element(i).Node1==0)||(Element(i).Node2==0))
case{0}
node_equation{Element(i).Node1}=[node_equation{Element(i).Node1} '+' '(' ...
'v_' num2str(Element(i).Node2) '-' 'v_' ...
num2str(Element(i).Node1) ')' '/' num2str(Element(i).Value)];
node_equation{Element(i).Node2}=[node_equation{Element(i).Node2} '+' '(' ...
'v_' num2str(Element(i).Node1) '-' 'v_' ...
num2str(Element(i).Node2) ')' '/' num2str(Element(i).Value)];
case{1}
if(Element(i).Node1==0)
node_equation{Element(i).Node2}=[node_equation{Element(i).Node2} ...
'-' '(' 'v_' num2str(Element(i).Node2) ')' '/' num2str(Element(i).Value)];
else
node_equation{Element(i).Node1}=[node_equation{Element(i).Node1} ...
'-' '(' 'v_' num2str(Element(i).Node1) ')' '/' num2str(Element(i).Value)];
end
end
case{'C'}
if(solver_flag==0)
solver_flag=1;
end
switch((Element(i).Node1==0)||(Element(i).Node2==0))
case{0}
node_equation{Element(i).Node1}=[node_equation{Element(i).Node1} ...
'+' num2str(Element(i).Value) '*' '(' 'vp(' num2str(Element(i).Node2) ')' ...
'-' 'vp(' num2str(Element(i).Node1) ')' ')'];
node_equation{Element(i).Node2}=[node_equation{Element(i).Node2} ...
'+' num2str(Element(i).Value) '*' '(' 'vp(' num2str(Element(i).Node1) ')' ...
'-' 'vp(' num2str(Element(i).Node2) ')' ')'];
case{1}
if(Element(i).Node1==0)
node_equation{Element(i).Node2}=[node_equation{Element(i).Node2} ...
'-' num2str(Element(i).Value) '*' '(' 'vp(' num2str(Element(i).Node2) ')' ')'];
else
node_equation{Element(i).Node1}=[node_equation{Element(i).Node1} ...
'-' num2str(Element(i).Value) '*' '(' 'vp(' num2str(Element(i).Node1) ')' ')'];
end
end
case{'L'}
if(solver_flag==0)
solver_flag=1;
end
L_ctr=L_ctr+1;
switch((Element(i).Node1==0)||(Element(i).Node2==0))
case{0}
node_equation{Element(i).Node1}=[node_equation{Element(i).Node1} '-' 'i_' Element(i).Name];
node_equation{Element(i).Node2}=[node_equation{Element(i).Node2} '+' 'i_' Element(i).Name];
L_equation{L_ctr}=['v_' num2str(Element(i).Node1) '-' 'v_' num2str(Element(i).Node2) '-' ...
'(' num2str(Element(i).Value) '*' 'ip(' num2str(L_ctr) ')' ')'];
case{1}
if(Element(i).Node1==0)
node_equation{Element(i).Node2}=[node_equation{Element(i).Node2} '+' 'i_' Element(i).Name];
L_equation{L_ctr}=['-' 'v_' num2str(Element(i).Node2) '-' ...
'(' num2str(Element(i).Value) '*' 'ip(' num2str(L_ctr) ')' ')'];
else
node_equation{Element(i).Node1}=[node_equation{Element(i).Node1} '-' 'i_' Element(i).Name];
L_equation{L_ctr}=['v_' num2str(Element(i).Node1) '-' ...
'(' num2str(Element(i).Value) '*' 'ip(' num2str(L_ctr) ')' ')'];
end
end
end
end
%%-----------------------------------------------------------------------------
%%Add the independent current sources using KCL to the node equations
for i=1:num_I
switch((Current_source(i).Node1==0)||(Current_source(i).Node2==0))
case{1}
if(Current_source(i).Node1==0)
node_equation{Current_source(i).Node2}=[node_equation{Current_source(i).Node2} ...
'+' num2str(Current_source(i).Value)];
else
node_equation{Current_source(i).Node1}=[node_equation{Current_source(i).Node1} ...
'-' num2str(Current_source(i).Value)];
end
case{0}
node_equation{Current_source(i).Node1}=[node_equation{Current_source(i).Node1} ...
'-' num2str(Current_source(i).Value)];
node_equation{Current_source(i).Node2}=[node_equation{Current_source(i).Node2} ...
'+' num2str(Current_source(i).Value)];
end
end
%%-----------------------------------------------------------------------------
%%Next, add the voltage controlled current sources using KCL to the node equations
for i=1:num_VCCS
switch((VCCS(i).N1==0)||(VCCS(i).N2==0))
case{1}
if(VCCS(i).N1==0)
switch((VCCS(i).NC1==0)||(VCCS(i).NC2==0))
case{1}
if(VCCS(i).NC1==0)
node_equation{VCCS(i).N2}=[node_equation{VCCS(i).N2} '+' ...
num2str(VCCS(i).Transconductance) '*' '(' '-' 'v_' num2str(VCCS(i).NC2) ')'];
else
node_equation{VCCS(i).N2}=[node_equation{VCCS(i).N2} '+' ...
num2str(VCCS(i).Transconductance) '*' '(' 'v_' num2str(VCCS(i).NC1) ')'];
end
case{0}
node_equation{VCCS(i).N2}=[node_equation{VCCS(i).N2} '+' ...
num2str(VCCS(i).Transconductance) '*' '(' 'v_' num2str(VCCS(i).NC1) '-' ...
'v_' num2str(VCCS(i).NC2) ')'];
end
else
switch((VCCS(i).NC1==0)||(VCCS(i).NC2==0))
case{1}
if(VCCS(i).NC1==0)
node_equation{VCCS(i).N1}=[node_equation{VCCS(i).N1} '-' ...
num2str(VCCS(i).Transconductance) '*' '(' '-' 'v_' num2str(VCCS(i).NC2) ')'];
else
node_equation{VCCS(i).N1}=[node_equation{VCCS(i).N1} '-' ...
num2str(VCCS(i).Transconductance) '*' '(' 'v_' num2str(VCCS(i).NC1) ')'];
end
case{0}
node_equation{VCCS(i).N1}=[node_equation{VCCS(i).N1} '-' ...
num2str(VCCS(i).Transconductance) '*' '(' 'v_' num2str(VCCS(i).NC1) ...
'-' 'v_' num2str(VCCS(i).NC2) ')'];
end
end
case{0}
switch((VCCS(i).NC1==0)||(VCCS(i).NC2==0))
case{1}
if(VCCS(i).NC1==0)
node_equation{VCCS(i).N1}=[node_equation{VCCS(i).N1} '-' ...
num2str(VCCS(i).Transconductance) '*' '(' '-' 'v_' num2str(VCCS(i).NC2) ')'];
node_equation{VCCS(i).N2}=[node_equation{VCCS(i).N2} '+' ...
num2str(VCCS(i).Transconductance) '*' '(' '-' 'v_' num2str(VCCS(i).NC2) ')'];
else
node_equation{VCCS(i).N1}=[node_equation{VCCS(i).N1} '-' ...
num2str(VCCS(i).Transconductance) '*' '(' 'v_' num2str(VCCS(i).NC1) ')'];
node_equation{VCCS(i).N2}=[node_equation{VCCS(i).N2} '+' ...
num2str(VCCS(i).Transconductance) '*' '(' 'v_' num2str(VCCS(i).NC1) ')'];
end
case{0}
node_equation{VCCS(i).N1}=[node_equation{VCCS(i).N1} '-' ...
num2str(VCCS(i).Transconductance) '*' '(' 'v_' num2str(VCCS(i).NC1) '-' ...
'v_' num2str(VCCS(i).NC2) ')'];
node_equation{VCCS(i).N2}=[node_equation{VCCS(i).N2} '+' ...
num2str(VCCS(i).Transconductance) '*' '(' 'v_' num2str(VCCS(i).NC1) '-' ...
'v_' num2str(VCCS(i).NC2) ')'];
end
end
end
%%-----------------------------------------------------------------------------
%%Finally, add the current controlled current sources using KCL to the node equations
for i=1:num_CCCS
switch((CCCS(i).N1==0)||(CCCS(i).N2==0))
case{1}
if(CCCS(i).N1==0)
node_equation{CCCS(i).N2}=[node_equation{CCCS(i).N2} ...
'+' '(' num2str(CCCS(i).Gain) '*' 'i_' CCCS(i).Vcontrol ')'];
else
node_equation{CCCS(i).N1}=[node_equation{CCCS(i).N1} ...
'-' '(' num2str(CCCS(i).Gain) '*' 'i_' CCCS(i).Vcontrol ')'];
end
case{0}
node_equation{CCCS(i).N1}=[node_equation{CCCS(i).N1} ...
'-' '(' num2str(CCCS(i).Gain) '*' 'i_' CCCS(i).Vcontrol ')'];
node_equation{CCCS(i).N2}=[node_equation{CCCS(i).N2} ...
'+' '(' num2str(CCCS(i).Gain) '*' 'i_' CCCS(i).Vcontrol ')'];
end
end
%%-----------------------------------------------------------------------------
%%If solver_flag=0 (purely resistive circuit), add the RHS('=0') to each
%%nodal KCL equation, to each VCVS equation, and to each CCVS equation
if(solver_flag==0)
for i=1:length(node_equation)
node_equation{i}=[node_equation{i} '=' '0'];
end
for i=1:length(VCVS_equation)
VCVS_equation{i}=[VCVS_equation{i} '=' '0'];
end
for i=1:length(CCVS_equation)
CCVS_equation{i}=[CCVS_equation{i} '=' '0'];
end
%%Else if solver_flag=1 (Pure C, pure L, LC, RC, RL or RLC circuit), do NOT add the RHS ('=0')
%%to each nodal KCL equation, to each VCVS equation, to each CCVS equation and to each inductor equation, instead replace
%%the node voltage terms v_1,v_2,...v_num_Nodes in LHS of all the equations with v(1),v(2),...v(num_Nodes) respectively,
%%modify each independent voltage source equation to only LHS [no RHS ('=0')] (similar to all the other equations),
%%also replace the independent voltage source current terms with v(num_Nodes+j) (j=1:num_V)
%%VCVS current terms with v(num_Nodes+num_V+j) (j=1:num_VCVS)
%%CCVS current terms with v(num_Nodes+num_V+num_VCVS+j) (j=1:num_CCVS)
%%and inductor current terms with v(num_Nodes+num_V+num_VCVS+num_CCVS+j) (j=1:num_L)
elseif(solver_flag==1)
for i=1:num_Nodes %For each nodal KCL equation (only LHS)
for j=1:num_Nodes
node_equation{i}=strrep(node_equation{i},['v_' num2str(j)],['v(' num2str(j) ')']);
end
for j=1:num_V
node_equation{i}=strrep(node_equation{i},['i_' Volt_source(j).Name],['v(' num2str(num_Nodes+j) ')']);
end
for j=1:num_VCVS
node_equation{i}=strrep(node_equation{i},['i_' VCVS(j).Name],['v(' num2str(num_Nodes+num_V+j) ')']);
end
for j=1:num_CCVS
node_equation{i}=strrep(node_equation{i},['i_' CCVS(j).Name],['v(' num2str(num_Nodes+num_V+num_VCVS+j) ')']);
end
for j=1:num_L
node_equation{i}=strrep(node_equation{i},['i_' Inductor(j).Name],['v(' num2str(num_Nodes+num_V+num_VCVS+num_CCVS+j) ')']);
end
end
for i=1:num_V %For each independent voltage source equation
for j=1:num_Nodes
volt_equation{i}=strrep(volt_equation{i},['v_' num2str(j)],['v(' num2str(j) ')']);
end
volt_equation{i}=strrep(volt_equation{i},'=','-'); %Modify each independent voltage source equation to only LHS [no RHS ('=0')]
end
for i=1:num_VCVS %For each VCVS equation (only LHS)
for j=1:num_Nodes
VCVS_equation{i}=strrep(VCVS_equation{i},['v_' num2str(j)],['v(' num2str(j) ')']);
end
end
for i=1:num_CCVS %For each CCVS equation (only LHS)
for j=1:num_Nodes
CCVS_equation{i}=strrep(CCVS_equation{i},['v_' num2str(j)],['v(' num2str(j) ')']);
end
for j=1:num_V
CCVS_equation{i}=strrep(CCVS_equation{i},['i_' Volt_source(j).Name],['v(' num2str(num_Nodes+j) ')']);
end
for j=1:num_VCVS
CCVS_equation{i}=strrep(CCVS_equation{i},['i_' VCVS(j).Name],['v(' num2str(num_Nodes+num_V+j) ')']);
end
for j=1:num_CCVS
CCVS_equation{i}=strrep(CCVS_equation{i},['i_' CCVS(j).Name],['v(' num2str(num_Nodes+num_V+num_VCVS+j) ')']);
end
end
for i=1:num_L %For each inductor equation (only LHS)
for j=1:num_Nodes
L_equation{i}=strrep(L_equation{i},['v_' num2str(j)],['v(' num2str(j) ')']);
end
end
end
%%-----------------------------------------------------------------------------
eqn=cell(num_Nodes+num_V+num_VCVS+num_CCVS+num_L,1);
for i=1:num_Nodes
eqn{i}=evalin(symengine,node_equation{i});
end
for i=1:num_V
eqn{num_Nodes+i}=evalin(symengine,volt_equation{i});
end
for i=1:num_VCVS
eqn{num_Nodes+num_V+i}=evalin(symengine,VCVS_equation{i});
end
for i=1:num_CCVS
eqn{num_Nodes+num_V+num_VCVS+i}=evalin(symengine,CCVS_equation{i});
end
for i=1:num_L
eqn{num_Nodes+num_V+num_VCVS+num_CCVS+i}=evalin(symengine,L_equation{i});
end
%%-----------------------------------------------------------------------------
switch(solver_flag)
case{0}
%Create the symbolic variables for node voltages and currents through voltage sources
variables='syms';
for i=1:num_Nodes
variables=[variables ' ' 'v_' num2str(i)];
end
for i=1:num_V
variables=[variables ' ' 'i_' Volt_source(i).Name];
end
for i=1:num_VCVS
variables=[variables ' ' 'i_' VCVS(i).Name];
end
for i=1:num_CCVS
variables=[variables ' ' 'i_' CCVS(i).Name];
end
eval(variables);
%----------------------------------------------
%Create a row vector var of the symbolic variables created above - to be used in solve
var_string=['var=[' variables(6:end) ']'];
eval(var_string);
%----------------------------------------------
%Create the symbolic variables for the symbolic equations
equations='syms';
for i=1:(num_Nodes+num_V+num_VCVS+num_CCVS)
equations=[equations ' ' 'eqn' num2str(i)];
end
eval(equations);
%----------------------------------------------
%Create a row vector eqn_solve of the equation symbolic variables
interm_string=['eqn_solve=[' equations(6:end) ']'];
eval(interm_string);
%----------------------------------------------
%Assign the equation symbolic variables with the corresponding symbolic equations
for i=1:(num_Nodes+num_V+num_VCVS+num_CCVS)
eqn_string=['eqn' num2str(i) '=' 'eqn{' num2str(i) '}'];
eval(eqn_string);
end
%----------------------------------------------
%Solve the symbolic linear equations using solve
sol=solve(eval(eqn_solve),var);
%Note :- We use eval(eqn_solve) to substitute the symbolic equation associated with
%each equation symbolic variable
%----------------------------------------------
F=fopen('Results.txt','wt+'); %Create an empty text file Results.txt
date=datetime('now');
date_string=datestr(date);
fprintf(F,date_string);
fprintf(F,'\n');
fprintf(F,['File name : ' fname]);
fprintf(F,'\n');
fprintf(F,'---------------------------------------------------------------------------- \n');
fprintf(F,'NODE VOLTAGES \n');
for i=1:num_Nodes
fprintf(F,['v_' num2str(i) ' = ']);
fprintf(F,num2str(eval(eval(['sol.v_' num2str(i)]))));
fprintf(F,'\n');
end
fprintf(F,'---------------------------------------------------------------------------- \n');
if(num_V~=0)
fprintf(F,'CURRENTS THROUGH INDEPENDENT VOLTAGE SOURCES (NEGATIVE TO POSITIVE TERMINAL) \n');
for i=1:num_V
fprintf(F,['i_' Volt_source(i).Name ' = ']);
fprintf(F,num2str(eval(eval(['sol.i_' Volt_source(i).Name]))));
fprintf(F,'\n');
end
fprintf(F,'---------------------------------------------------------------------------- \n');
end
if(num_VCVS~=0)
fprintf(F,'CURRENTS THROUGH VOLTAGE CONTROLLED VOLTAGE SOURCES (NEGATIVE TO POSITIVE TERMINAL) \n');
for i=1:num_VCVS
fprintf(F,['i_' VCVS(i).Name ' = ']);
fprintf(F,num2str(eval(eval(['sol.i_' VCVS(i).Name]))));
fprintf(F,'\n');
end
fprintf(F,'---------------------------------------------------------------------------- \n');
end
if(num_CCVS~=0)
fprintf(F,'CURRENTS THROUGH CURRENT CONTROLLED VOLTAGE SOURCES (NEGATIVE TO POSITIVE TERMINAL) \n');
for i=1:num_CCVS
fprintf(F,['i_' CCVS(i).Name ' = ']);
fprintf(F,num2str(eval(eval(['sol.i_' CCVS(i).Name]))));
fprintf(F,'\n');
end
fprintf(F,'---------------------------------------------------------------------------- \n');
end
type('Results.txt'); %Display the contents of Results.txt text file
fclose(F); %Close the Results.txt text file
%%-----------------------------------------------------------------------------
case{1}
%Create the state variables for node voltages, currents through voltage sources and inductor currents
variables='syms';
for i=1:(num_Nodes+num_V+num_VCVS+num_CCVS+num_L)
variables=[variables ' ' 'v' num2str(i) '(t)'];
end
eval(variables);
%----------------------------------------------
%Create a row vector var of the state variables - to be used in daeFunction
var_string=['var=[' variables(6:end) ']'];
eval(var_string);
%----------------------------------------------
%Convert the symbolic equations (only LHS) to a form suitable for daeFunction
%Use the converted symbolic equations to make a row vector eqn_daeFunction - to be used in daeFunction
eqn_string='eqn_daeFunction=[';
for i=1:length(eqn)
interm_string=char(eqn{i});
for j=1:(num_Nodes+num_V+num_VCVS+num_CCVS+num_L)
interm_string=strrep(interm_string,['v(' num2str(j) ')'],['v' num2str(j) '(t)']);
end
for j=1:num_Nodes
interm_string=strrep(interm_string,['vp(' num2str(j) ')'],['diff(v' num2str(j) ...
'(t)' ',t)']);
end
for j=1:num_L
interm_string=strrep(interm_string,['ip(' num2str(j) ')'],['diff(v' num2str(num_Nodes+num_V+num_VCVS+num_CCVS+j) ...
'(t)' ',t)']);
end
eqn_string=[eqn_string interm_string ','];
end
eqn_string=[eqn_string ']'];
eval(eqn_string);
%----------------------------------------------
%Use daeFunction to create the function handle odefun
odefun=daeFunction(eqn_daeFunction,var);
%----------------------------------------------
%Use ode15i along with created function handle odefun
v0=zeros(length(eqn_daeFunction),1); %Initial conditions for v
vp0=zeros(length(eqn_daeFunction),1); %Initial conditions for v'
disp('----------------------------------------------------------------------------');
fprintf('The transient analysis will be performed from t=0 to t=tf');
fprintf('\n');
tf=input('Enter the final time value tf in seconds : ');
options=odeset('RelTol',1e-03,'AbsTol',1e-03);
[t,v]=ode15i(odefun,[0 tf],v0,vp0,options);
%----------------------------------------------
table_heading=cell(1,(1+num_Nodes+num_V+num_VCVS+num_CCVS+num_L));
table_heading{1}='Time';
for j=1:num_Nodes
table_heading{1+j}=['v_' num2str(j)];
end
for j=1:num_V
table_heading{1+num_Nodes+j}=['i_' Volt_source(j).Name];
end
for j=1:num_VCVS
table_heading{1+num_Nodes+num_V+j}=['i_' VCVS(j).Name];
end
for j=1:num_CCVS
table_heading{1+num_Nodes+num_V+num_VCVS+j}=['i_' CCVS(j).Name];
end
for j=1:num_L
table_heading{1+num_Nodes+num_V+num_VCVS+num_CCVS+j}=['i_' Inductor(j).Name];
end
T=array2table([t,v],'VariableNames',table_heading);
date=datetime('now');
date_string=datestr(date);
xlswrite('Results.xls',{date_string});
xlswrite('Results.xls',{['File name : ' fname]},1,'A2');
writetable(T,'Results.xls','Range','A4');
plot(t,v(:,1:num_Nodes)); %Plot the node voltages vs. time
legend_voltage='legend(';
for i=1:num_Nodes
interm_string=table_heading{1+i};
interm_string=strrep(interm_string,'_','\_');
legend_voltage=[legend_voltage '''' interm_string '''' ','];
end
legend_voltage(end)=')';
eval(legend_voltage);
xlabel('TIME (s)');
ylabel('NODE VOLTAGES (V)');
savefig('Voltages_graph.fig');
if((num_V~=0)||(num_VCVS~=0)||(num_CCVS~=0)||(num_L~=0))
figure; %Create new figure window
plot(t,v(:,(num_Nodes+1):end)); %Plot the currents through voltage sources and inductor currents vs. time
legend_current='legend(';
for i=1:num_V
interm_string=table_heading{1+num_Nodes+i};
interm_string=strrep(interm_string,'_','\_');
legend_current=[legend_current '''' interm_string '''' ','];
end
for i=1:num_VCVS
interm_string=table_heading{1+num_Nodes+num_V+i};
interm_string=strrep(interm_string,'_','\_');
legend_current=[legend_current '''' interm_string '''' ','];
end
for i=1:num_CCVS
interm_string=table_heading{1+num_Nodes+num_V+num_VCVS+i};
interm_string=strrep(interm_string,'_','\_');
legend_current=[legend_current '''' interm_string '''' ','];
end
for i=1:num_L
interm_string=table_heading{1+num_Nodes+num_V+num_VCVS+num_CCVS+i};
interm_string=strrep(interm_string,'_','\_');
legend_current=[legend_current '''' interm_string '''' ','];
end
legend_current(end)=')';
eval(legend_current);
xlabel('TIME (s)');
ylabel('CURRENTS (A)');
savefig('Currents_graph.fig');
end
end