034IN "Fundamentals of Automatic Control" - Introduction to MATLAB/Simulink - Solving Exercises on MATLAB Programming
Exercise: Scripts
Assignment
Make a helloWorld script, such that
- When run, the script should display the following text
Hello World!
I am going to learn MATLAB!
- Hint: use the command disp to display strings. Check the command help for options and examples.
- Hint 2: The simplest way to write a string is to surround text with single quotes, like
- Open the editor, create a new script, write the code and save the script as helloWorld.m
Solution
% my first hello world program in MATLAB
disp('I am going to learn MATLAB!');
Exercise: Scalars and Math Operations
Assignment
Consider the variable
where the time constant τ is
- Calculate the number of seconds in
days and name this variable tau. - Evaluate
when
- Create a script, write the code and save the script as expRateEval1SCRIPT.m
Solution
% solution of Exercise: Scalars and Math Operations
% script expRateEval1SCRIPT.m
tau = Tau_days * secPerDay;
Exercise: Math and Vector Operations
Assignment
Consider the variable
where the time constant τ is
- Calculate the number of seconds in
days and name this variable tau. - Create a row-vector for the time instants
- Evaluate
when the time t assumes values in the previously defined array. - Create a script, write the code and save the script as expRateEval_2_SCRIPT.m
Solution
% solution of Exercise: Math and Vector Operations
% script: expRateEval_2_SCRIPT.m
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
- By concatenating vectors or matrices according to the following pictures, create the new matrices D, E and F
- Save the vectors
and the matrices
to a file named ExConcatenateMatrices.mat - Hint: use the command save
- Create a script, write the code and save the script as exConcatenateVectors_SCRIPT.m
Solution
% exConcatenateVectors_SCRIPT.m
% E_equiv = [ [ a ; b] c]
% 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
- Create a row-vector tVec that has 10000 samples between 0 and 648000 seconds.
- Hint: use the command linspace. Do you know any alternative to this command?
- Evaluate
for each of the time instant t in tVec. - Create a script, write the code and save the script as expRateEval_3_SCRIPT.m
Solution
% solution of Exercise: Vector Math Operations
% script: expRateEval_3_SCRIPT.m
clear % clean the MATLAB workspace
tau = Tau_days * secPerDay;
% let's define the time row array tVec
tVec = linspace(startT, endT, Nsamples);
% evaluate the function using the time row-vector
y_vals = (1-exp(-tVec/tau));