Wednesday, January 30, 2013

Mengapa jangan pakai GUIDE di MATLAB

Berikut ini saya berikan contoh salah satu alasan untuk menghindari penggunaan GUIDE di MATLAB.

Adapun source nya:

Belajar baca dan tulis file excel dengan MATLAB

Berikut ini adalah tutorial membaca dan menulis file excel dengan MATLAB. Sebenarnya ada cara lain yang lebih bagus. Cuma anda boleh menggunakan cara ini sebagai perbandingan. Kendatipun hakikatnya intinyapun sama (menggunakan ActiveX ).

Tuesday, January 29, 2013

Belajar GUI di MATLAB

Pada tulisan kali ini saya akan memberikan sebuah tutorial sederhana mengenai pemrogaraman GUI di MATLAB. Kebetulan ini tutorial pertama tentang GUI, jadi saya ambil satu contoh familiar yakni bagaimana memplot fungsi sinusoidal dengan GUI. Sebenarnya kalo anda ingin pahami benar bagaimana GUI di MATLAB bekerja, ada baiknya jangan gunakan GUIDE (gui builder default). MEndingan langsung koding langsung dari scriptnya. Di samping membantu pemahaman anda ---- khususnya jika GUI yang dibuat makin rumit ---- juga memperkuat ingatan. Kata teman saya, dengan menulis maka ingatan kita tentang sesuatu semakin bagus.
Biar lebih paham, silakan copy-paste source berikut ke komputer anda, dan langsung dijalankan. Kebetulan pada komennya sudah diberikan penjelasannya.

function runGUI
% dibuat oleh Mohammad Fajar pada malam selasa (29/1/2013)
clc;
init();
% dalam GUI matlab, container kedua tertinggi adalah figure... 
% yang pertama tertinggi  adalah root yang merupakan  monitor
fig = ... 
    figure('name', 'Test Sin', ...
         'numbertitle', 'off', ...
         'position' , [200, 100, 500,500], ...% relatif terhadap 
         'menubar' , 'none'  ...              % monitor
         );
     
% panel yang digunakan untuk memanajemen control-control 
panel_control =  uipanel( ...
        'title', 'Control Panel', ...
        'units', 'pix', ...
        'fontsize', 12, ...
        'backgroundcolor', 'white', ... 
        'position',[10 10 480 100] ...  % relatif terhadap 'fig'
        );

buttonStart = uicontrol('units','pix',...
          'style','pushbutton',...
          'parent', panel_control, ...
          'position',[10 10  90 25],...  % relatif terhadap 'panel_control'
          'fontweight','bold',...
          'fontsize',12,... 
          'string','draw',...
          'HorizontalAlignment' , 'right', ...
          'callback',@start ...  % memanggil fungsi 'start' 
          );
      
% untuk button ini kita akan gunakan unit centimeter...
% sementara pada button sebelumnya (button start) yang digunakan adalah
% unit pixels (atau disingkat 'pix'  ==> dalam matlab terdapat kemudahan
% dengan menggunakan singkatan)
% cuma saya bingung yang mana bisa disingkat, yang mana tidak...
% demi amannya mending g usa disingkat..... 
buttonPause  = uicontrol('units', 'centimeters', ...
            'style', 'pushbutton',... 
            'parent' , panel_control , ... 
            'position', [ .24  1.0   2.5   .7], ...  % rel. terhadap 'panel_control'
            'fontsize', 12, ...
            'fontweight', 'bold', ...
            'string', 'delete', ...
            'HorizontalAlignment', 'left', ... 
            'callback', @pause );  % memanggil fungsi 'pause' 
        
% ingat parameter-parameter pada MATLAB selain bisa disingkat juga tidak
% mengenal case-sensitiv ... alias tidak peduli huruf besar atau huruf
% kecil .... jadi 'Position' atau 'posiTion' hasilnya sama saja...
% sepanjang hurufnya tidak kurang atau lebih.... 


% selanjutnya akan dibuat panel untuk memanajemen daerah plotting 
panel_plot = uipanel('title', 'plot area', ...
            'units', 'pix', ...
            'positioN', [10 120 480 370] , ...
            'backgrounDcolor', 'cyan', ...
            'fontsize', 12);

% sekarang tambahkan plot area 
% saya ga' tau kenapa, yang jelas untuk object "axis" tidak dibolehkan 
% untuk menggunakan units selain 'inch'
global g;
g =axes('position',[.1  .1  .86  .87], ...
        'xlim', [0 (2*pi)], ... 
        'parent', panel_plot);
% mesh(peaks(20));
end

function init()
global x;
x = linspace(0,2*pi , 400 );
end 

function []  = start(varargin)
global x;
global hline;
hline = plot(x,sin(x));
end

function [] =  pause(varargin)
% gunanya untuk menghapus plotting 
cc = get(gcf, 'CurrentAxes');
aa = get(cc, 'Children');
delete(aa);
end 

Friday, December 21, 2012

Basic Hack Plotting V1.0

Berikut ini adalah sedikit lanjutan tutorial sebelumnya tentang "plotting berjalan". Pekerjaan selanjutnya adalah bagaimana membuat skala vertikal dari plottting-nya bisa "serasi". Tapi untuk saat ini, hanya itu saja. Semoga bermanfaat.

