Want to create interactive content? It’s easy in Genially!
Lab Session 2
Guillermo Gandía
Created on October 13, 2024
Start designing with a free template
Discover more than 1500 professional designs like these:
Transcript
Calculus
Lab session 2
Start
Victor Adsuar, Isaac Cicuendez , Guillermo Gandía and Ignacio Pérez.
Index
Introduction
Scripts
For
Functions
Fprintf and disp
If
While
1. Introduction
MATLAB as a programming language.
.m files
- - Scripts
- - Functions
(Non-Dependant of input arguments)
(Dependant of input arguments)
.m files
Warning: Function exp has the same name as a MATLAB built-in. We suggest you rename the function to avoid a potential name conflict.
Things to consider
- - The .m files must be stored in the current folder.
- - The name of a file must be different from the MATLAB reserved names.
- - The name must start with a letter.
- - Matlab is case-sensitive.
Creating a folder
You have to create a folder where your Matlab files will be stored. For example, you can make C:\matlabfiles
2. Scripts
A file that contains multiple sequential lines of MATLAB commands.
Make a new file inside your folder.
Now, in the live script editor, this script can be accessed by tiping its name.
3. For
Used for repeating calculations.
Where 1:n is a range and i is the variable.
The statement For is used when you know the number of iterations you want. While is more proper when you want the number of iterations to be determined inside the loop.
For example
In this case, we are using for in order to calculate the sum of the first ten natural numbers.
For example
Script exercises
Create two script files for computing the following expressions with an appropiate number of digits (vpa).
a)
b)
For example
Solutions:
a)
b)
For example
Script exercises
Repeat the last exercise with these expressions. Also, compare the result with log(2).
c)
d)
For example
Solutions:
c)
For example
Solutions:
d)
4. Functions
How do you do functions in MatLab?
Example: Create a function that calculates the product and the sum of three variables
Function Files
Syntax
Name of the function (Must be the same as the file name)
Variables that take the result value(output) -Always on []
Variables introduced/inputed to the funcion -Always on ()
Body of the function where you can operate with the variables
- The function starts with "function..." and ends with an "end"
- All arguments end with ;
- The file must be in the the mlx folder
Function Files
Syntax
Example
Name of the function (Must be the same as the file name)
Variables that take the result value(output) -Always on []
Variables introduced/inputed to the funcion -Always on ()
Body of the function where you can operate with the variables
Mlx file syntax
explanation
We asign a value for the input variables
On this output variable will be expresed our result
Result
With this code we asigned a number to our variables on the function we created before and it's result is: x=2 y=-6
Exsercises
Function exercises
For a given n that will be the input obtain one output is the sum when n=10, n=100 and 100000
For a given n that will be the input obtain one output is the sum when n=10, n=100 and 100000
Exsercises solved
Function exercises solved
For a given n that will be the input obtain one output is the sum when n=10, n=100 and 100000
For a given n that will be the input obtain one output is the sum when n=10, n=100 and 100000
5. Some usefulcommands
Me trying to understand matlab
FPRINTF
DISP
Fprintf
Disp
simple function used to show an string value or a function
fprintf: used to show text on the screen to the user
used for its quicknes
allows formating
does not allow formating
used for more complex outputs with various variables
It does an automatic line break
Syntax
text we want to display
while using display, we can only show the variable without text.
As previously mentioned, we can show variables and text with fprintf
if anyone is curious
we can achieve the same as with a fprintf , if we use the function num2str
EX 7
main code
function /m file
what the user sees
6. If , elseif, esle
If (condition)<statement> elseif (condition) <statement> else <statement> end
These are control statements to control the program.
IIf is used when you want your program to do a specefic thing if a condition is completed.After an if, it is posible to add more conditions with elseif and finally else is runned if none of the previous conditions is true.
7. While
What is it and how to use it
While is a tool in Matlab and other programming languages that allow us to repeat a process until a condition is completed.
E xample
Syntax
Let's try this program with B = 10 and B = 20
While (logical expresion)
Yes
Orders to execute
(Is the logical expresion true?)
No
End
We are going to the Moon
With a piece of paper (and "while")
We can obtain any distance by folding a piece of paper several times.
MATERIALS
Goals
To make a function to which a distance is introduce and it gives us the number of times that we have to fold a piece of paper in order to reach that distance.
Matlab
program
Function
While
Fold a paper from Albacete to Madrid
Paper
Fold a paper from the Earth to the Moon
END
Thanks for listening
Asking the user the distance in km
Paper initial thickness
((km -> mm)
function to calculate the folds
While
Calling the function