Repair a two-output calculation

Mark Yourself Question

  1. Grab a piece of paper and try this problem yourself.
  2. When you're done, check the "I have answered this question" box below.
  3. View the solution and report whether you got it right or wrong.

An engineer saves this code in resistorReport.m to calculate resistor power and energy:


MATLAB
function [P,E] = resistorReport(I,R,t)
    P = I^2*R;
    energy = P*t;
end


Inputs are scalar current I in A, resistance R in ohms, and duration t in s. The intended outputs are power P in W followed by energy E in J. Assume constant current and resistance.


The caller executes:


MATLAB
[energy,power] = resistorReport(1.5,12,40);


Explain why the call fails, correct both the function body and the caller's output list, and calculate the values that should be stored in power and energy. Also state whether the successful call creates a variable named I in a caller workspace that previously had no I.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More User Defined Functions and Function Files Questions: