Fisika Komputasi: Pengenalan Sintaks Dasar Matlab

Basic MATLAB Syntax

Concept Example Description
Comments % This is a comment Anything after % is ignored by MATLAB
Assignment x = 5; Assigns the value 5 to variable (x)
Suppress Output x = 5; The semicolon (;) hides the output
Display Output x = 5 Without (;), MATLAB displays the result
Vectors v = [1 2 3 4] Row vector v = [1; 2; 3; 4]; (column vector)
Matrices A = [1 2; 3 4] 2 x 2 matrix. Rows separated by (;)
Element Access A(2,1) Access element in row 2, column 1.
Range (colon) 1;5 -> [1 2 3 4 5] Creates an arithmetic sequence.
1:2:9 -> [1 3 5 7 9]


Mathematical Operations

OperationSyntaxExample
Addition/Subtraction+/-a + b, a - b
Multiplication*A*B (matrix multiplication)
Element-wise Multiplication.*A.*B (multiply element by element)
Division/,.\A/B (right division),
A.\B (element-wise left division)
Power^ or .^A^2 (matrix power), A.^2 (element-wise power)
Transpose'A' (matrix transpose)
Built-in functionssin(x), cos(x), exp(x), log(x), sqrt(x)Common math functions.
Constantspi, inf, nan, epsÏ€, ∞, NaN, smallest increment.


Plotting and Visualization

OperationSyntaxExample
plot(x,y)2D plotx=0:0.1:2*pi;
plot(x, sin(x));
xlabel('text')Axis labelsxlabel('Time'); ylabel('Amplitude');
title('text')Plot titletitle('Sine Wave');
legend('A','B')Add legend--
grid onAdd grid--
figureOpen new figure window--
subplot(m,n,p)Multiple plots in one figuresubplot(2,1,1);
plot(x,sin(x));
------


Programming Structures

With structure "if statement", with syntax "matlab,

if x > 0
elseif x < 0
disp('Negative')
else
disp('Zero')
end

Post a Comment

Previous Post Next Post