% plot_demo.m
%	This file contains a brief demonstration of MATLAB plotting

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     Title:	ps1.m
%   Created:	Tuesday, January 20, 1998
%   Project:	18-205, Mathematical Software in Engineering, Spring 1998
%   Purpose:	Demonstrate a variety of MATLAB plotting commands
% 		and the use of an m-file as a script.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Just as commands can be entered on the command line, commands
% can be entered in a script file.  When performing analysis it
% is often easier to enter the commands in a script file, and
% rerun the file (rather than retype a variety of commands).

% This file analyzes a circuit with a resistor and a diode.
% The diode is characterized by the equation:
%	i = Io{Exp[beta*V] - 1}

% The KCL equation for the circuit is given by:
%	i = (Vin - V)/R

% Now, enter all required parameters:
Io = 100e-6	% Amps (100 microamps -- entered in scientific notation)
Vin = 5		% Volts
beta = 2	% diode characteristic
R = 1e3		% Ohms (choose a 1k-Ohm resistor for this analysis)
		% what happens when you change 1k to 10k?

% Choose a range of values of V to analyze the circuit over
V = -5:0.001:10;	
% The result of the above command creates a vector of values to 
% plot.  The first number in the sequence (-5) describes the value
% the vector should begin at.  The second number in the sequence
% (0.001) indicates the step size.  The final number indicates
% the end of the range (10).  Thus, the above line creates a vector
% starting at -5V in increments of 0.001V to 10V.  The step size
% must be controlled so as to obtain the correct output (too many
% or too few points are not good)

% Compute values for i from the diode equation.
i1 = Io.*(exp(beta.*V) - 1);
% Note the use of the .* operator.  This indicates to multiply
% each element in the vector V, and not perform vector multiplication.

% Determine the values for i from the KCL equation.
i2 = (Vin - V)./R;
% Note the ./ operator in the above function.  Also note in the above
% three equations, a ";" was placed at the end of the line.  This
% supresses the output of the function or operation.

% Now, plot the data.
plot(V,i1,V,i2);

% Whenever a plot is created, it is useful to label the plot as:
xlabel('Voltage (V)');
ylabel('Current (A)');
title('Current as a function of Voltage for Series Resistor-Diode Circuit');
legend('i1 (diode equation)','i2 (KCL equation)');

% use the axis command to zoom in on the data
axis([-2,2,-100e-3,100e-3]);

% you might have to try more than once
print plot_demo_1.ps
disp('press any key to continue');
pause
axis([1,2.5,-10e-3,10e-3]);

disp('now use the zoom command, and the ginput command to obtain');
disp('the intersection values of the two lines');

% end of file