/* program for finding the eigen-frequencies of a "guitar" * * by using the Numerov- and Schiess-Algorithms * * * * @author: Christoph Federrath */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class Guitar extends Applet implements Runnable, ActionListener { private int W=500, H=300, i=0; //window-size, order i private double b, bstart=0.0; //eigen-frequency private double a=1.0, h=0.01, dbstart=0.1, db; private double y, ym1, yp1, x, xp1, xm1, ymax=1, xmax=1; private boolean begin = true; Thread t; Canvas can; Button but_next, but_previous; private Graphics g; public void init() { setLayout(null); can = new Canvas(); but_next = new Button("Next"); but_previous = new Button("Previous"); can.setBackground(Color.white); can.setBounds(1,1,W,H); but_next.setBounds(W+20,H/2-35,80,30); but_previous.setBounds(W+20,H/2+5,80,30); add(can); add(but_next); add(but_previous); but_next.addActionListener(this); but_previous.addActionListener(this); g = can.getGraphics(); g.setColor(Color.blue); g.setFont(new Font("Verdana",Font.BOLD,12)); db=dbstart; b=bstart; //initial values }//init() public void run() { try{Thread.sleep(50);} catch(InterruptedException e){} g.setColor(Color.blue); g.clearRect(0,0,W,H); g.drawRect(0,0,W-1,H-1); g.drawLine(0,H/2,W,H/2); if(begin) //only one time { begin = false; y = 0.01; //y > 0 i = 1; while(db > 1.0e-7) { if(y > 0) { b = b+db; run(); } else { b = b-db; db = db/10; b = b+db; run(); } }//while } int px_old, px, py_old, py; x=h; ym1=0.0; y=2.0*h; px_old=0; py_old=H/2; yp1=1.0; while(x <= 1) //Numerov-algorithm uses function k(x) { xp1 = x+h; xm1 = x-h; yp1 = ((2.-5./6.*h*h*k(x))*y-(1.+h*h/12.*k(xm1))*ym1)/(1.+h*h/12.*k(xp1)); xm1 = x; x = xp1; ym1 = y; y = yp1; px = (int)(x/xmax*W); py = H/2 -(int)(y/ymax*H/2); g.setColor(Color.red); g.drawLine(px_old,py_old,px,py); //draw points px_old = px; py_old = py; }//while print(); //print values }//run() public void actionPerformed(ActionEvent evt) { if(evt.getActionCommand()==but_next.getActionCommand()) { if(db < 1.0e-7) { db = dbstart; b = b+db; run(); //sets a new value for y int w; w = i%2; //i odd or even i++; if(w == 0) //i == odd { while(db > 1.0e-7) //Schiess-algorithm { if(y > 0) { b = b+db; run(); } else { b = b-db; db = db/10; b = b+db; run(); } }//while }//if i == odd if(w == 1) //i == even { while(db > 1.0e-7) //Schiess-algorithm { if(y < 0) { b = b+db; run(); } else { b = b-db; db = db/10; b = b+db; run(); } }//while }//if i == even }//if db < 10^-7 }//but_next if(evt.getActionCommand()==but_previous.getActionCommand()) { if(i <= 1){} //only positive values for i are allowed else { db = dbstart; b = b-db; run(); //sets a new value for y int w; w = i%2; //i odd or even i--; if(w == 0) //i == odd { while(db > 1.0e-7) //Schiess-algorithm { if(y < 0) { b = b-db; run(); } else { b = b+db; db = db/10; b = b-db; run(); } }//while }//if i == odd if(w == 1) //i == even { while(db > 1.0e-7) //Schiess-algorithm { if(y > 0) { b = b-db; run(); } else { b = b+db; db = db/10; b = b-db; run(); } }//while }//if i == even }//else i > 1 }//but_previous }//actionPerformed() void print() { g.setColor(Color.blue); g.drawString("order:",60,H-30); g.drawString("i = "+String.valueOf(i),60,H-10); g.drawString("frequency:",W/2-60,H-30); g.drawString("b = "+String.valueOf((float)b),W/2-60,H-10); g.drawString("accuracy:",W/2+110,H-30); g.drawString("db = "+String.valueOf((float)db),W/2+110,H-10); }//print() double k(double x) //function k(x) in the diff. equ. y''(x)+k(x)*y(x) = 0 { return b*b*Math.PI*Math.PI*(1+a*(Math.sin(Math.PI*x))*(Math.sin(Math.PI*x))); }//k(x) public void start() { t = new Thread(this); t.start(); }//start() }