Project di hosting pada:
https://bitbucket.org/moh_fajar/basicplothacking

Saturday, December 15, 2012

Belajar plot berjalan di java

Dalam tulisan kali ini saya akan memberikan contoh source code java yang bisa digunakan dalam membuat plotting berjalan seperti yang dimaksud pada situs http://www.chem.mtu.edu/~tbco/cm416/newpida.html
(pastikan di komputer anda terinstal java untuk melihat simulasi yang dimaksud)
Untuk mencoba secara sederhana bagaimana algoritma yang digunakan dalam program tersebut, anda dapat mencoba source code berikut:

Saturday, December 8, 2012

Hypocycloid di java

Berikut saya berikan contoh bagaimana membuat animasi hypocycloid di java:

source code nya:
  /**
   * Copyright 2012, Moh. Fajar
   * 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see 
    */


package fjr.geometri;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author mamat
 */
public class Hypocycloid extends JPanel {

    //posisi dan ukuran persegi utama
    int rectX = 10;
    int rectY = 10;
    int rectH = 420;
    int rectW = 600;
    
    //posisi dan diameter lingkaran  besar
    int radius1 = 270;
//    int x = rectW+rectX-radius1;
    int x = rectX;
    int y = rectH+rectY - radius1;
   
    //posisi dan diameter lingkaran kecil
    int radius2 = 100;
    int x1 = x + (radius1 - radius2) / 2, 
            y1 = y + (radius1 - radius2);
    
    //sudut putaran lingkaran kecil
    double omega = 0; 
    
    //posisi dan titik Hypocycloid
    int x2= x1+radius2;
    int y2 = x1 + radius2;
   
    public Hypocycloid() {
        this.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paintComponent(g);
        drawMainRect(g);
        drawBigCircle(g);
        drawSmallCircle(g);
        drawPoint(g);
    }
   
    public void drawBigCircle(Graphics g) {
        g.setColor(Color.red);
        g.drawOval(x, y, radius1, radius1);
    }
    
    public void drawSmallCircle(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawOval(x1, y1, radius2, radius2);

    }

    public void drawMainRect(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawRect(rectX, rectY, rectW, rectH);
    }
    
    public void drawPoint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.black);
        g2.fillOval(x2, y2, 10, 10);
        
    }
  
    public void start() {
        LingkaranDalam lingkarandalam = new LingkaranDalam(this);
        Thread t1 = new Thread(lingkarandalam);
        t1.start();
        LingkaranLuar lingkarangluar = new LingkaranLuar(this);
        Thread t2 = new Thread(lingkarangluar);
        t2.start();
              
    }
    
    private class LingkaranLuar implements Runnable {
        
        Hypocycloid hypo;
        
        public LingkaranLuar(Hypocycloid hypo){
            this.hypo = hypo;
        }
        
        @Override
        public void run() {

            while (true) {
                for (int i = 0; i < 110; i++) {//sorong ke kiri
                    x += (int) ((rectW + rectX - radius1) / 100);
                    synchronized (hypo) {
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.out.println("thread error 2");
                    }
                }

                for (int i = 0; i < 50; i++) { //sorong ke atas
                    y -= (int) ((rectH + rectY - radius1) / 50);
                    synchronized (hypo) {
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.out.println("thread error 2");
                    }
                }

                for (int i = 0; i <110; i++) { //sorong ke kanan
                    x -= (int) ((rectW + rectX - radius1) / 100);
                    synchronized (hypo) {
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.out.println("thread error 2");
                    }
                }

                for (int i = 0; i < 50; i++) { //sorong ke bawah
                    y += (int) ((rectH + rectY - radius1) / 50);
                    synchronized (hypo) {
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.out.println("thread error 2");
                    }
                }
            }

        }
    }

    private class LingkaranDalam implements Runnable {
        
        Hypocycloid hypo;
        
        public LingkaranDalam(Hypocycloid hypo){
            this.hypo = hypo;
        }

        @Override
        public void run() {
            boolean run = true;
            int a = radius1/2;
            int b = radius2/2;
            int rtemp = a - b;
            while (run) {
                for (int i = 0; i < 200; i++) {
                    omega -= Math.PI / 200;
                    x1 = (int) (x + rtemp + rtemp * Math.cos(omega));
                    y1 = (int) (y + rtemp + rtemp * Math.sin(omega));
                       
                    x2 = (int)(x1+(b-5)+b*Math.cos((a-b)*omega/b));
                    y2 = (int)(y1+(b-5)-b*Math.sin((a-b)*omega/b) );
                    
                    synchronized(hypo){
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(5);
                    } catch (Exception e) {
                        System.out.println("thread error 1");
                    }
                }

            }
        }
    }

    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame mainframe = new JFrame("Geometri Fun");
                mainframe.setLayout(new BorderLayout());
                mainframe.setPreferredSize(new Dimension(700, 500));
                Hypocycloid hypo = new Hypocycloid();
                mainframe.add(hypo);
                mainframe.setResizable(false);
                mainframe.pack();
                mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mainframe.setVisible(true);
            }
        });
    }
}