% OscillatorI2
% This program plots the voltage and current in an LC
% LC circuit 
% (a) as a function of time, (b) as a phase plot

% Lin = inductance (in mH)
% Cap = capacitance (in pF)
% Vamp = voltage amplitude (in volts)
% phi0 = phase offset

% Vplot = Voltage amplitudes for phase plot
% Nplot = number of phase plots

% Nosc = number of oscillations plotted
% Npoints = number of points in each period

% omega = radial oscillation frequency (in kHz)
% Iamp = current amplitude (in uA)

close all   %this clears previous figures

Lin = 1, Cap = 100, Vamp = 1; phi0 = 0,
L = Lin*1e-3; C = Cap*1e-12; om = 1/sqrt(L*C); 
Imp = sqrt(L/C); Ia = Vamp/Imp;
   % fundamental quantities in SI units
omega = 1e-6*om, %convert frequency to MHz.
Iamp = 1e6*Ia,  %convert current amplitude to uA

Nosc = 5, Npoints = 100, Ntot = Nosc*Npoints + 1;
   % set the plotting parameters
   
Nt = 1:Ntot; time = 2*pi*(Nt-1)/Npoints;
tphys = time/omega;  % physical time
   % set up the time arrays
   
Voltage = Vamp*cos(time + phi0);
Current = -Iamp*sin(time + phi0);
   % voltage and current as functions of time

subplot(2,1,1)
plot(tphys,Voltage,'linewidth',3,'color','r')
set(gca,'fontname','Times New Roman','fontsize',20);
set(gca,'LineWidth',1.5,'TickLength',[0.025 0.025]); 
title('Voltage','fontname','Times New Roman','fontsize',20)
xlabel('time (\mus)','fontname','Times New Roman','fontsize',20);
ylabel('Voltage (V)','fontname','Times New Roman','fontsize',20);
axis tight;
 xlim([0 10])
 ylim([-1 1])
set(gcf, 'WindowStyle', 'normal');
set(gca, 'Unit', 'inches');
set(gca, 'TickLabelInterpreter','LaTeX',...
    'YTick',[-1:1:1],'YTickLabel',{'$-1$','$0$','$1$'})
set(gcf, 'PaperPosition',1.9*[0 0 4 3],'PaperSize',1.9*[4 3])

subplot(2,1,2)
plot(tphys,.001*Current,'linewidth',3,'color','r')
set(gca,'fontname','Times New Roman','fontsize',20);
set(gca,'LineWidth',1.5,'TickLength',[0.025 0.025]);
title('Current','fontname','Times New Roman','fontsize',20)
xlabel('time (\mus)','fontname','Times New Roman','fontsize',20);
ylabel('Current (mA)','fontname','Times New Roman','fontsize',20);
axis tight;
xlim([0 10])
ylim([-.500 .500])
set(gcf, 'WindowStyle', 'normal');
set(gca, 'Unit', 'inches');
set(gca, 'TickLabelInterpreter','LaTeX',...
    'YTick',[-.500:.500:.500],'YTickLabel',{'$-0.5$','$0$','$0.5$'})
set(gcf, 'PaperPosition',1.9*[0 0 4 3],'PaperSize',1.9*[4 3])
saveas(gcf,'1.pdf','pdf')


Vplot=[0.5 0.7 0.9], Nplot=3,
% Set the values for the phase plots
%figure
%axis square
close all
for Np=1:Nplot
    Vpl=Vplot(Np)*Voltage(1:Ntot);
    Ipl=.001*Vplot(Np)*Current(1:Ntot);
    plot(Ipl,Vpl,'linewidth',3)
    hold on

end
hold off
annotation('arrow','Position',[0.739,0.5,0,0.005])
% text(150.0,0.8,'V_0=0.9','fontname','Times New Roman','fontsize',16)
annotation('arrow','Position',[0.689,0.5,0,0.005])
% text(85.0,0.7,'V_0=0.7','fontname','Times New Roman','fontsize',16)
annotation('arrow','Position',[0.640,0.5,0,0.005])
% text(10.0,0.55,'V_0=0.5','fontname','Times New Roman','fontsize',16)
set(gca,'fontname','Times New Roman','fontsize',20);
set(gca,'LineWidth',1.5,'TickLength',[0.025 0.025]);
title('Phase plot:  Voltage vs. Current','fontname','Times New Roman','fontsize',20)
xlabel('Current (mA)','fontname','Times New Roman','fontsize',20)
ylabel ('Voltage (V)','fontname','Times New Roman','fontsize',20)
xlim([-.500 .500]) %define limits for x axis
ylim([-1 1]) %define limits for y axis
set(gcf, 'WindowStyle', 'normal');
set(gca, 'Unit', 'inches');
set(gca, 'TickLabelInterpreter','LaTeX',...
    'YTick',[-1:.5:1],'YTickLabel',{'$-1$','$-0.5$','$0$','$0.5$','$1$'})

set(gca, 'TickLabelInterpreter','LaTeX',...
    'XTick',[-.5:.25:.5],'XTickLabel',{'$-0.5$','$-0.25$','$0$','$0.25$','$0.5$'}) %define the ticks and use LaTeX interpreter to have real minus signs
set(gcf, 'PaperPosition',1.9*[0 0 4 3],'PaperSize',1.9*[4 3]) %define the paper size and position
lgd = legend('$V_0=0.5$ V','$V_0=0.7$ V','$V_0=0.9$ V'); %add the legend
% legend('Location','northwest')
set(lgd,'interpreter','latex','FontSize',17);%     set(lgd,'interpreter','latex');
legend ('boxoff'); %turn the box around the elegend off
saveas(gcf,'2.pdf','pdf') %save file as a .pdf file


