How can I write a code for cicuits?

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

The question is too long. Do you have any specific problems? Where are your attempts?
I need a code where I can ask the user to enter a random but EVEN number of resistor values. For example, if the user writes [ 5 1 3] , the the output would say " no , the number of resistors must be even,re-write it again." and the user will re-write the values for example [ 5 1 3 8] and then the loop will continue in that case and so on for the rest of the conditions listed above. Hope you can help.
You need to use input function to ask user for data
mod function can help your to check if number is odd or even
So what kind of help do you need? Here is input and here mod
Thank you so much. I'll start writing my code and i'll tell you if i face any problems later on. BUT STILL THANK YOU VERY MUCH for your quick reply.
You are always welcome
I want to write a while loop code where i can ask my user to enter an infinite but EVEN number of values . Theses values must be real and positive. If the values enetered by the user satisfy my condition, it will end the the loop.
My attemtps where many and they all didn't end well , most ended with a red message from matlab:)
Hope you can help me with the code
clear all ;
close all;
equation=false;
while equation==false
Resistors=input(' Enter values of two resistances in ohm [R1 R2]= '); % to enter tha values of the resistors
Total=length(Resistors);
m= Resistor
if mod(m,2) == 0
[row,column]=size(Resistors);% matrix of the resistors
if isreal(Resistors) && row==1 && column>=2 % this is to make sure the values are real and the matrix is one row and two columns
for number=1:Total
if Resistors(number)>0 % value of the resistor is positive or otherwise
equation=true;
elseif Resistors(number)<=0
equation=false;
break;
end
end
end
end
if equation==false
disp(' invalid numbers .... please enter valid numbers')
end
end
disp ('===================================================================')
John D'Errico
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.
umm i really didn't get younor your suggestion. If you could either be clearer or teach me how to write a while loop code with "and" conditions..thank you though
I changed this part of your code and it works ok
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.
I can't thank you enough Darova for you help with me. May god bless you soul.
I voted for your question for your gratitude
Sorry for disturbing you again, but if i wanted to specify things for example if the number of values isn't even i want a sentence to be displayed saying "rewrite the values aking sure their number is even" and if the numbers entered by the user are complex and negative which is opposite to my code i also want a similar message to show.. so basically for each condition i want a message to be displayed.. I tried playing around with the end of the code and it got me somewhere but not exactly my wish.. could you give me your tips, guidience, and little bit help?
clear all ;
close all;
equation=false;
while equation==false
Resistors=input(' Enter the resistance values in ohm, [R1...Rn]= '); % to enter tha values of the resistors
V=length(Resistors);
if mod(V,2) == 0
[row,column]=size(Resistors);% matrix of the resistors
if isreal(Resistors) && row==1 && column>=2 % this is to make sure the values are real and the matrix is one row and two columns
equation=true;
for number=1:V
isreal(Resistors) && row==1 && column>=2==false
disp(' Re-enter the resistances by which the number of resistors is even ')
break;
if Resistors(number)>0 % value of the resistor is positive or otherwise
equation=true;
elseif Resistors(number)<=0
equation=false;
disp(' Re-enter the resistances where its values are positive and real ')
break;
end
end
end
end
end
disp ('===================================================================')
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?
Hello againn, sorry but i've been working on what i have and got this.. for some reason, the sentence "Re-enter the resistances by which the number of resistors is even" keeps repeating with me in the output.. do you know a solution?
This is my code
clear all ;
close all;
equation=false;
while equation==false
Resistors=input(' Enter values of two resistances in ohm [R1 R2]= '); % to enter tha values of the resistors
V=length(Resistors);
if mod(V,2) == 0
[row,column]=size(Resistors);% matrix of the resistors
if isreal(Resistors) && row==1 && column>=2 % this is to make sure the values are real and the matrix is one row and two columns
for number=1:V
if Resistors(number)>0 % value of the resistor is positive or otherwise
equation=true;
else
equation=false;
break;
end
end
end
if equation==false
disp('Re-enter the resistances where its values are positive and real ')
end
end
if equation==false
disp(' Re-enter the resistances by which the number of resistors is even')
end
end
disp ('===================================================================')
and this is my output
Enter values of two resistances in ohm [R1 R2]= [8 7 6]
Re-enter the resistances by which the number of resistors is even
Enter values of two resistances in ohm [R1 R2]= [8 -7]
Re-enter the resistances where its values are positive and real
Re-enter the resistances by which the number of resistors is even
Enter values of two resistances in ohm [R1 R2]= [7 8]
===================================================================
  • do you know a solution?
Solution for what? You don't want to see these?
i don't want to see line five from the output.. the second repeated sentence
try to remov this line
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
and how did you use the phase and frquency in the Voltage eqaution.
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
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 ...
Create new question
and use special button
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

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 6 Apr 2020

Commented:

on 14 Apr 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!