MATLAB - Predefined Variables
MATLAB - Built-in Constants and Special Values
Section titled “MATLAB - Built-in Constants and Special Values”MATLAB provides a set of built-in names that represent common mathematical constants and special values. While you can overwrite these with your own variable assignments, it is strongly discouraged as a best practice. Doing so can lead to confusing and hard-to-debug errors, especially when using functions that rely on these standard values.
Numeric Constants and Special Values
Section titled “Numeric Constants and Special Values”These are the most common built-in values you will encounter.
| Name | Description and Modern Best Practice |
|---|---|
pi | Represents the mathematical constant π (≈ 3.14159). It is a double-precision floating-point number. |
i, j | Represent the imaginary unit, where i² = -1. Best Practice: Use 1i or 1j (e.g., z = 3 + 4*1i) to create complex numbers. This prevents issues if you accidentally use i or j as a loop counter. |
inf | Represents infinity. It typically results from operations like 1/0 or log(0). |
NaN | Stands for ‘Not a Number’. It results from mathematically undefined operations, such as 0/0 or inf - inf. |
eps | The ‘floating-point relative accuracy’. It is the distance from 1.0 to the next largest representable double-precision number. Useful for numerical stability checks. You can also use eps(x) to find the gap at a value x. |
Example: Working with Constants
Section titled “Example: Working with Constants”% Calculate the area of a circle with radius 5radius = 5;area = pi * radius^2;
% Create a complex number using the recommended syntaxcomplex_num = -2 + 1i * 5;
% Demonstrate how NaN and Inf can ariseval1 = 0/0; % Results in NaNval2 = 1/0; % Results in Inf
disp(['Area: ', num2str(area)])disp(['Complex Number: ', num2str(complex_num)])disp(['0/0 is: ', num2str(val1)])disp(['1/0 is: ', num2str(val2)])Expected Output:
Area: 78.5398Complex Number: -2+5i0/0 is: NaN1/0 is: InfWorkspace and Command-Line Variables
Section titled “Workspace and Command-Line Variables”| Name | Description |
|---|---|
ans | A temporary variable that holds the result of the most recent command that was not assigned to a variable. It’s useful for quick calculations in the Command Window but should not be relied upon in scripts. |
Example: Using ans
Section titled “Example: Using ans”% In the command window...5 * 10
ans =
50
ans / 2
ans =
25Important Commands (Not Variables)
Section titled “Important Commands (Not Variables)”The original tutorial listed some commands and keywords as variables. It’s important to distinguish them correctly.
| Command/Keyword | Description |
|---|---|
whos | A command that lists all variables in the current workspace, along with their size, memory usage, and data type. |
global | A keyword used to declare a variable that can be accessed across different functions and the base workspace. Use with caution as it can make code harder to debug. |
Example: Using whos
Section titled “Example: Using whos”% Create some variables of different typesA = rand(3, 3); % A 3x3 double matrixS = "Hello, World!"; % A string scalarC = {1, 'two', [3,3]}; % A cell array
% Display the workspace contentswhosExpected Output (will vary slightly):
Name Size Bytes Class Attributes
A 3x3 72 double C 1x3 266 cell S 1x1 174 stringModern Date and Time Handling
Section titled “Modern Date and Time Handling”Deprecation Notice: The legacy functions date and clock are not recommended for new code. They are less flexible and can be ambiguous.
The modern and recommended way to handle date and time information is with the datetime object.
% Get the current date and timenow_dt = datetime('now')
% Get just today's date (time is midnight)today_dt = datetime('today')
% You can perform arithmetic and formatting easilyyesterday_dt = today_dt - days(1);disp(['Yesterday was: ', datestr(yesterday_dt)])Expected Output (will reflect the current date):
now_dt =
datetime
15-Oct-2024 10:30:00
today_dt =
datetime
15-Oct-2024
Yesterday was: 14-Oct-2024