CARA LOAD DAN BACA FILE EXCEL DENGAN GUI MATLAB

Have you ever heard of MATLAB? It’s a high-level programming language that is commonly used for mathematical and scientific computations. In fact, MATLAB stands for “matrix laboratory”, highlighting its capability in handling matrix operations and computations.

In this article, we will discuss two things: how to create functions in MATLAB and a specific application of MATLAB in calculating the trajectory of a parabolic motion.

Cara Membuat Function pada MATLAB dan Contoh Programnya

Functions are one of the fundamental structures in programming. Simply put, functions are reusable blocks of codes that perform a specific task. In MATLAB, functions can be created to modularize the code and promote code clarity and organization. Here’s how to create a function in MATLAB:

  1. Open MATLAB, and create a new script.
  2. In the new script, write the function in the following format:
    function [outputArg1,outputArg2] = functionName(inputArg1,inputArg2)
            %FUNCTIONNAME Summary of this function goes here
            %   Detailed explanation goes here
            outputArg1 = inputArg1;
            outputArg2 = inputArg2;
        end

    This format consists of a function declaration, an optional comments section, and a function ending. The function declaration includes the function keyword, the function name (in camelCase), input arguments inside the parentheses (also in camelCase), output arguments separated by commas inside square brackets, and the equal sign. The comments section, which is optional, provides a summary of the function and a detailed explanation. The body of the function, which is indented, contains the code that performs the specific task.

  3. Save the script with the same name as the function, and with the .m extension.
  4. Use the function in other scripts or functions by simply calling the function name and providing the input arguments.
Baca Juga :  CARA MEMBUAT TOMBOL DI EXCEL

Now that you know how to create a function in MATLAB, let’s see an example program. Here’s a program that calculates the sum of two numbers:

% sum.m
function [result] = sum(a,b)
    %SUM Returns the sum of two numbers.
    %   Detailed explanation goes here
    result = a + b;
end

To use this function, create another script and call the function with two input arguments:

% main.m
a = 5;
b = 7;
result = sum(a,b);
disp(result);

Executing this script will yield the output 12, which is the sum of the two numbers.

Cara Membuat Aplikasi Perhitungan Gerak Parabola Dengan GUI MATLAB

Now that you know how to create a function in MATLAB, let’s see an application of MATLAB in calculating the trajectory of a parabolic motion. Parabolic motion is a type of projectile motion that follows the path of a parabola due to a uniform vertical gravitational force and a uniform horizontal velocity. This type of motion can be observed in many phenomena, such as ballistics, rocketry, and sports.

In this section, we will create a graphical user interface (GUI) that calculates and displays the trajectory of a parabolic motion. Here’s how to create a GUI in MATLAB:

  1. Open MATLAB, and create a new script.
  2. In the new script, create a new figure for the GUI using the figure function. The parameters of the function can be customized to match the desired size and position of the figure.
  3. function main()
        figure('Name','Parabolic Motion Calculator',...
               'NumberTitle','off',...
               'Position',[500,500,400,250]);
  4. Add controls to the figure using various functions, such as uicontrol, uitable, and uipanel. These controls can be buttons, text boxes, sliders, and other widgets that interact with the user.
  5. function main()
        figure('Name','Parabolic Motion Calculator',...
               'NumberTitle','off',...
               'Position',[500,500,400,250]);
        uicontrol('Style','text',...
                  'Position',[30,190,120,20],...
                  'String','Initial velocity (m/s)');
        initialVelocity = uicontrol('Style','edit',...
                                    'Position',[30,170,60,20]);
        uicontrol('Style','text',...
                  'Position',[180,190,100,20],...
                  'String','Launch angle (degrees)');
        launchAngle = uicontrol('Style','slider',...
                                'Position',[180,170,160,20],...
                                'Min',0,'Max',90,...
                                'Value',45,'SliderStep',[1/90,10/90]);
  6. Add callbacks to the controls, which are functions that are executed when the control is interacted with. These functions can be created as new functions or as nested functions inside the main function.
  7. function main()
        figure('Name','Parabolic Motion Calculator',...
               'NumberTitle','off',...
               'Position',[500,500,400,250]);
        uicontrol('Style','text',...
                  'Position',[30,190,120,20],...
                  'String','Initial velocity (m/s)');
        initialVelocity = uicontrol('Style','edit',...
                                    'Position',[30,170,60,20]);
        uicontrol('Style','text',...
                  'Position',[180,190,100,20],...
                  'String','Launch angle (degrees)');
        launchAngle = uicontrol('Style','slider',...
                                'Position',[180,170,160,20],...
                                'Min',0,'Max',90,...
                                'Value',45,'SliderStep',[1/90,10/90]);
        calculateButton = uicontrol('Style','pushbutton',...
                                    'Position',[30,120,100,20],...
                                    'String','Calculate',...
                                    'Callback',@calculateTrajectory);
        axes('Units','pixels','Position',[50,50,300,80]);
        function calculateTrajectory(~,~)
            v0 = str2double(initialVelocity.String);
            theta = deg2rad(launchAngle.Value);
            t = linspace(0,2*v0*sin(theta)/9.81,1000);
            x = v0*cos(theta)*t;
            y = v0*sin(theta)*t-0.5*9.81*t.^2;
            plot(x,y);
            xlabel('Distance (m)');
            ylabel('Height (m)');
            title('Projectile motion');
        end
    end
  8. Save the script as a .m file, and run the script in MATLAB by calling the main function.
  9. main();

Executing this script will open a GUI window that consists of two controls: an edit box for the initial velocity and a slider for the launch angle. Clicking the “Calculate” button will plot the trajectory of the projectile motion on an axes widget.

Baca Juga :  Cara Membuat Kolom Agar Sama Di Excel

FAQ

What are the advantages of using functions in MATLAB?

Functions promote code clarity and organization, and can also improve the efficiency and modularity of the code. Functions can be reused in different scripts or functions, which reduces code duplication and allows for easier maintenance and debugging. In addition, functions can encapsulate complex algorithms or calculations into a single block of code, which makes the code more readable and easier to understand.

What are the other applications of MATLAB besides scientific and mathematical computations?

MATLAB has various toolboxes and libraries that can be used for different applications, such as image and signal processing, control systems, optimization, and machine learning. MATLAB can be applied in industries such as aerospace, automotive, biotech, finance, and gaming, among others. MATLAB can also be used for educational purposes, such as teaching programming, data analysis, and modeling and simulation.

Check out this video tutorial on creating a GUI calculator in MATLAB: