/* * rctrps.java - revised 22 Apr 05 - width 421, height 346 * tests 4 different capacitors by analyzing R-C transients * with nominal 20 ms time constants * @author jackord@kw.igs.net * revised for consistency with Liberty Basic version with eps option */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class rctrps extends Applet implements ActionListener { int xz=400; int yz=320; int np=32; int kk=0; // Declarations double ym=4096; double la, lb, lsy; String b1s = "C1"; Button b1 = new Button(b1s); String b2s = "C2"; Button b2 = new Button(b2s); String b3s = "C3"; Button b3 = new Button(b3s); String b4s = "C4"; Button b4 = new Button(b4s); String b5s = "fit"; Button b5 = new Button(b5s); TextField tx1=new TextField(20); TextField tx2=new TextField(20); char URLSeparator='/'; String fs; double[] x=new double[33]; double[] y=new double[33]; double[] z=new double[33]; public void init() { setBackground(new Color(211, 211, 211)); tx1.setBackground(getBackground()); tx2.setBackground(getBackground()); add(b1); add(b2); add(b3); add(b4); add(b5); add(tx1); add(tx2); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); } public void pin(Graphics g) { b1.setBounds(10, 0, 30, 20); // Screen layout b2.setBounds(45, 0, 30, 20); b3.setBounds(80, 0, 30, 20); b4.setBounds(115, 0, 30, 20); b5.setBounds(150, 0, 30, 20); tx1.setBounds(190, 0, 105, 20); tx2.setBounds(305, 0, 105, 20); tx1.setText("Time Constant"); tx2.setText("Deviation"); g.setColor(Color.black); // Draw grid for (int i=10; i<=xz+10; i=i+40) { g.drawLine(i, 25, i, yz+25); } for (int i=25; i<=yz+25; i=i+40) { g.drawLine(10, i, xz+10, i); } } public void paint(Graphics g) { // Main program int test=0; int y1, y2; double t, dt, yy; double h=0; pin(g); if (kk>0) { for (int i=1; i<=np; i=i+1) { // Plot raw data g.setColor(Color.green); g.fillArc(4*(int)(x[i])+5, (int)(yz*(1-y[i]/ym)+.5)+20, 11, 11, 0, 360); g.setColor(Color.black); g.drawArc(4*(int)(x[i])+5, (int)(yz*(1-y[i]/ym)+.5)+20, 10, 10, 0, 360); } } if (kk==2) { // Vary t for best fit t=x[np]/Math.log(np); dt=t/4; lsq(t); h=lsy; while (test==0) { t=t+dt; lsq(t); if (lsy>h) { dt=-dt/2; if (Math.abs(dt)<(t/100000)) { test=1; lsy=h; t=t+2*dt; } } h=lsy; } lsq(t); for (int i=1; i<=np; i=i+1) { // Plot fitted data g.setColor(Color.blue); g.fillArc((int)(z[i]*xz+.5)+5, (int)(yz*(1-y[i]/ym)+.5)+20, 11, 11, 0, 360); g.setColor(Color.black); g.drawArc((int)(z[i]*xz+.5)+5, (int)(yz*(1-y[i]/ym)+.5)+20, 10, 10, 0, 360); } tx1.setText("t ="+(float)t); tx2.setText("d ="+(float)lsy); g.setColor(Color.red); y1=25+(int)(yz*(1-(la+lb)/ym)+.5); // Fitted line g.drawLine(10, 25+(int)(yz*(1-lb/ym)+.5), 410, y1); for (int i=1; i<=np-1; i=i+1) { // Fitted curve y2=25+(int)(yz*(1-(la*Math.exp(-x[i+1]/t)+lb)/ym)+.5); g.drawLine(10+4*(int)(x[i]), y1, 10+4*(int)(x[i+1]), y2); y1=y2; } for (int i=1; i<=np; i=i+1) { // Scatter plot yy=y[i]-la*z[i]-lb; if (lsy>3) { // Divide by 20 for C4 yy=yy/20; } yy=185-40*yy; g.setColor(Color.red); g.fillArc((int)(z[i]*xz+.5)+5, (int)(yy+.5)-5, 11, 11, 0, 360); g.setColor(Color.black); g.drawArc((int)(z[i]*xz+.5)+5, (int)(yy+.5)-5, 10, 10, 0, 360); } kk=0; } } public void lsq(double t) { // Fit y=la*x+lb double l0, l1, l2, l3, l4, ls; for (int i=1; i<=np; i=i+1) { // Convert to exp z[i]=Math.exp(-x[i]/t); } l1=0; l2=0; l3=0; l4=0; ls=0; // Start analysis from for (int i=2; i<=np; i=i+1) { // 2nd point l1=l1+z[i]; l4=l4+y[i]; } l1=l1/(np-1); l4=l4/(np-1); for (int i=2; i<=np; i=i+1) { l0=z[i]-l1; l2=l2+l0*(y[i]-l4); l3=l3+l0*l0; } la=l2/l3; lb=-la*l1+l4; for (int i=2; i<=np; i=i+1) { l0=y[i]-la*z[i]-lb; ls=ls+l0*l0; } lsy=Math.sqrt(ls/(np-3)); } public void actionPerformed(ActionEvent e) { String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; x[1]=0; y[1]=4086; x[2]=1; y[2]=3843; x[3]=2; y[3]=3651; x[4]=3; y[4]=3468; x[5]=4; y[5]=3294; x[6]=5; y[6]=3129; x[7]=6; y[7]=2972; x[8]=7; y[8]=2823; x[9]=8; y[9]=2682; x[10]=9; y[10]=2548; x[11]=10; y[11]=2420; x[12]=11; y[12]=2299; x[13]=12; y[13]=2184; x[14]=13; y[14]=2075; x[15]=14; y[15]=1971; x[16]=15; y[16]=1873; x[17]=16; y[17]=1779; x[18]=17; y[18]=1690; x[19]=18; y[19]=1606; x[20]=19; y[20]=1526; x[21]=20; y[21]=1450; x[22]=21; y[22]=1377; x[23]=23; y[23]=1243; x[24]=25; y[24]=1123; x[25]=27; y[25]=1014; x[26]=30; y[26]=870; x[27]=33; y[27]=747; x[28]=37; y[28]=610; x[29]=41; y[29]=498; x[30]=47; y[30]=368; x[31]=55; y[31]=247; x[32]=69; y[32]=125; } if (b2s.equals(tst)) { kk=1; x[1]=0; y[1]=4086; x[2]=1; y[2]=3842; x[3]=2; y[3]=3650; x[4]=3; y[4]=3468; x[5]=4; y[5]=3295; x[6]=5; y[6]=3131; x[7]=6; y[7]=2975; x[8]=7; y[8]=2827; x[9]=8; y[9]=2686; x[10]=9; y[10]=2552; x[11]=10; y[11]=2425; x[12]=11; y[12]=2305; x[13]=12; y[13]=2190; x[14]=13; y[14]=2081; x[15]=14; y[15]=1978; x[16]=15; y[16]=1880; x[17]=16; y[17]=1787; x[18]=17; y[18]=1698; x[19]=18; y[19]=1614; x[20]=19; y[20]=1534; x[21]=20; y[21]=1458; x[22]=21; y[22]=1385; x[23]=23; y[23]=1252; x[24]=25; y[24]=1131; x[25]=27; y[25]=1022; x[26]=30; y[26]=878; x[27]=33; y[27]=755; x[28]=37; y[28]=617; x[29]=41; y[29]=505; x[30]=47; y[30]=374; x[31]=55; y[31]=252; x[32]=70; y[32]=123; } if (b3s.equals(tst)) { kk=1; x[1]=0; y[1]=4086; x[2]=1; y[2]=3870; x[3]=2; y[3]=3698; x[4]=3; y[4]=3536; x[5]=4; y[5]=3381; x[6]=5; y[6]=3233; x[7]=6; y[7]=3092; x[8]=7; y[8]=2957; x[9]=8; y[9]=2829; x[10]=9; y[10]=2706; x[11]=10; y[11]=2588; x[12]=11; y[12]=2476; x[13]=12; y[13]=2368; x[14]=13; y[14]=2266; x[15]=14; y[15]=2168; x[16]=15; y[16]=2074; x[17]=16; y[17]=1985; x[18]=17; y[18]=1899; x[19]=19; y[19]=1739; x[20]=21; y[20]=1593; x[21]=22; y[21]=1524; x[22]=24; y[22]=1397; x[23]=27; y[23]=1226; x[24]=29; y[24]=1124; x[25]=32; y[25]=988; x[26]=35; y[26]=868; x[27]=38; y[27]=764; x[28]=43; y[28]=618; x[29]=48; y[29]=501; x[30]=55; y[30]=375; x[31]=65; y[31]=250; x[32]=83; y[32]=124; } if (b4s.equals(tst)) { kk=1; x[1]=0; y[1]=4086; x[2]=1; y[2]=3875; x[3]=2; y[3]=3709; x[4]=3; y[4]=3562; x[5]=4; y[5]=3428; x[6]=5; y[6]=3304; x[7]=6; y[7]=3188; x[8]=7; y[8]=3078; x[9]=8; y[9]=2974; x[10]=9; y[10]=2873; x[11]=10; y[11]=2776; x[12]=11; y[12]=2681; x[13]=13; y[13]=2494; x[14]=14; y[14]=2399; x[15]=15; y[15]=2302; x[16]=17; y[16]=2103; x[17]=18; y[17]=2003; x[18]=19; y[18]=1904; x[19]=21; y[19]=1721; x[20]=22; y[20]=1640; x[21]=24; y[21]=1497; x[22]=26; y[22]=1376; x[23]=28; y[23]=1271; x[24]=31; y[24]=1135; x[25]=34; y[25]=1018; x[26]=38; y[26]=886; x[27]=43; y[27]=749; x[28]=48; y[28]=635; x[29]=55; y[29]=506; x[30]=64; y[30]=379; x[31]=77; y[31]=251; x[32]=100;y[32]=125; } if (b5s.equals(tst)) { if (kk==1) { kk=2; } else { kk=0; } } repaint(); } }