How can I write a code for cicuits?
Show older comments
I am asked to Write a program in MATLAB to analyze a circuit. The circuit contains of n Resistors, n/2 capacitors, n/2 coils and an AC voltage source in the form of V Vm wt = + cos( ) .
The following requirements should be fulfilled
1. The number of resistances, n, has to be even number.
2. The minimum value for n is 2.
3. If the number of resistances is n, the number of coils must be n/2 and the number of capacitors must be n/2. The user will be prompted to enter the correct number if it is different from n/2
4. The values of all resistors, coils and capacitors must be real and positive
Please help me with the code.. I'm dying trying to find videos and tutorials
Sample example 1 (note: The red colour represents the data entered by the user) Enter the resistance values in ohm, [R1...Rn]=[10 30 20] The number of resistances should be even. Reenter values of the two resistances in Ohm, [R1...Rn]=[10 20 40 100] ==============================================================================
Enter the capacitance values in Farads, [C1...Cn/2]=[0.01 0.02 0.04]
The number of capacitors should be 2, 1/2 number of resistances Enter the capacitance values in Farads, [C1...Cn/2]=[0.01 0.02] ==============================================================================
Enter the inductance values in Henry, [L1...Ln/2]=[0.3 0.2] ==============================================================================
Enter the amplitude of the voltage source in volt, Vmag=10
Enter the phase of the voltage source in degree, Vphase=0
Enter the frequency of the voltage source in rad/s, Freq=100
24 Comments
darova
on 6 Apr 2020
The question is too long. Do you have any specific problems? Where are your attempts?
Mariam Al Zarouni
on 6 Apr 2020
Mariam Al Zarouni
on 6 Apr 2020
darova
on 6 Apr 2020
You are always welcome
Mariam Al Zarouni
on 7 Apr 2020
Edited: darova
on 7 Apr 2020
John D'Errico
on 7 Apr 2020
Edited: John D'Errico
on 7 Apr 2020
You want to enter an infinite number of resistors? That will not take a long time to run?
Until you learn to write code carefully and to debug it, you won't get far writing code. I'm sorry, but this is true.
Here, for example, you most immediate error is that you enter the resistors in the variable Resistors, yet then you use the variable Resistor.
While that is most likely just a typo, MATLAB does not know this.
Computers cannot read your mind. Perhaps the indicated error was not a mistake, but intentional. They try to do what you tell them to do, and then if something happens that is a problem, they return an error message indicating what it sees as the problem.
So you need to learn to read the error message, then look at your code and fix it. Sometimes that is best done using the debugger to track down your problem. Here the error message would have said something about the existence of a variable or function named Resistor. Since you apparently wanted to use the variable Resistors, you should have seen the problem immediately.
I have a funny feeling that your code has at least two other errors in it. But learning to use the debugger will help you greatly.
Mariam Al Zarouni
on 7 Apr 2020
darova
on 7 Apr 2020
I changed this part of your code and it works ok

John D'Errico
on 7 Apr 2020
How much more clear must I be? You typed the variable named Resistors incorrectly, calling it Resistor, WITHOUT an s at the end.
That will generate an error. Your code will then fail to run.
How should you fix it? Edit the code. Fix the typing error. Then try to run the code again, perhaps fixing the next errors until you have code that works and does what you wish it to do.
Mariam Al Zarouni
on 7 Apr 2020
darova
on 7 Apr 2020
I voted for your question for your gratitude

Mariam Al Zarouni
on 7 Apr 2020
darova
on 7 Apr 2020
I suggest you to re-write code as following
% if something1
% % code
% if something2
% %code
% %code
% if something3
% %code
% end
% end
% end
if something1
% code1
end
if something2
% code2
end
if something3
% code3
end
Can you do this?
Mariam Al Zarouni
on 7 Apr 2020
darova
on 7 Apr 2020
- do you know a solution?
Solution for what? You don't want to see these?

Mariam Al Zarouni
on 7 Apr 2020
darova
on 7 Apr 2020
try to remov this line

layla Hweij
on 14 Apr 2020
Hello All,
Im struggling with the same question but fruther this point, the continuation of the question says that we have to figure out the approximate current of the impedance matrix I have tried everything and its still not working.. so Mariam Al Zarouni, have you got the answer to that part or not ? please if you can send me the solution of the approximated current loop and how did you got it , would be fantastic
layla Hweij
on 14 Apr 2020
and how did you use the phase and frquency in the Voltage eqaution.
layla Hweij
on 14 Apr 2020
this is my code %%%%%% this the inversed impedance matrix multiplied by voltage vector to get the actual current, inwhich its outputs about are correct
Itrue= inv(z)*V;
%To get length of currents = 4
nn = numel(Itrue);
disp('The current:');
itrue = complex(Itrue);
disp(itrue);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
here is supposed code for the approximated current using the relaxation method but its not working it gives me zeros
disp('========= this is Successive Relaxation Method ========');
%to create the X matrix
b = size(z,1);
X = zeros(1,nn);
err=100;
xhistory=[];
while( err > tolerance)
for v = 1:b
Xprev = X(v);
X(v) = sum(Z(v,1:(v-1)).* X(1:(v-1)))+ sum(Z(v,(v+1):b).*X(v+1:b));
disp(X(v));
end
err = max(abs( X - Xprev));
disp(err);
xhistory =[xhistory,X];
break;
end
layla Hweij
on 14 Apr 2020
this is rest of mariam question if you want a clearer verison of what im saying
==============================================================================
The Alpha should be positive real number between (0 and 2), check the value and enter it again
Enter the Alpha (between 0 and 2) to be used with the Successive Relaxation Method=0.5
==============================================================================
The impedance matrix to solve the mesh current is:
1.0e+02 *
0.1000 - 0.0100i -0.1000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
-0.1000 + 0.0000i 0.3000 - 0.0003i -0.2000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -0.2000 + 0.0000i 0.6000 - 0.0050i -0.4000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i -0.4000 + 0.0000i 1.4000 - 0.0005i
The voltage vector to solve the mesh current is:
10
0
0
0
I'm stuck at this point
||
v
approximated, exact=
1.7813+0.33424i 1.7857+0.33861i
0.816+0.15698i 0.81954+0.16005i
0.33475+0.067759i 0.33674+0.069396i
0.095532+0.019329i 0.096204+0.019862i
relative error, significant digit=
0.0034088 3
0.0056082 2
0.0074962 2
0.0087305 2
Magnitude Phase
1.8124 10.6275
0.8310 10.8894
0.3415 11.4432
0.0975 11.4380
Please if you can help us, it will be wonderful since I dont have much time left to the deadline ...
darova
on 14 Apr 2020
Create new question
and use special button

layla Hweij
on 14 Apr 2020
and here is the result including the voltage vector , the actual current and supposed relaxation method that will give me the approximated current
========================================
The Voltage vector to solve the mesh current is:
10
0
0
0
The current:
1.166 - 0.16397i
0.14959 - 0.28057i
0.062247 - 0.11448i
0.01285 - 0.034543i
========= this is Relaxation Method ========
0
0
0
0
0
Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!