2. (35 points) Electrical resistors are said to be connected \"in series\" if th
ID: 3348740 • Letter: 2
Question
2. (35 points) Electrical resistors are said to be connected "in series" if the same current flows through each of them and "in parallel" if the same voltage is applied across cach. If in series, they are equivalent to a single resistor whose resistance given by R-R1+R2 +Rs+R+RsR If in parallel, their equivalent resistance is given by +-+-???? +? Write an m-file that prompts the user for the type of connection (series or parallel) and the number of resistors n and then computes the equivalent resistance (assume the individual resistances are 2 ohms). Use switch statement for the connection and for loop for the resistance calculationExplanation / Answer
Matlab code
clc;clear all;% clearing the workspace and command window
% Prompting the user to enter the type of the connection
Connection = input('Enter the type of the connection (series or parallel): ','s');
% prompting the user to enter the number of resistors in the circut
N = input('Enter the number of resistors: ');
% Prompting the user to enter the values of resistors
fprintf('Enter the values of the resistors ');
for k =1:N
Res(k) = input(': '); % reading the values of the resistance
end
ext =1; % loop exit parameters
while ext % Continue loop until user enteres a valid circuit type
if strcmp(Connection,'series') % if the connection is series
R = sum(Res); % The equivalent resistence is the sum of resistance
ext = 0; % And exit the loop
elseif strcmp(Connection,'parallel') % if the connection is parallel
R = 1/(sum(1./Res)); % Compute the equivalent resistance
ext = 0; % And exit the loop
else % IF the connection entered is not parallel and serial then
% prompting the user to enter a valid connection type
Connection = input('Please enter series or parallel: ','s'); % Reading again
end
end
fprintf('The equivalent resistance R = %f',R); % Printing the equivalent resistance val
Testing the code
Enter the type of the connection (series or parallel): parallel
Enter the number of resistors: 2
Enter the values of the resistors
: 4
: 4
The equivalent resistance R = 2.000000>>
Enter the type of the connection (series or parallel): series
Enter the number of resistors: 2
Enter the values of the resistors
: 4
: 4
The equivalent resistance R = 8.000000>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.