Advertisement
hiddenGem

Two Resistors in Series

Jun 17th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.03 KB | None | 0 0
  1. %% Two Resistors in Series
  2. % Calculates the electric current and total potential difference across the
  3. %   two resistors. Notes and clarifications at the end of code.
  4.  
  5. % Inputs and Variables
  6. resis = input('Input resistances in brackets\n');
  7. a = input('Given the Voltage of R1 or R2?\n');
  8. switch a
  9.     case 1
  10.         V(1) = input('Voltage across R1\n');
  11.         I = V(1)/resis(1);
  12.         V(2) = I * resis(2);
  13.     case 2
  14.         V(2) = input('Voltage across R2\n');
  15.         I = V(2)/resis(2);
  16.         V(1) = I * resis(1);
  17. end
  18. Vsum = sum(V);
  19. fprintf(['The current and individual and total voltages are \n', ...
  20.        '%.3e A,  %.3i V, %.3i V, %.3i V'], I, V, Vsum)
  21.  
  22. %{
  23. The user must enter the resistances in brackets [] for the code to work
  24. The outputs are rounds to three decimal places. This can be adjust through
  25.     changing for removing the .# in the last fprintf line.
  26. The output lists current through both, voltage through the first
  27.     resistor, voltage through the second resistor, and the total
  28.     voltage through the series.
  29. %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement