Rabu, 19 September 2018

Metode Runge Kutta menggunakan Matlab

MATLAB adalah kependekan dari MATrix LABoratory dikarenakan setiap data pada MATLAB menggunakan dasar matriks. MATLAB adalah bahasa pemrograman tinggi, tertutup, dan case sensitive dalam lingkungan komputasi numerik yang dikembangkan oleh MathWorks. 



kelebihannya yang paling populer adalah kemampuan membuat grafik dengan dukungan kustomisasi terbaik. MATLAB mempunyai banyak tools yang dapat membantu berbagai disiplin ilmu. Ini merupakan salah satu penyebab industri menggunakan MATLAB.

Kali ini kita akan mencoba mengaplikasikan salah satu metode yaitu metode Runge Kutta ke matlab. 
kalian bisa mengikuti cara tersebut. 


function varargout = metoda_runge_kutta(varargin)
% METODA_RUNGE_KUTTA MATLAB code for metoda_runge_kutta.fig
%      METODA_RUNGE_KUTTA, by itself, creates a new METODA_RUNGE_KUTTA or raises the existing
%      singleton*.
%
%      H = METODA_RUNGE_KUTTA returns the handle to a new METODA_RUNGE_KUTTA or the handle to
%      the existing singleton*.
%
%      METODA_RUNGE_KUTTA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in METODA_RUNGE_KUTTA.M with the given input arguments.
%
%      METODA_RUNGE_KUTTA('Property','Value',...) creates a new METODA_RUNGE_KUTTA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before metoda_runge_kutta_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to metoda_runge_kutta_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help metoda_runge_kutta

% Last Modified by GUIDE v2.5 18-May-2018 14:39:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @metoda_runge_kutta_OpeningFcn, ...
                   'gui_OutputFcn',  @metoda_runge_kutta_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before metoda_runge_kutta is made visible.
function metoda_runge_kutta_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to metoda_runge_kutta (see VARARGIN)

% Choose default command line output for metoda_runge_kutta
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes metoda_runge_kutta wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = metoda_runge_kutta_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function nilai_awal_x0_edit_Callback(hObject, eventdata, handles)
% hObject    handle to nilai_awal_x0_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nilai_awal_x0_edit as text
%        str2double(get(hObject,'String')) returns contents of nilai_awal_x0_edit as a double


% --- Executes during object creation, after setting all properties.
function nilai_awal_x0_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nilai_awal_x0_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function nilai_awal_y01_edit_Callback(hObject, eventdata, handles)
% hObject    handle to nilai_awal_y01_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nilai_awal_y01_edit as text
%        str2double(get(hObject,'String')) returns contents of nilai_awal_y01_edit as a double


% --- Executes during object creation, after setting all properties.
function nilai_awal_y01_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nilai_awal_y01_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function nilai_awal_y02_edit_Callback(hObject, eventdata, handles)
% hObject    handle to nilai_awal_y02_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nilai_awal_y02_edit as text
%        str2double(get(hObject,'String')) returns contents of nilai_awal_y02_edit as a double


% --- Executes during object creation, after setting all properties.
function nilai_awal_y02_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nilai_awal_y02_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function nilai_akhir_edit_Callback(hObject, eventdata, handles)
% hObject    handle to nilai_akhir_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nilai_akhir_edit as text
%        str2double(get(hObject,'String')) returns contents of nilai_akhir_edit as a double


% --- Executes during object creation, after setting all properties.
function nilai_akhir_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nilai_akhir_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function nilai_akhir2_edit_Callback(hObject, eventdata, handles)
% hObject    handle to nilai_akhir2_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nilai_akhir2_edit as text
%        str2double(get(hObject,'String')) returns contents of nilai_akhir2_edit as a double


% --- Executes during object creation, after setting all properties.
function nilai_akhir2_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nilai_akhir2_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in calculate_pushbutton.
function calculate_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to calculate_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x1 = str2num(get(handles.nilai_awal_x0_edit,'String'));
y1_1 = str2num(get(handles.nilai_awal_y01_edit,'String'));
y1_2 = str2num(get(handles.nilai_awal_y02_edit,'String'));
%x from x2 to x3, y calculated for x from x2 to x3 
x2 = str2num(get(handles.nilai_akhir_edit,'String'));
x3 = str2num(get(handles.nilai_akhir2_edit,'String'));
x_final  = x2:0.1:x3; %x target from x2 to x3 with interval 
pita=1000;   %the number of segments / bands 
delx=(x2-x1)/pita;   %the width of each segment /band
for i=1:1:length(x_final)    
%xx=x1;    
if i==1       
yy_it(i,1)=y1_1;       
yy_it(i,2)=y1_2;       
xx=x1;    
else       
yy_it(i,1)=yy_it(i-1,1);
yy_it(i,2)=yy_it(i-1,2);       
xx=xx+delx;    
end        
while (xx<x_final(i))           
xi=xx;        
y(1)=yy_it(i,1);        
y(2)=yy_it(i,2);        
for j=1:1:4            
%Pers.Differensial yang dicari solusinya             %*************************            
%kasus osilator harmonik teredam : d^2x/dt^2=-(k/m)x-nu dx/dt           
nu=2;    %koefisien redaman            
k=100;   %konstanta pegas           
m=1;     %massa benda            
fx(1)=y(2);                    %dx/dt = v              
fx(2)= -(k/m)*y(1)-nu*y(2);    %dv/dt = -(k/m)x-nu dx/dt %*************************            
k(j,1)=delx*fx(1);            
k(j,2)=delx*fx(2);            
if j==1 || j==2               
xi=xx+delx/2;               
y(1)=yy_it(i,1)+k(j,1)/2;              
y(2)=yy_it(i,2)+k(j,2)/2;                         
else                
if j==3                    
xi=xx+delx;                    
y(1)=yy_it(i,1)+k(j,1);                    
y(2)=yy_it(i,2)+k(j,2);                                     
end            
end        
end        
yy_it(i,1)=yy_it(i,1)+(k(1,1)+2*k(2,1)+2*k(3,1)+k(4,1))/6; %iterasi RK-4        
yy_it(i,2)=yy_it(i,2)+(k(1,2)+2*k(2,2)+2*k(3,2)+k(4,2))/6;%iterasi RK-4            
xx=xx+delx;    
end
end   
cla;
axes(handles.axes1);
%display y, the solution of d^2y/dx^2
plot(x_final,yy_it(:,1),'b');
title('Solusi Pers.Diff : d^2y/dx^2 = -(k/m)*y-nu*dy/dx');
xlabel('x');
ylabel('y');
hold on;

Lebih mudah kamu bisa download filenya disini | Link 1 |




Tidak ada komentar:

Posting Komentar