Matlab Modeling and Simulation : Cheat Sheet

Sinan Thahir
4 min readAug 18, 2021

Matlab, is a high-performance language which assimilates computation, visualization and programming in a single environment. The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix software developed by the LINPACK and EISPACK projects, which together represent the state-of-the-art in software for matrix computation.

MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar noninteractive language such as C or Fortran.

MATLAB® is widely used in different areas of applied mathematics, in education and research and in numerous industries. This software finds its wide applications in different domains of engineering such as:

  1. Electronics engineers mainly use MATLAB® for designing more efficient devices that are smaller in size and can integrate wireless communications, audio, video, and other attributes.
  2. In electrical engineering, the application of this program is to examine and simulate momentary phenomena in power systems.
  3. Mechanical engineers need MATLAB® for scrutiny of problems in control systems, mechanical vibrations, basic engineering mechanics, electrical circuits, statics and dynamics and numerical methods.
  4. It is used to model and simulate physical problems in the field of chemical engineering.

Cheat Sheet

Small variables like x and y will be either row or column vectors and A will always be a matrix.

Logicals

a = 20;                 %Assign a the value of 10 
a == 5 %Test if a is equal to 5
false
a == 20 %Test if a is equal to 10
true
a >= 5 %Test if a is greater than or equal to 5
true
a 1 && a ~= 10 %Test if a is greater than 1 AND
false %not equal to 10
a > 1 || a ~= 5 %Test if a is great

For Loops

for k = 1:10 
disp(k);
end

Conditional Statements

if a > 90 
disp('Greater than 90');
elseif a == 90
disp('a is 90');
else
disp('None of the conditions is mets');
end

While Loops

k = 0; 
while k < 5
k = k + 1;
end

Functions

function [a, b] = testfct(x, y) 
a = x + y;
b = x * y;
end testfct(2, 3) %Call function in script or command window

Function Handles

sqr = a(n) n.^2; 
x = sqr(3) %Outputs 9

Plotting and Subplot

x = linspace(-5*pi, 5*pi, 1000); 
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'g-', 'LineWidth',3); % Plot black sin(x) curve
hold on % Adding additional curve
plot(x, y2, 'r-', 'LineWidth',3); % Plot red cos(x) curve
grid on
set(gca,'fontsize',20)
% Set the axis limits
axis([-5*pi, 5*pi, -1.5, 1.5])
% Add axis labels
xlabel('x', 'FontSize',20);
ylabel('y', 'FontSize',20);
% Add a title
title('A plot of cos(x) and sin(x)', 'FontSize', 20);
% Add a legend
legend('sin(x)', 'cos(x)');
-----
% Code for Subplots
x = linspace(0,10,50);
y = rand(50,1);
subplot(2,2,1), plot(x,sin(x),'Color','red','LineWidth',3) set(gca,'fontsize',14)
axis([0,2*pi,-1,1]), axis square
subplot(2,2,2), plot(x,cos(x),'Linewidth',3,'Color','blue') set(gca,'fontsize',14)
axis([0,2*pi,-1,1]), axis square
subplot(2,2,3:4)
y2 = rand(50,1); plot(x,y2,'LineWidth',3)
set(gca,'fontsize',14)

The resulting plot looks like :

--

--

Sinan Thahir

2023 and still alive, spreading sheets, ppt and tables