loop for parallel resestance

connection=input('type of connection 1=series 2= parallel');
n=input('nomber of r')'
r=input('r');
if connection==(1);
for i=1:n;
sum=sum+r;
end
else
for i=1:n;
y=(1/r)+sum;
sum=y;
end
end
disp('equivelant r');disp(sum)

Risposte (1)

Geoff Hayes
Geoff Hayes il 17 Dic 2015
leyth - it would be helpful if you include the error message when posting your question. When I try to run your code, I observe
Error using sum
Not enough input arguments.
The sum is a built-in MATLAB function to (for example) sum the elements in an array. In your case, it appears that your sum is a local variable to represent the sum of a handful of numbers. It is never a good idea to name a variable with the same name as a function, so please change this to something else such as mySum or whatever it is supposed to represent.
Your code could then be written as
mySum = 0;
if connection==1
mySum = n*r;
else
mySum = n*1/r;
end
Note how the for loops have been removed to simplify the code. (Though the second loop for the parallel case may need to be reviewed by you because it will produce a different answer from what you had before). While the initialization of mySum to zero isn't necessary, it would be if you continued to use the for loops since you try to access the variable before it has been initialized at
mySum=mySum+r;

Questa domanda è chiusa.

Richiesto:

il 17 Dic 2015

Chiuso:

il 20 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by