Sabtu, 26 Juni 2010

VISUALISASI AKAR POLINOMIAL DENGAN 
METODE REGULA FALSI 
Source code diimplementasikan dengan matlab 7.0 
while 1
clear, clf,clc
fprintf( '\n=============================================' ); 
   fprintf( '\n     VISUALISASI AKAR PERSAMAAN POLINOMIAL   ' ); 
   fprintf( '\n                 menggunakan              ' );
   fprintf( '\n             METODE REGULA FALSI           ' );     
   fprintf( '\n        oleh : FEBRIYANTO                  ' );
   fprintf( '\n       fungsi f(X) disimpan di fcn.m       ' );
   fprintf( '\n============================================= \n' ); 
      x0 = input ('input tebakan x0  : ');
      x1 = input ('input tebakan x1  : ');
      fprintf(' nilai f(x0)=%10g',fcn(x0));
      fprintf('\n nilai f(x1)=%10g ',fcn(x1));
      d=fcn(x0)*fcn(x1);
      fprintf('\n nilai fcn(x0)*fcn(x1) %10g',d);
      if (d< 0);
      maxerror = input('\n input nilai error: ');
       maxit = input('input N max      : ');
      fplot('fcn',[-10 10]); grid on; hold on;
xlabel('SUMBU X');
ylabel('SUMBU Y');
title('VISUALISASI AKAR DENGAN METODE REGULA FALSI');
count = 0;         
rel_error = 1;     
c = x1;           
fprintf(1,'\n============================================================================'); 
fprintf('\n  No_ITerasi        X0               X1              Xr             error\n');
while (rel_error > maxerror) & (count < maxit)
  count = count + 1;
  xrold = c;
  c = x1 - fcn(x1)*(x1-x0)/(fcn(x1)-fcn(x0));
  plot(c,fcn(c),'ro');

  if c ~= 0
    rel_error = abs((c - xrold)/c) * 100;
  end
    fprintf('%3g           %10g     %10g      %10g         %10.4f\n',count,x0,x1,c,rel_error)
    test = fcn(x0) * fcn(c); 
    if test == 0
    rel_error = 0;
  elseif test < 0
    x1 = c;       
  else
    x0 = c;       
  end
end
fprintf(1,'\n============================================================================ \n'); 
fprintf(1,'                  Hasilnya (x)     :%10g   \n',c) ;
fprintf(1,'                dengan toleransi   :%10.4f \n',rel_error) ;
fprintf(1,'                  dengan iterasi   :  %3g   \n',count) ;
if rel_error>maxerror
    fprintf(1,'                                 Proses  GAGAL  \n') ;
else
    fprintf(1,'                                 Proses  SUKSES  \n') ;
end
fprintf(1,'=============================================================================');
 kont1 = input( '\n Tekan 1 untuk lanjut,0 untuk berhenti:' );  
   if kont1 == 0, break; end 
      else
      fprintf('\n Nilai x0 dan x1 tidak memenuhi persyaratan');
      fprintf('\n Syarat Regula Falsi adalah fcn(x0)*fcn(x1)< 0');
      fprintf('\n input nilai x0 dan x1 kembali');
     x0 = input('\ninput tebakan x0  : ');
      x1 = input('input tebakan x1  : ');
      fprintf('\n nilai f(x0)=%10g',fcn(x0));
      fprintf('\n nilai f(x1)=%10g',fcn(x1));
      maxerror = input('\n input nilai error: ');
      maxit = input('input N max      : ');
      fplot('fcn',[-10 10]); grid on; hold on;
xlabel('SUMBU X');
ylabel('SUMBU Y');
title('VISUALISASI AKAR DENGAN METODE REGULA FALSI');
count = 0;         
rel_error = 1;     
c = x1;           
fprintf(1,'\n============================================================================'); 
fprintf('\n  No_ITerasi        X0               X1              Xr             error\n');
while (rel_error > maxerror) & (count < maxit)
  count = count + 1;
  xrold = c;
  c = x1 - fcn(x1)*(x1-x0)/(fcn(x1)-fcn(x0));
  plot(c,fcn(c),'ro');
  if c ~= 0
    rel_error = abs((c - xrold)/c) * 100;
  end
    fprintf('%3g           %10g     %10g      %10g         %10.4f\n',count,x0,x1,c,rel_error)
    test = fcn(x0) * fcn(c); 
    if test == 0
    rel_error = 0;
  elseif test < 0
    x1 = c;       
  else
    x0 = c;       
  end
end
fprintf(1,'\n============================================================================ \n'); 
fprintf(1,'                  Hasilnya (x)     :%10g   \n',c) ;
fprintf(1,'                dengan toleransi   :%10.4f \n',rel_error) ;
fprintf(1,'                  dengan iterasi   :  %3g   \n',count) ;
if rel_error>maxerror
    fprintf(1,'                                 Proses  GAGAL  \n') ;
else
    fprintf(1,'                                 Proses  SUKSES  \n') ;
end
fprintf(1,'=============================================================================');
 kont1 = input( '\n Tekan 1 untuk lanjut,0 untuk berhenti:' );  
   if kont1 == 0, break; end 
      end
end
====Semoga Bisa Membantu====

Sabtu, 19 Juni 2010

Kalkulator dengan NetBeans7

Berikut sourcecode untuk kalkulator sederhana dengan netbeans7

