In: Computer Science
Suppose you have a voltage source V = 120V with an internal resistance RS of 50 ohms, supplying a load of resistance RL . Create a program that outputs a plot of the power PL supplied to the load as a function of the load resistance RL for RL = 1 to 100 ohms in steps of 1 ohm. The power PL is given by the equation PL = I 2RL where I = V/(RS + RL ). Write a function to calculate PL for the inputs RL , RS , V. Do not forget to label the axes with units and to create a title for the plot. · y-axis = Watts · x-axis = Ohms · Title = Load Power vs Load Resistance
I am confused on how to make this into a function in matlab. I have the code as a script but am struggling to turn it into a function
Question : To write the Matlab Function.
Solution : The Code Snippet as well as the necessary images are added below:
%Function to calculate Power supplied to the Load RL
% PL is the output and RL,RS,V are the inputs
function [PL] = Calculate(RL,RS,V)
%Initializing Values
V=120;
RS = 50;
%Creating a vector to store PL values
res=zeros(1,100);
%Looping RL through 1 to 100
for RL = 1:100
I = V/(RS+RL); % Calculating I
PL = I*2*RL; % Calculating PL
res(RL)=PL; % Storing PL into the Vector
fprintf('PL for %d = %f\n',RL,PL); % Printing each Iteration using formatted display fprintf
end
%Plotting Values Using bar chart as the result is discrete
bar(res);
% Providing Title
title('Load Power Vs Load Resistance');
% Labelling x and y axis
xlabel('Ohms');
ylabel('Watts');
Output :
If " ans =160 " is not required then change the function as
" function [ ] = Calculate( RL,RS,V ) "