1 Introducción Matlab
1 Introducción Matlab
Content
1. What is mat lab?
2. Layout (menu bar, command window, Help window, command current folder window,
command history window)
3. Arithmetic operations
4. Order of precedence
5. Display formats
6. Elementary Math Built-in Functions
7. Defining scalar variables
8. Rules about variable names
9. Predefined variables and keywords
10. Useful commands for managing variables
11. Creating and saving scripts
12. Examples
¿What is MATLAB?
MATLAB stands for "MATrix LABoratory," is a high-level
programming language and interactive environment primarily used
for numerical computing, data analysis, and scientific and
engineering applications. It was originally developed by MathWorks
and is widely used in academia, industry, and research.
• It is known for its powerful matrix and array operations. Many mathematical and scientific problems can be
efficiently solved using matrix manipulation.
• Provides an interactive environment with a command-line interface where users can enter commands,
execute code, and get immediate feedback. It also offers a graphical user interface (GUI) for building and
running applications.
• MATLAB comes with a vast library of built-in functions and toolboxes, covering various domains such as
signal processing, image processing, control systems, optimization, machine learning, and more.
Layout
Menu bar
Current
working Command
directory window Memory
workspace
Details
Change Layout Distribution
Arithmetic operations
Order of precedence
Matlab como calculadora
>> C=(a-B)+40-a/B*10
x= A new value is assigned to x. Assign the value of the expresión on the
C= right-hand side to the variable C.
>>a=12;
>>B=4;
>>C=(a-B)+40-a/B*10
>>C =
>>a =
>>C=
>>ABB=72;
>>ABB=9;
>>ABB=
Rules about variables names
A variable can be named according to the following rules:
• Must begin with a letter.
• Can be up to 63 characters long.
• Can contain letters, digits, and the underscore character (_).
• Cannot contain punctuation characters (e.g., period, comma, semicolon).
• MATLAB is case-sensitive: it distinguishes between uppercase and lowercase letters.
• No space are allowed between characters
• Avoid using the name of a built-in function for a variable
A racehorse runs with a constant speed of 54 km/h. If the racecourse
measures 1.5Km, how long does the race last in seconds?
Useful commands for managing variables
Command Outcome
clear Removes all variables from the memory
clear x y z Removes only variable x, y, and z from the memory
who Displays a list of the variables currently in the memory
whos Displays a list of the variables currently in the memory and their size
together with information about their bytes and class
Script Files
• A script file is a sequence of MATLAB commands, also called a program.
• When a script file runs, MATLAB executes the commands in the order they are written, just as if
they were typed in the Command Window.
• When a script file has a command that generates an output it will be displayed in the Command
Window.
• Using a script file is convenient because it can be edited (or changed) and executed many times.
• Script files can be typed and edited in any text editor and then pasted into the MATLAB editor.
• Script files are also called M-files because the extension .m is used when they are saved.
Example
Heat transfer
An object with an initial temperature of T0 that is at the time t=0 inside
a chamber that has a constant temperature of Ts will experience a
temperature change according to the equation:
𝑇 = 𝑇𝑆 + 𝑇0 − 𝑇𝑠 𝑒 −𝑘𝑡
Where T is the temperature of the object at time t, and k is a constant. A soda can at a
temperature of 120°F (after being left in the car) is placed inside a refrigerator where
the temperature is 38°F. Determine, to the nearest degree, the temperature of the can
after three hours. Assume k=0.45. First define all the variables and then calculate the
temperature using one MATLAB command.
Inputs
Input is a function that allows
us to establish communication
between the script and the
user. Input is capable of
receiving two arguments.
The first argument is a
prompt, that is, a message
that will be requested from
the user, this is written
between quotes (since it is a
string of characters).
You have to be careful since
the function is executed on
the Command Window.
The second argument refers
to what type of data the user's
input will be stored in.
Outputs: disp()
Outputs are functions that will allow us to show information from the algorithm to the user. We are going to have
two functions to generate outputs: the disp() function and the fprintf() function.
Do not confuse the disp() function with the display function. Display shows us the assignment of the variable, on
the other hand disp() shows us what we have as an argument.
Disp() does not allow any format adaptation
Outputs: fprintf()
This function will allow us to
generate information that goes
from the algorithm to the user,
but it will also allow us to
manage a certain format on the
output that we are going to
generate. It handles two
arguments.
In the first argument it refers to
writing a message that we want
to show to the user, it can be
included with % which is the
type of variable that we are
going to show to the user (%d,
%s, %f, %c, etc).
The second argument is the
variable that we are going to
present.
Format fprintf()
When we talk about variable format, we refer to the number of figures that we want to display or the number
of decimal places in the variables.