Membuat kalkulator sederhana di matlab | kalkulator sederhana
3 min read
2 months ago
Published on Dec 02, 2025
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Introduction
In this tutorial, we will create a simple calculator using MATLAB. This step-by-step guide will help you understand the process of programming basic arithmetic operations in MATLAB, making it an excellent exercise for beginners and a great way to enhance your computational skills.
Step 1: Setting Up MATLAB
- Open MATLAB on your computer.
- Create a new script by clicking on "New Script" from the home tab.
- Save the script with an appropriate name, such as
simple_calculator.m.
Step 2: Designing the User Interface
- Use MATLAB's built-in functions to create a simple user interface.
- Start by defining a basic layout for your calculator. You can create buttons for each operation (addition, subtraction, multiplication, division) and a display area for the results.
Step 3: Writing the Code for Operations
- Define functions for each arithmetic operation. Below is a sample code structure:
function simple_calculator()
% Create the user interface
f = figure('Position', [500, 500, 300, 400]);
% Create UI components
num1 = uicontrol('Style', 'edit', 'Position', [50, 300, 200, 40]);
num2 = uicontrol('Style', 'edit', 'Position', [50, 250, 200, 40]);
result = uicontrol('Style', 'text', 'Position', [50, 200, 200, 40]);
% Create buttons for operations
uicontrol('Style', 'pushbutton', 'String', '+', 'Position', [50, 150, 40, 40], 'Callback', @(src, event) calculate(num1, num2, result, '+'));
uicontrol('Style', 'pushbutton', 'String', '-', 'Position', [100, 150, 40, 40], 'Callback', @(src, event) calculate(num1, num2, result, '-'));
uicontrol('Style', 'pushbutton', 'String', '*', 'Position', [150, 150, 40, 40], 'Callback', @(src, event) calculate(num1, num2, result, '*'));
uicontrol('Style', 'pushbutton', 'String', '/', 'Position', [200, 150, 40, 40], 'Callback', @(src, event) calculate(num1, num2, result, '/'));
end
function calculate(num1, num2, result, operation)
a = str2double(get(num1, 'String'));
b = str2double(get(num2, 'String'));
switch operation
case '+'
res = a + b;
case '-'
res = a - b;
case '*'
res = a * b;
case '/'
if b ~= 0
res = a / b;
else
res = 'Error';
end
end
set(result, 'String', res);
end
Step 4: Testing the Calculator
- Run the script by clicking the "Run" button in the editor.
- Input numbers in the text fields and click the operation buttons to see results displayed.
- Test different combinations of numbers and operations to ensure the calculator functions correctly.
Conclusion
You have now created a simple calculator in MATLAB that can perform basic arithmetic operations. This exercise not only enhances your programming skills but also gives you a foundation to build more complex applications in the future. For further improvements, consider adding features like error handling, memory functions, or a more advanced user interface. Happy coding!