Sie sind auf Seite 1von 13

The purpose of this program is to solve the DC electric circuit.

It contains three MATLAB functions and one script file utilizes all the functions. The first MATLAB function named circuit_para records the structure and parameters of a DC electric circuit. This function also puts all the values into a .txt file. The second MATLAB function named load_para loads the data from the saved .txt file: An_Vu.txt. The last MATLAB function named solve_circuit performs the following: o Use Node-voltage method to solve currents in all branches. o Calculate the power dissipated by each resistor in the circuit. o Calculate the power generated by each source voltage. The MATLAB script that involves the three MATLAB functions above solves a circuit and displays all of the calculated results.

function [choice]= circuit_para clear all clc %Input the number of branches a =input('Enter the number of branches (from 4 to 10 branches):'); while a<4 || a>10 a = input('Re-enter the number of branches (from 4 to 10 branches):'); end %Input the number of nodes b = input('Enter the number of nodes (from 3 to 6 nodes):'); while b<3 || b>6 b =input('Re-enter the number of nodes (from 3 to 6 nodes):'); end count=0; % Input the parameter of each branches (Voltage source,resistance , starting and ending point) for i = 1:1:a fprintf('\nEnter the parameter of branch %g: \n',i) R(i)= input('Enter the value of resistance in this branch (R>0): '); while R(i)<=0 R(i)= input('Re-enter the value of resistance in this branch (R>0):'); end V(i) = input('Enter the source voltage in this branch (V can be <=0):'); if(V(i)~=0) count=count+1; end % Enter starting and ending nodes. S(i) = input('Enter the starting node: '); while S(i)>b || S(i)<=0 %To check this node whether exists or not. S(i) = input('Re-enter the starting node: '); end E(i) = input('Enter the ending node: '); while E(i)>b || E(i)<=0 || E(i)==S(i) whether exists or not.

%To check this node

E(i) = input ('Re-enter the ending node: '); end if S(i)>E(i) %Switch the values of star and end nodes temp=S(i); S(i)=E(i); E(i)=temp; end end %Check the accuracy of voltage. if (count==0) choice= input('There are mistakes in your input. Enter 1 to reenter the value of source voltage, enter other numbers to exit.\n'); if (choice==1) while (count==0) for i = 1:1:a fprintf('Enter the source voltage of branch %g: \n',i) V(i) = input('Enter the source voltage in this branch (V can be <=0):'); if(V(i)~=0) count=count+1; end end end else return end end choice=1; % Save name of parameters into a text file file= fopen('An_Vu.txt','w'); fprintf(file,'Branches: %g\r\n',a); fprintf(file,'Nodes: %g\r\n',b); fprintf(file,'%3s %10s %14s %26s %29s\r\n','Branch','Resistance','Voltage','Starting_node','Ending_node' ); % Save information of parameters into a text file for i = 1:1:a x=i; A=[x;R(i);V(i);S(i);E(i)]; fprintf(file,'%3g %9g %16g %24g %29g\r\n',A); end disp(' '); disp('All the figures have been saved in a text file.'); end

This function meets the requirements: In each branch, there is at least one resistor. (line 19 to 23). In the whole circuit, there is at least one voltage source. (line 47 to 62). The value of starting node must be smaller than the ending nodes. If the user inputs the opposite, the function will switch the value of start and end node to satisfy the condition. (line 39 to 43). In each branch, the starting node must be different from ending node. (line 36). From the line 64, these code lines save all the values into an An_Vu.txt file. First, I create a .txt file named An_Vu.txt. Second, I write the first two lines of the .txt file which are the number of branches and nodes. Third, I put the names of five columns in the third line. Then I create a matrix A contains all the values of parameters and print to the file to match five columns above. At the end of the function, I notify the user that a .txt file has been created.

function[branch,node,R,V,S,E]= load_para %Get the number of branches and nodes branch =textread('An_Vu.txt','%*s %d', 1); node=textread('An_Vu.txt','%*s %d', 1,'headerlines', 1); %Get the value of parameters (Skip the first three rows) [x,a,b,c,d] =textread('An_Vu.txt','%d %d %d %d %d','headerlines', 3); A=[a b c d]; R=A(:,1); V=A(:,2); S=A(:,3); E=A(:,4); %Show the table with values in txt file h1=branch*20+70; %Adjust the height( in position of table) h2=branch*20+20; %Adjust the height( in position of value in table) f = figure('Position',[500 500 420 h1]); %Create the window table cnames = {'Branch','R','V','Starting node','Ending node'}; %Set the row name t = uitable('Data',[x,a,b,c,d],'ColumnName',cnames,'RowName',[],'Position', [20 20 379 h2]); %Put all the values into the table end

To get values out of the .txt file, I use the textread command. First, I get the number of branches. Second, I get the number of nodes by skipping the first line in the text file. Third, I skip the first three lines and get the values of parameters. Then, I put all the figures into a matrix A. Last, I get the values of each parameter by collecting each column of matrix A.

From the line 15, I create a table contains the required values. (Notes: in the position concept, four numbers between the two brackets stand for left, bottom, width and height). The first height partially adjusts the position of the table in the screen. The second height partially adjusts the position of the values inside the table. The purpose is matching the values to the table and the size of table can be modified due to the values. The table may look like this:

