PDA

View Full Version : MATLAB - Resistors series/paralell


fusi0n556
10-06-2009, 06:24 AM
Okay first of all I did read the guidlines about posting assignment questions
.. and yes this is an assignment question, but I am completely stuck on what to do and I just need someone to tell me what direction i should continue

anyway here is the guideline on what the coding should do:

Resistors are electronic components which impede the flow of electrical current. They are
measured in units of Ohms which is represented by the Greek character Ω (omega). Sometimes an
electronic designer needs a precise value for resistance within a circuit such as 29.65 kΩ (or 29650
Ω), which can pose a problem since they are only manufactured with certain preferred values.
Preferred resistor values are:
1.0 Ω, 1.2 Ω, 1.5 Ω, 1.8 Ω, 2.2 Ω, 2.7 Ω, 3.3 Ω, 3.9 Ω, 4.7 Ω, 5.6 Ω, 6.8Ω, 8.2 Ω and 10 Ω.

These values may seem like a strange choice but they actually form a geometric
series with each one about 21% larger than the previous one (the E12 series). One Ohm is the
smallest value that is widely available and ten million Ohms is the largest. Resistor values are
available in each of the intervening decade ranges so, for instance, it is possible to buy
3.9 Ω, 39 Ω, 390 Ω, 3.9 kΩ, 39 kΩ, 390 kΩ and 3.9 MΩ values.

However, if the designer needs a specific value it may be possible to combine two, or more,
resistors in series of parallel to achieve a closer value.
When two resistors R1 and R2 are combined in series their values add:

Rs = R1 + R2

(Rs is a single equivalent resistance value for the series combination)
and when they are combined in parallel their values combine in a reciprocal fashion:

Rp = 1.0 / ( (1.0 / R1) + (1.0 / R2) )

(Rp is a single equivalent resistance value for the parallel combination)
So for example if we connect 4700 Ω and 5600 Ω resistors in parallel the equivalent resistance is
2555.3 Ω.
Both the series and parallel equations can be extended for an arbitrary number of resistors, so for a
third resistor R3 the formulae become:

Rs = R1 + R2 + R3, and
Rp = 1.0 / ( (1.0 / R1) + (1.0 / R2) + (1.0 / R3) )

