How do I write a script file that asks the user to input the total resistance for a resistor and then outputs a list of colors for the bands in order (1, 2, 3)?

71 views (last 30 days)
A resistor has three colored bands that shows its total resistance. The total resistance = ((color of first band)*10 + (color of second band))*10^(color of the third band)
black is 0
brown is 1
red - 2
orange - 3
yellow - 4
green - 5
blue - 6
violet - 7
gray - 8
white -9
  2 Comments
Geoff Hayes
Geoff Hayes on 7 Feb 2016
Ian - is the idea that the code must determine which of the three colours can be used in the above equation that returns the total resistance that the user entered? Do all combinations have to be determined? If you want to get user input, use input to request that the user enter a total resistance.
totalResistance = input('Please enter the total resistance: ');
Does your code now iterate over all possible combinations (504 of them?) to determine which triple produces totalResistance? What if no such triple exists?

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 7 Feb 2016
Edited: Star Strider on 7 Feb 2016
This takes me back to my amateur radio days (although I was used to mapping from stripes to resistance):
Colours = {'black' 'brown' 'red' 'orange' 'yellow' 'green' 'blue' 'violet' 'gray' 'white'};
Values = [0:9];
Rc = inputdlg('Stripe your resistor! What is its resistance (Ω)? ')
Rstr = Rc{:};
for k1 = 1:length(Rstr)
parse{k1} = Rstr(k1);
end
Mult = fix(log10(str2num(Rstr)));
Bands = parse(1:2);
Resistor = Colours([str2num(Bands{1})+1, str2num(Bands{2})+1, Mult])
fprintf(1, '\tYour resistor of %s Ohms will be striped %s, %s, %s\n\n', Rstr, char(Resistor{1}), char(Resistor{2}), char(Resistor{3}))
Example output:
Your resistor of 47000 Ohms will be striped yellow, violet, orange
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Debugging and Analysis 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!