%ee93wx1.m ee93wx1 solution, 93,4,21 rev tlyeh 93,4,25
% call ee93wx1q.m for data and data plot
% ['question 2, LED v-i data:'],
% [' * R1f, R1r are R values in series with LED to convert iD to v for foreward and reverse region respectively,'],
% [' x:vD1,iD1: 順向不亮的範圍, +:vD2,iD2:順向到達開始微亮的範圍, o:vD3,iD3:順向到達極亮的範圍, *:vDr,iDr: 反向產波器能測到的範圍,'], 
% [' * (v_DH1,v_DV1) (v_DH2,v_DV2) (v_DH3,v_DV3) (v_DHr,v_DVr) are scope H & V scale for dark, little light, max light, and max reverse, respectively,'],
% ['data points, in unit of Division, cover knee area and asymptotically straight section.']
%figure(1), plot data1 data2 data3 datar, hold off, title('LED v~i'), ylabel('-i'),xlabel(' x:vD1,iD1: 順向不亮, +_2:順向到達微亮, o_3:順向達極亮, *_r:反向'); 
%figure(2), plot data1 data2, hold off, title('LED v~i'), ylabel('-i'),xlabel(' x:vD1,iD1: 順向不亮, +_2:順向到達微亮'); 
ee93wx1q
%2.b important data: vD_rev_max, iD_不亮_max, iD_亮_min, iD_亮_max
vDr(2),iDr(2), vD1(5),iD1(5), vD2(7),iD2(7), vD3(9),iD3(9), 
% sorting data by index k into datas=data(:,k)'; and kinv: index of the location of a data() element in datas
t=size(data1);ndata1=t(2), t=size(data2);ndata2=t(2), t=size(data3);ndata3=t(2), t=size(datar);ndatar=t(2), t=size(k); ndata=t(1),
 [t,kinv]=sort(k); 
%2.c 亮: 微亮的範圍 last one to 極亮的範圍 last one
kinv_on=[kinv(ndata1+ndata2):ndata]',
Y_on=datav(kinv_on);A_on=[ones(size(Y_on)),datai(kinv_on)]; 
 theda_on=inv(A_on'*A_on)*A_on'*Y_on, VD_on=theda_on(1), RD_on=theda_on(2),
figure(1), hold on, plot(Y_on,-((Y_on-VD_on*ones(size(Y_on)))/RD_on),'r')
 axis([min(datav), max(datav), -max(datai), -min(datai)]), hold off
%2.d 順向不亮的範圍: datas(:,(kinv(1):kinv(ndata1))
%*** note: this model must pass through origin
kinv_off=[kinv(1):kinv(ndata1)]',
Y_off=datav(kinv_off);A_off=[datai(kinv_off)]; 
 RD_off=inv(A_off'*A_off)*A_off'*Y_off, 
figure(2), hold on, plot(Y_off,-(Y_off/RD_off),'b')
  plot(vD2',-((vD2-VD_on*ones(size(vD2)))/RD_on)','r')
 axis([min(vD2), max(vD2), -max(iD2), -min(iD2)]), hold off
%2.e 反向的範圍:
Y_r=vDr'; A_r=iDr';
 RD_rev=inv(A_r'*A_r)*A_r'*Y_r, 
figure(1), hold on, plot(Y_r,-(Y_r/RD_rev),'c')

['3.a']
R_lower_bound=[(5-1.6-3.8)/(0.3e-3)-(12.5+35); (5-1.6-0.22)/(42.2e-3)-(12.5+22)],
R_upper_bound=[(5-1.6-0.22)/(2e-3)-(12.5+22)],
['3.b']  
ROH=35;VOH=3.8; ROL=22; VOL=0.22; Rsc=1e6;Csc=5e-12;
 lamdaH=1/ROH/Csc;lamdaL=1/ROL/Csc; Vth=1.4; 
 tpHL=-log((Vth-VOL)/(VOH-VOL))/lamdaL, 
 tpLH=-log((VOH-Vth)/(VOH-VOL))/lamdaH, 

['3.c']
w=2*pi*1e3;t=[0:.01:1]'*1e-3;VFG=2.5*(cos(w*t)+1); VFG1=ones(size(VFG));
vxH=-.3225806452e-1*VFG-3.677419355*VFG1; vyH=.9216589862*VFG-3.502304147*VFG1;
vxL=-.2052238806e-1*VFG-.2154850746*VFG1; vyL=.9328358209*VFG-.2052238806*VFG1;
figure(3);plot(vxH,vyH,'+'); hold on; plot(vxL,vyL,'x');
 plot((1e3)*t,[VFG,vxH,vyH,vxL,vyL]);hold off

['5.b plot f,a,R v.s. V1']
% v1=[2.4:.1:5.5]'; v11=ones(size(v1));
% a = -.1000000000*(1323402932*v11-1495283419.*v1+327623660.*v1.*v1)./(-12431*v11+14000.*v1)./(-11413*v11+70.*v1);
% f = -6.*(-11413*v11+70.*v1).*(5777.*v1-1400*v11)./(1323402932*v11-1495283419.*v1+327623660.*v1.*v1);
% R = -50000.*(-12431*v11+14000.*v1)./(-11413*v11+70.*v1);
%figure(6),hold off
% subplot(3,1,1),plot(v1,a,'+'),ylabel('+:a'), xlabel('v1'),
% subplot(3,1,2),plot(v1,f,'o'),ylabel('o:f')
% subplot(3,1,3),plot(v1,R,'x'),ylabel('x:R')

v2=0.4;v3=[-1.5:.1:(v2-0.1)]'; v31=ones(size(v3));
% s/,/;/g  s/[.][*]/*/g  s/[.]+/*v31+/g  s/\//.\//g  s/v3.2)/v3.*v3)/g  s/)[*](/).*(/g
%v1=2.4
R = -10000*(6406*v31+1485*v3)./(-191*v31+495*v3);f = 4*(-191*v31+495*v3).*(-1782*v31+13205*v3)./(-14006546*v31+23818905*v3+35649900*v3.*v3); a = -.4000000000e-1*(-14006546*v31+23818905*v3+35649900*v3.*v3)./(6406*v31+1485*v3)./(-191*v31+495*v3);
%v1=2.5
% R = -200000*(2252*v31+495*v3)./(-1271*v31+3300*v3);f = 5*(-1271*v31+3300*v3)*(1475*v3-198*v31)./(-12501430*v31+21272229*v3+32456490*v3.*v3); a = -.1000000000*(-12501430*v31+21272229*v3+32456490*v3.*v3)./(2252*v31+495*v3)./(-1271*v31+3300*v3);
%v1=3.8
% R = -100000.*(11306*v31+1485*v3)./(-1861*v31+4950*v3);
% f = (-1861*v31+4950*v3).*(87715*v3-11286*v31)./(-79676684*v31+96733890*v3+477410175.*v3.*v3);
% a = -.4000000000e-1*(-79676684*v31+96733890*v3+477410175.*v3.*v3)./(11306*v31+1485*v3)./(-1861*v31+4950*v3);
figure(6),hold off
 subplot(3,1,1),plot(v3,a,'+'),ylabel('+:a'), xlabel('v3'),
 subplot(3,1,2),plot(v3,f,'o'),ylabel('o:f')
 subplot(3,1,3),plot(v3,R,'x'),ylabel('x:R')