For additional background material read through the topic “Series and parallel resistors” in the
Wikipedia article on Resistors (see http://en.wikipedia.org/wiki/Resistor) and the section on the E12
series in the article on preferred values (see http://en.wikipedia.org/wiki/Preferred_number).
This assignment involves writing a Matlab program. The requirements for the program are listed
below:

1. The program must request two values from the user, the value of resistance that is to be achieved
(the target value) and the maximum number of resistors that can be combined. Normally there is a
finite limit on the maximum number of resistors which can be combined due to cost or available
space on the printed circuit board.

2. Electronic designers often use a shorthand for specifying resistance values by using the letters R,
K and M instead of Ω, k Ω or M Ω. (You will recall the prefix k, or kilo, means thousands of Ohms
and M, or mega, means millions of Ohms). Moreover designers often use this letter in place of the
decimal point. So, for instance, a designer might use any one of 239500R, 239.5k, 239K5,
0.2395M or 0m2395 to mean the same resistance value. Your program should allow the resistance
value to be entered in any of these forms and using lower or upper case letters.

3. It must list the values of the appropriate set of standard resistor values (chosen from the E12
series above) which, when combined in series or parallel, come as close as possible to the target
value. The user should be asked for a file name and the chosen values written as a “human
readable” report within the text file. See the items below for other information to include within
this report.

4. It must choose the best possible combination of component values and display the resulting error
as a percentage. So for example if the target value is 500 Ω, but with three resistors the closest
approximation that can be achieved is 483 Ω this leads to an error of (17 Ω / 500 Ω) x 100 = 3.400%.
Like many engineering calculations the error should be displayed to 4 significant figures.

5. It should consider all possible series and parallel combinations for the chosen number of
resistors. For example if the user chooses to use three resistors to approximate a target value the
program must consider each of the following combinations for resistors R1, R2 and R3:

(i) All three resistors in series: Rt = R1 + R2 + R3
(ii) Two of the resistors in parallel and the third in series:
Rt = R1 + (1.0 / ( (1.0 / R2) + (1.0 / R3)))
(iii) Two of the resistors in series and the third in parallel:
Rt = (1.0 / ( (1.0 / (R1 + R2)) + (1.0 / R3)))
(iv) All three resistors in parallel: Rt = 1.0 / ( (1.0 / R1) + (1.0 / R2) + (1.0 / R3) )

8. If it is possible to precisely achieve the target value with fewer resistors than the designer
specified then the program should provide this result, otherwise it should use all the resistors
available to achieve the closest possible approximation to the target value.

anyway this is what ive done so far, ive only been able to do the series
clc
%-- resistance INPUTS/calls --g
resistance_call = input('What is the resistance wanting to be achieved?: ','s');
thousand_set = '[kK]';
million_set = '[mM]';
r_set = 'rR';
var = 'kKmM0123456789.';
non_r = [];
non_l = [];
thousd = '.*1000';
mill = '.*1000000';
digits = '0123456789.';

for count = 1 : length(resistance_call);
x = resistance_call(count);
if strfind(r_set, x);
else
non_r = [non_r count];
end;
end

resistance_array = ([resistance_call(non_r)]);
resistance_array_call_final = str2num(resistance_array);

for count2 = 1:length(resistance_array);
x = resistance_array(count2);
if strfind(var, x)
else
disp('ERROR:2 (refer to manual)');
return;
end

end

end_count = 0;

for count3 = 1:length(resistance_array);
x = resistance_array(count3);
if strfind(thousand_set, x)
if end_count == 1
disp('ERROR:3 (refer to manual)');
return;
elseif length(resistance_array) == count3
resistance_array = regexprep(resistance_array, thousand_set, '.*1000', 'ignorecase');
resistance_array_call_final = str2num(resistance_array);
end_count = end_count + 1;
elseif length(resistance_array) ~= count3
temp_array = regexprep(resistance_array, thousand_set, '.', 'ignorecase');
temp2_array = str2num(temp_array);
resistance_array_call_final = temp2_array.*1000;
end_count = end_count + 1;
end
elseif strfind(million_set, x)
if end_count == 1
disp('ERROR176 refer to manual');
return;
elseif length(resistance_array) == count3
resistance_array = regexprep(resistance_array, million_set, '.*1000000', 'ignorecase');
resistance_array_call_final = str2num(resistance_array);
end_count = end_count + 1;
elseif length(resistance_array) ~= count3
temp_array = regexprep(resistance_array, thousand_set, '.', 'ignorecase');
temp2_array = str2num(temp_array);
resistance_array_call_final = temp2_array.*1000;
end_count = end_count + 1;
end
end
end

if resistance_array_call_final == 0;
disp('You wont need any resistors for that!');
return
else
end

int_resis_msg = sprintf('Resistance: %10.4f Ohm/s',resistance_array_call_final);
disp(int_resis_msg);

%-- resistor amount INPUTS/calls --
amount_call = input('What is the maximum amount of resistors allowed?: ', 's');

for count = 1:length(amount_call);
x = amount_call(count);
if strfind(digits, x)
else
disp('ERROR:5 (refer to manual)');
return;
end

end

amount_restistors = str2num(amount_call);

if amount_restistors >= 10
disp('ERROR:6 (refer to manual)');
return;
elseif amount_restistors ~= round(amount_restistors);
disp('ERROR:7 (refer to manual)');
return;
else
end

%-- resistance arrays --
resistors = [...
10000000 8200000 6800000 5600000 4700000 3900000 3300000 2700000 ...
2200000 1800000 1500000 1200000 1000000 820000 680000 560000 470000 ...
390000 330000 270000 220000 180000 150000 120000 100000 82000 68000 ...
56000 47000 39000 33000 27000 22000 18000 15000 12000 10000 8200 ...
6800 5600 4700 3900 3300 2700 2200 1800 1500 1200 1000 820 680 560 ...
470 390 330 270 220 180 150 120 100 82 68 56 47 39 33 27 22 18 15 ...
12 10 8.2 6.8 5.6 4.7 3.9 3.3 2.7 2.2 1.8 1.5 1.2 1];

array = rot90(resistors);

%-- series --

totalval=resistance_array_call_final;
temp=zeros(amount_restistors,1);

for n =1:amount_restistors ;
for k = 1:length(array); %'k' is created as a new variable so that values can be stored following the length of 'array'.
if array(k)-resistance_array_call_final >0 ; %This value of k which was stored is then subtracted from the total resistance value entered by the user and is checked if it is greater than zero.
temp(n) = array(k-1); %A temporary variable is then created. This variable is created on the basis of what 'array)k-1) is. This value is copied into a new array.
break
end
end
resistance_array_call_final = resistance_array_call_final-temp(n); %The resistor value changes to the new value which needs to be worked out and therefore repeating the loop till all conditions are satisfied
if resistance_array_call_final <1;
break
end
end



%-- messages --
p = 0;

for a = 1:amount_restistors;
if temp(a,1) > 0;
message1=sprintf('Resistor %g is %g Ohm''s',a,temp(a,1)); %This 'sprintf' command is used to display the resistor values used in the calculations. But only the number used, not the maximum amount that could have been used
disp(message1);
p = p+1;
elseif temp(a,1) < 0;
message1=sprintf('Resistor %g is %g Ohm''s',a,temp(a,1)); %This 'sprintf' command is used to display the resistor values used in the calculations. But only the number used, not the maximum amount that could have been used
disp(message1);
p = p+1;
elseif temp(a,1) == 0;
message2=sprintf('Resistor %g is not required as more accurate value is obtained using %g resistor/s',a,p);
disp(message2);
end
end

message4=sprintf('Overall achieved resistance: %10.4f Ohm/s',sum(temp));
disp(message4);

if totalval-sum(temp) ~= 0;
message2 = sprintf('The error rate is %g percent',abs(totalval-sum(temp))/totalval*100);
disp(message2);
elseif totalval-sum(temp) == 0; %This whole 'if' loop is used to calculate the percentage error that occurs in the calculations. It displays the error if there one, or displays a 100% match statement if the resistors used are exact.
message3 = sprintf('100 percent match, use the above resistors in series');
disp(message3);
end


all I want to know is how to go about step 5) i) to iv)
I'm wanting to know how i should produce the possible values when both series and paralell then calculate the closest value to the resistance_call.

Any way any help is appreciated :)

oesxyl
10-06-2009, 02:37 PM
I would start with 8, explore 5, i-iv, compare solution and keep the best at each step. Something like this in a procedural language:

1. find the nearst resistor for the given value (8 ) => first solution
2. find a combination of 3 resistors near the given value (5 i) => second solution
3. compare solution, first and second, keep the best drop otherone
4. you have any other combination of 3 resistors? then go to step 2
... continue same way with each of 5 ii-iv and keep the best solution

I don't recognise the language you use for coding, is a constraint language? seems procedural for me.
This is a constraint programing problem. The best and simple way is to use a constraint language which have optimised solvers. But this is just my opinion, you already go too far to drop all your work now and start from begining, :)

best regards