034IN "Fundamentals of Automatic Control" - Introduction to MATLAB/Simulink - Solving Exercises on MATLAB Programming

Table of Contents

Exercise: Scripts

Assignment

Make a helloWorld script, such that
Hello World!
I am going to learn MATLAB!
'This is a string'

Solution

% helloWorld.m
% my first hello world program in MATLAB
 
disp('Hello World!');
disp('I am going to learn MATLAB!');
 

Exercise: Scalars and Math Operations

Assignment

Consider the variable
where the time constant τ is

Solution

% solution of Exercise: Scalars and Math Operations
% script expRateEval1SCRIPT.m
 
Tau_days = 1.5;
 
secPerDay = 60*60*24;
tau = Tau_days * secPerDay;
 
% inefficient solution
t1 = 0;
y1 = (1-exp(-t1/tau));
 
t2 = 1/10;
y2 = (1-exp(-t2/tau));
 
t3 = 120;
y3 = (1-exp(-t3/tau));
 
t4 = 2400;
y4 = (1-exp(-t4/tau));
 
 

Exercise: Math and Vector Operations

Assignment

Consider the variable
where the time constant τ is

Solution

% solution of Exercise: Math and Vector Operations
% script: expRateEval_2_SCRIPT.m
 
Tau_days = 1.5;
 
secPerDay = 60*60*24;
tau = Tau_days * secPerDay;
 
% let's define the time row array
t = [ 0, 1/10, 120, 2400];
 
% evaluate the function using the time row-vector
y_vals = (1-exp(-t/tau));
 
 

Exercise: Vectors and Matrices

Assignment

Screenshot 2024-03-08 at 11.46.29.png
Screenshot 2024-03-08 at 11.46.38.png
Screenshot 2024-03-08 at 11.46.47.png
Screenshot 2024-03-08 at 11.46.55.png
Screenshot 2024-03-08 at 11.47.04.png
Screenshot 2024-03-08 at 11.47.25.png

Solution

% exConcatenateVectors_SCRIPT.m
 
a = [1 2];
b = [3 4];
 
c = [ 5; ...
6 ];
 
D = [ a ; ...
b ]
 
E = [D c ]
% equivalent expression:
% E_equiv = [ [ a ; b] c]
 
F = [ [E E]; [a b a]]
% equivalent expression ?
 
save ExConcatenateMatrices a b c D E F
% refer to the save command documentation

Exercise: Vector Math Operations

Assignment

Consider the variable
where the time constant τ is

Solution

% solution of Exercise: Vector Math Operations
% script: expRateEval_3_SCRIPT.m
 
clear % clean the MATLAB workspace
Tau_days = 1.5;
 
secPerDay = 60*60*24;
tau = Tau_days * secPerDay;
 
% let's define the time row array tVec
startT = 0;
endT = 648000;
Nsamples = 10000;
 
tVec = linspace(startT, endT, Nsamples);
 
% evaluate the function using the time row-vector
y_vals = (1-exp(-tVec/tau));