Berikut ini saya berikan contoh program MATLAB untuk membuat plot berjalan. Plot berjalan yang saya maksud adalah bagaimana grafik plotnya itu bergeser sepanjang waktu seiring dengan berubahnya nilai variabel-variabel yang di-plot.
Sesungguhnya shalat itu mencegah dari (perbuatan-perbuatan) keji dan mungkar
Q.S. Al-'Ankabut Ayat 45
Showing posts with label animasi. Show all posts
Showing posts with label animasi. Show all posts
Tuesday, November 25, 2014
Sunday, July 7, 2013
Vektorisasi loop molekular dinamik di MATLAB
Hal ini berlaku pula dalam dunia pemrograman. Dalam dokumentasi resmi MATLAB yang saya ambil di link http://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html disebutkan bahwa agar program MATLAB bisa berjalan dengan lebih cepat maka salah satu cara yang bisa digunakan adalah menggunakan pre-alocating array dan menghindari penggunaan looping:
http://stackoverflow.com/questions/12074373/matlab-vectorization-how-to-avoid-this-for-loop
http://stackoverflow.com/questions/16214891/how-to-avoid-a-for-loop-in-matlab-when-performing-a-operation-on-each-row-in-a
Akan tetapi baru-baru ini saya mencoba mengetes dugaan tersebut dalam simulasi molekular dinamik. Dan ternyata hasilnya tidak sesuai yang ada pada buku. Rupanya pengembangan MATLAB akhir-akhir ini cenderung melakukan optimisasi pada looping ketimbang prosedur standar (yakni operasi matriks) yang merupakan brand dari MATLAB.
Simulasi molekular dinamik sendiri saya translate dari sumber di
http://www.personal.psu.edu/auk183/MolDynamics/Molecular%20Dynamics%20Simulations.html
Yang aslinya dikembangkan dengan bahasa java. Setelah melakukan debugging yang susah payah, akhirnya saya berhasil mengembangkan versi MATLAB nya: menggunakan teknik vektorisasi yang menjadi brand MATLAB ketimbang menggunakan looping yang katanya mengurangi performa. Berikut merupakan kode hasil vektorisasi tersebut
Adapun kode hasil translate secara 'mentah' adalah sebagai berikut
Setelah dieksekusi terlihat bahwa hasil penggunaan loop bukannya memperlambat performa, malah mempercepat performa dari program MATLAB sendiri. Rata-rata waktu eksekusi looping berada di sekitar 0.03 detik, sementara hasil vektorisasi ternyata membutuhkan waktu 3 kali lebih lama yani berada di sekitar 0.1 detik.
Saturday, December 8, 2012
Hypocycloid di java
Berikut saya berikan contoh bagaimana membuat animasi hypocycloid di java:
source code nya:
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);
}
});
}
}
Subscribe to:
Comments (Atom)
