Calculate equivalent resistance using MATLAB
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
the series part works fine but not the parallel part, what am I doing wrong?
reminder please, \(\frac1R = \sum_i \frac1{r_i} \) ? but what exactly doesn't work?
initiallly your r =0
i see, so how do i go about solving it?
you must ask the value of one resistance before going into the loop.
ok, let me try it out first
alternately you assume some initail resistance and adjust the same before displaying the final result...
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.
@reemii correct ..
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
tell us what values you enter, and what the output is. we'll understand better what happens
no matter what value, or how many values i enter, the output remains 0
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.
And how do I make such a test? Another while loop?
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 ———
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
I mean, "if r <= 0"
Thank you! I get it now
Join our real-time social learning platform and learn together with your friends!