Ask your own question, for FREE!
Mathematics 28 Online
OpenStudy (anonymous):

Calculate equivalent resistance using MATLAB

OpenStudy (anonymous):

r=0; k=0; type=input('Type of connection (series or parallel): ','s'); if strcmpi('series',type) while k>=0 k=input('Resistor rating (enter 0 to stop): '); r=r+k; if k==0 break; end end disp(['Equivalent resistance: ',num2str(r)]) end if strcmpi('parallel',type) while k>=0 k=input('Resistor rating (enter 0 to stop): '); r=1/(1/r+1/k); if k==0 break; end end disp(['Equivalent resistance: ',num2str(r)]) end

OpenStudy (anonymous):

the series part works fine but not the parallel part, what am I doing wrong?

OpenStudy (reemii):

reminder please, \(\frac1R = \sum_i \frac1{r_i} \) ? but what exactly doesn't work?

OpenStudy (anonymous):

initiallly your r =0

OpenStudy (anonymous):

i see, so how do i go about solving it?

OpenStudy (reemii):

you must ask the value of one resistance before going into the loop.

OpenStudy (anonymous):

ok, let me try it out first

OpenStudy (anonymous):

alternately you assume some initail resistance and adjust the same before displaying the final result...

OpenStudy (reemii):

I wouldn't start with a default value like 1 or .. 2 ? because that would fix one 'resistor rating' to that value; I don't think he wants that.

OpenStudy (anonymous):

@reemii correct ..

OpenStudy (anonymous):

if strcmpi('parallel',type) r=input('Resistor rating: '); while k>=0 k=input('Resistor rating (enter 0 to stop): '); r=1/(1/r+1/k); if k==0 break; end end disp(['Equivalent resistance: ',num2str(r)]) end I tried it like this but didnt work

OpenStudy (reemii):

tell us what values you enter, and what the output is. we'll understand better what happens

OpenStudy (anonymous):

no matter what value, or how many values i enter, the output remains 0

OpenStudy (reemii):

I get it. you must make the test (k>0) before the line r=1/(1/r+1/k); because you must break before doing anything forbidden like dividing by zero or a negative value. I think that r is always evolving fine, until you decide to "break". then you have this 1/0 division and 1/(1/r + infinity) always gives you ZERO.

OpenStudy (anonymous):

And how do I make such a test? Another while loop?

OpenStudy (reemii):

No, instead of ——— k=input('Resistor rating (enter 0 to stop): '); r=1/(1/r+1/k); if k==0 break; end ——— test the value of k immediately like this: ——— while k>0 %% this could be simply "while true", don't you think? k=input('Resistor rating (enter 0 to stop): '); if k <= 0 break end r=1/(1/r+1/k); end % of while ———

OpenStudy (reemii):

before the loop you might also make this test: ——— if r < 0 disp('you dont even want to supply one valid value? too bad...'); else while .... % now the loop ... end

OpenStudy (reemii):

I mean, "if r <= 0"

OpenStudy (anonymous):

Thank you! I get it now

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!