If the number of branches increases, the size of the table will change to make sure that all the figures are visible. -----------------------------------------------------------------------------function [pI,pV] = solve_circuit(branch,node,R,V,S,E) branches=branch; %Get the number of branches ref=node; %Take the last node as the reference node X=zeros(node-1,node-1); %The matrix contains coefficients Y=zeros(node-1,1); %The zero matrix on the right side for i=1:branches n1=S(i); n2=E(i); if n2==ref %If end node is the reference node X(n1,n1)=X(n1,n1)+1/R(i); Y(n1)=Y(n1)+V(i)/R(i); else %Other cases X(n1,n2)=X(n1,n2)-1/R(i);

X(n2,n1)=X(n2,n1)-1/R(i); X(n1,n1)=X(n1,n1)+1/R(i); X(n2,n2)=X(n2,n2)+1/R(i); Y(n2)=Y(n2)-V(i)/R(i); Y(n1)=Y(n1)+V(i)/R(i); end end voltage=pinv(X)*Y; %Calculate the voltage matrix I=zeros(branches,1); for i=1:branches n1=S(i); n2=E(i); if n2==ref %If end node is the reference node I(i)=-(V(i)-voltage(n1))/R(i); else %Other cases I(i)=(voltage(n1)-voltage(n2)-V(i))/R(i); end end %Calculate The powers for i=1:branches pI(i)=(I(i))^2*R(i); if V(i)~=0 pV(i)=V(i)*I(i); end end

In this function, I use the node-voltage method to solve the circuit. First, I get the number of branches and set the last node as the reference node. Second, I create two matrices. The X matrix contains the coefficients of the voltages. The Y matrix is a zero matrix on the right side of =. From the line 7 to line 21, I use a for loop which involves specific cases to collect the coefficients. Then I

have a matrix of values of voltage by multiply inverse of X with Y. Third, from the line 23 to 32, I create a zero matrix I. By using the for loop with several cases, I can put the values of current into the matrix I. The last step is calculating the required powers. -----------------------------------------------------------------------------%A few sentences of requirement clear all clc disp('PLEASE READ THESE REQUIREMENTS: '); disp(' '); disp('You have to make sure that in each branch '); disp('the value of start node must be smaller than the end node`s.'); disp('If you input the opposite, the program will change the values to'); disp('meet the above condition.'); disp('The voltage in branch receives plus sign toward the start node'); disp('minus sign toward the end node.'); disp('Thus, You must flexibly choose the positive or negative value of voltage.'); choice=input('\nPRESS 1 TO CONTINUE, OTHER NUMBERS TO EXIT: '); if (choice==1) %Start the functions option= circuit_para; if (option==1) [branch,node,R,V,S,E]= load_para; [pI,pV] = solve_circuit(branch,node,R,V,S,E); disp(' '); %Print out the Powers required for i=1:branch pI(i)=pI(1,i); fprintf('The power dissipated by R%d= %g W\n',i,pI(i)) end disp(' '); for i=1:length(pV) pV(i)=pV(1,i); if pV(i)~=0 fprintf('The power generated by V%d= %g W\n',i,pV(i)) end end end else return end

disp(' '); disp('Thank you and appreciate for using this program.'); disp(' Le Van Vu An');

Because everything in this script is rather clear, I just explain some important functions. I show a few requirements to the users before let them use the program. If the users want to continue, they have to enter 1. The program will exit if they push other numbers. From the line 28, I let i comes from one to the length of matrix pV because the number of power generated by voltages may not equal to the number of branches. I also give a condition that the program only prints out the values of powers that are greater than zero. ------------------------------------------------------------------------------

In this part, I will check the accuracy of the program by comparing the answer I calculate by hand with the one generated by the program. This is the specific example I take from a quiz in Physics 221 course. I got full score in this test so I can be sure 100% that the answer is correct.

The circuit has 5 branches and 3 nodes. I assume that V1 is between node 1 and 3 and goes through R2. V2 is between node 2 and 3 and goes through R4. Solution: Node 1: i1 - i2 - i3 = 0 Node 2: i3 - i4 - i5 = 0

i1 =

; i2 =

; i3 =

; i4 =

; i5 =

We have two equations to solve the voltages:

=0 =0

Thus, the voltage V1 and V2 are:

V1=102,1V V2=53,3V I1=16.3 A; i2=10.21 A; i3=6.1 A; i4=-11.675 A; i5=17.76 A The power dissipated by R1= 1594.14 W The power dissipated by R2= 1042.44 W The power dissipated by R3= 297.68 W The power dissipated by R4= 545.2 W The power dissipated by R5= 946.25 W The power generated by V1= -3260 W The power generated by V4= -1167.5 W ---------------------------------------------------------------------------------------

The answer comes from the program:

The power dissipated by R1= 1596.68 W The power dissipated by R2= 1042.89 W The power dissipated by R3= 297.758 W The power dissipated by R4= 544.857 W The power dissipated by R5= 947.52 W

The power generated by V1= -3262.6 W The power generated by V4= -1167.11 W Since the sum of all the powers is zero, the answer is correct. We can see that the difference of answer between doing by hand and generating by program is less than 1%. Thus, we can guarantee the accuracy of the program.

This final project creates a program that helps user calculate the powers both dissipated by each resistor and generated by each voltage. I recommend that you should follow the requirements mentioned above to get the correct answer. Thank you for using my program! Hope you succeed. L Vn V An

Das könnte Ihnen auch gefallen