class GUI extends javax.swing.JFrame {

/** Bikin form GUI */
public GUI() {
initComponents();
tf.setText("0");
}
// Method ini dipanggil untuk menginisialisasi form
private void initComponents() {
tf = new javax.swing.JTextField();
bC = new javax.swing.JButton();
b7 = new javax.swing.JButton();
b8 = new javax.swing.JButton();
b9 = new javax.swing.JButton();
b4 = new javax.swing.JButton();
b5 = new javax.swing.JButton();
b6 = new javax.swing.JButton();
b1 = new javax.swing.JButton();
b2 = new javax.swing.JButton();
b3 = new javax.swing.JButton();
bBagi = new javax.swing.JButton();
bKali = new javax.swing.JButton();
bMin = new javax.swing.JButton();
bAdd = new javax.swing.JButton();
b0 = new javax.swing.JButton();
bEqual = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tf.setEditable(false);

bC.setText("C");
bC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCActionPerformed(evt);
}
});

b7.setText("7");
b7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b7ActionPerformed(evt);
}
});

b8.setText("8");
b8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b8ActionPerformed(evt);
}
});

b9.setText("9");
b9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b9ActionPerformed(evt);
}
});

b4.setText("4");
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b4ActionPerformed(evt);
}
});

b5.setText("5");
b5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b5ActionPerformed(evt);
}
});

b6.setText("6");
b6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b6ActionPerformed(evt);
}
});

b1.setText("1");
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b1ActionPerformed(evt);
}
});

b2.setText("2");
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b2ActionPerformed(evt);
}
});

b3.setText("3");
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b3ActionPerformed(evt);
}
});

bBagi.setText("/");
bBagi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bBagiActionPerformed(evt);
}
});

bKali.setText("x");
bKali.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bKaliActionPerformed(evt);
}
});

bMin.setText("-");
bMin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bMinActionPerformed(evt);
}
});

bAdd.setText("+");
bAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bAddActionPerformed(evt);
}
});

b0.setText("0");
b0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b0ActionPerformed(evt);
}
});

bEqual.setText("=");
bEqual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bEqualActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tf, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(b7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bBagi, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
.addComponent(bC)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(b0)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bEqual, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(b1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b3)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
.addComponent(bMin, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(b4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bKali, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bC)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b7)
.addComponent(b8)
.addComponent(b9)
.addComponent(bBagi))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b4)
.addComponent(b5)
.addComponent(b6)
.addComponent(bKali))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b1)
.addComponent(b2)
.addComponent(b3)
.addComponent(bMin))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b0)
.addComponent(bEqual)
.addComponent(bAdd))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}

// method untuk menangani event tombol '=' ketika di klik
private void bEqualActionPerformed(java.awt.event.ActionEvent evt){
inp=Double.parseDouble(tf.getText());
total=proses(inp,operator,total);
tf.setText(""+total);
input="";
operator="";
}

// method untuk menangani event tombol '/' ketika di klik
private void bBagiActionPerformed(java.awt.event.ActionEvent evt) {
inp=Double.parseDouble(tf.getText());
if(operator.equals(""))
{
operator="/";
total=inp;
}
else
{
total=proses(inp,operator,total);
operator="/";
}
tf.setText(""+total);
input="";
}

// method untuk menangani event tombol 'x' ketika di klik
private void bKaliActionPerformed(java.awt.event.ActionEvent evt) {
inp=Double.parseDouble(tf.getText());
if(operator.equals(""))
{
operator="*";
total=inp;
}
else
{
total=proses(inp,operator,total);
operator="*";
}
tf.setText(""+total);
input="";
}

// method untuk menangani event tombol '-' ketika di klik
private void bMinActionPerformed(java.awt.event.ActionEvent evt) {
inp=Double.parseDouble(tf.getText());
if(operator.equals(""))
{
operator="-";
total=inp;
}
else
{
total=proses(inp,operator,total);
operator="-";
}
tf.setText(""+total);
input="";
}

// method untuk menangani event tombol '+' ketika di klik
private void bAddActionPerformed(java.awt.event.ActionEvent evt) {
inp=Double.parseDouble(tf.getText());
if(operator.equals(""))
{
operator="+";
total=inp;
}
else
{
total=proses(inp,operator,total);
operator="+";
}
tf.setText(""+total);
input="";
}


// method untuk menangani event tombol '0-9' ketika di klik
private void b0ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"0";
tf.setText(input);
}

private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"3";
tf.setText(input);
}

private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"2";
tf.setText(input);
}

private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"1";
tf.setText(input);
}

private void b6ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"6";
tf.setText(input);
}
private void b5ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"5";
tf.setText(input);
}

private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"4";
tf.setText(input);
}

private void b9ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"9";
tf.setText(input);
}

private void b8ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"8";
tf.setText(input);
}

private void b7ActionPerformed(java.awt.event.ActionEvent evt) {
input=input+"7";
tf.setText(input);
}

private void bCActionPerformed(java.awt.event.ActionEvent evt) {
tf.setText("0");
operator="";
total=0;
}

/**
* @param args the command line arguments
*/
public double proses(double input,String operator,double total)
{
double jum=0;
if(operator.equals("+"))
jum=total+input;
else if(operator.equals("-"))
jum=total-input;
else if(operator.equals("*"))
jum=total*input;
else if(operator.equals("/"))
jum=total/input;
return jum;
}

// Variables declaration - do not modify//
private javax.swing.JButton b0;
private javax.swing.JButton b1;
private javax.swing.JButton b2;
private javax.swing.JButton b3;
private javax.swing.JButton b4;
private javax.swing.JButton b5;
private javax.swing.JButton b6;
private javax.swing.JButton b7;
private javax.swing.JButton b8;
private javax.swing.JButton b9;
private javax.swing.JButton bAdd;
private javax.swing.JButton bBagi;
private javax.swing.JButton bC;
private javax.swing.JButton bEqual;
private javax.swing.JButton bKali;
private javax.swing.JButton bMin;
private javax.swing.JTextField tf;

String input="";
String operator="";
double inp;
double total=0;
}

public class Utama {

/** Creates a new instance of Utama */
public Utama() {
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
} // TODO code application logic here
}