Docsity

Programming - Lab Session 2: Introduction to programming in MATLAB, Ejercicios de Programación Informática

Toggle sidebar
bg1
Programming.
Bachelor in Aerospace Engineering.
2019-20
1
Lab Session 2. Introduction to
programming in MATLAB
SECTION 1. DATA TYPES
Exercise 1. From binary to decimal
Can you tell what the equivalent to these binary numbers in the decimal number system
is?
(10111)2 = (11111)2 = (11100011)2 =
You can check your results in MATLAB using the bin2dec function.
Exercise 2. Data types
Let’s test how to convert a numeric variable between different data types
Declare a variable a, and assign to it any two digit value (19 for example). By default the
variable will be declared as double data type.
Convert the variable to the int16 type (using the int16 function) and store the result in
a new variable. Try the same with a two digit number with five decimal values (19.12345)
Try other functions to convert numeric values (int8, int16, int32, int64, uint8, uint16,
uint32, uint64, single and double).
Remember you can use help or demo in the command window to get help on a function
and it’s syntax (syntax = how to use it).
You can also use whos to see the variable names, size, and datatypes
Exercise 3. Declaring numeric variables of different types
When we create a new numeric variable it will be a double by default
To create a variable of any other data type, you can use the same functions as when converting
numeric types
a =uint16(8)
Exercise 4. Range of values represented for each data type
a) How many different integer values can be represented with 8 bits (bit = binary digit)?
If we use a signed representation, which would be the smallest and largest values
represented?
pf3
pf4
pf5

Vista previa parcial del texto

¡Descarga Programming - Lab Session 2: Introduction to programming in MATLAB y más Ejercicios en PDF de Programación Informática solo en Docsity!

Bachelor in Aerospace Engineering.

Lab Session 2. Introduction to

programming in MATLAB

SECTION 1. DATA TYPES

Exercise 1. From binary to decimal

Can you tell what the equivalent to these binary numbers in the decimal number system is? (10111) 2 = (1 1 111) 2 = ( 1 1100011) 2 = You can check your results in MATLAB using the bin2dec function.

Exercise 2. Data types

Let’s test how to convert a numeric variable between different data types Declare a variable a, and assign to it any two digit value (19 for example). By default the variable will be declared as double data type. Convert the variable to the int16 type (using the int16 function) and store the result in a new variable. Try the same with a two digit number with five decimal values (19.12345) Try other functions to convert numeric values (int8, int16, int32, int64, uint8, uint16, uint32, uint64, single and double). Remember you can use help or demo in the command window to get help on a function and it’s syntax (syntax = how to use it). You can also use whos to see the variable names, size, and datatypes

Exercise 3. Declaring numeric variables of different types

When we create a new numeric variable it will be a double by default

To create a variable of any other data type, you can use the same functions as when converting

numeric types

a =uint 16 (8)

Exercise 4. Range of values represented for each data type

a) How many different integer values can be represented with 8 bits (bit = binary digit)? If we use a signed representation, which would be the smallest and largest values represented?

Bachelor in Aerospace Engineering.

You can check your answer by typing intmin('int 8 ') and intmax('int 8 '). Intmin and intmax give information on the maximum value to represent with a given integer datatype. Repeat the same exercise for 32 bits Exercise 5. Assigning beyond the limits of the data type a) Which type of variable is declared with the next instruction? x = uint8(-1); What is the initial value of the variable x? Why? b) Which type of variable is declared with the next instruction? y = int8(128); What is the initial value of the variable y? Why? Exercise 6. c) What are the results of the following numerical expressions? i_1 = uint8(13); i_2 = uint8(4); r_1 = double(13); r_2 = double(4); r_3 = 4; result1 = i_1 / i_ result2 = r_1 / r_ result3 = i_1 / r_ result 4 = i_1 / r_ Check your answers creating a script in MATLAB. d) What are the results of the following numerical expressions? i_1 = 13 ; i_2 = 4; r_1 = 3; r_2 = 4; r_3 = 4; result1a = int8(i_1 / i_2) result1b = i_1 / i_ result2b = r_1 / r_ Check your answers creating a script in MATLAB. Exercise 7. Floating point representation and precision

Because there are only a finite number of double-precision numbers, you cannot represent all

numbers in double-precision storage. On any computer, there is a small gap between each

double-precision number and the next larger double-precision number. You can determine the

size of this gap, which limits the precision of your results, using the eps function.

Bachelor in Aerospace Engineering.

clear; clc; disp('Lab 02 First Section '); fprintf('\nLets show how to use the built-in function fprintf\n'); % Some examples using the function fprintf(...) % to show on the screen a mix of text and var values. a = 15; fprintf('The value of var a is (version 01) %d \n',a); fprintf('The value of var a is (version 02) %5d \n',a); fprintf('The value of var a is (version 03) %05d\n',a); fprintf('\n --- --- \n\n'); myPi = 3.1416; fprintf('The value of var myPi is %f \n',myPi); fprintf('The value of var myPi is %5.3f \n',myPi); fprintf('The value of var myPi is %6.4f \n',myPi); fprintf('The value of var myPi is %10.4f \n',myPi); fprintf('The value of var myPi is %010.4f \n',myPi); fprintf('\n --- --- \n\n'); fprintf('The value of var myPi is %E \n',myPi); fprintf('The value of var myPi is %5.3E \n',myPi); fprintf('The value of var myPi is %6.4E \n',myPi); fprintf('The value of var myPi is %10.4E \n',myPi); fprintf('The value of var myPi is %010.4E \n',myPi); fprintf('The value of var myPi is %15.4E \n',myPi); fprintf('The value of var myPi is %015.4E \n',myPi); Exercise 10. Area of a triangle Write a MATLAB script to calculate the area of a right-angled triangle. The program must ask the height and the length of the base. The output of the program must be like this: Right-angled triangle of height __ and base __. So, Area is __. Blank spaces are filled with the actual values using the built-in function fprintf( ) Exercise 11. Write a MATLAB script that prints on the screen a welcome message and asks the user’s year of birth, which must be stored in a variable named birthYear. The program must calculate the current age of the user and print on the screen: You were born in . You are years old the present year () being and the read and the calculated value, respectively. Think about which data types are the most appropriate for each variable. SECTION 3. SIMPLE MATLAB PROGRAMS Exercise 12. Write a script to convert between GB pound and Euro currencies (1 euro = 0. pounds). The program must ask the amount in pounds and print the conversion to euros.

Bachelor in Aerospace Engineering.

Extend the program to also convert the resulting euros to dollars (1 euro = 1. dollars). Exercise 13. A company produces a component that should have a specific weight. However, there is a tolerance of k percent, meaning that the range between minus and plus k % of the desired weight is acceptable. Create two variables to respectively store the desired weight and the value of k. Use two additional variables to store the minimum and maximum values in the acceptable range. Exercise 14.

Write a script to calculate the distance between two points in a 2-D (bidimensional) space. The

program must:

1. Ask the user for a value x.

2. Ask the user for a value y.

3. Ask the user for a value x.

4. Ask the user for a value y.

5. Calculate the distance d between the points ( x1 , y1 ) and ( x2 , y2 ):

  1. Use the following format to print the result: A = ( 5 , 6 ) B = ( 7 , 8 ) => d(A, B) = … Exercise 1 5. Which are the results of evaluating the following logical and relational expressions?
    • 3 == 5 + 2
    • 'b' < 'a' + 1
    • 10 > 5 + 2
    • (10 > 5) + 2
    • 'c' == 'd' - 1 && 2 < 4
    • 'c' == 'd' - 1 || 2 > 4
    • 'b' >= 'c' - 1
    • 3 == ( 2 + 1 )
    • (3 == 2) + 1
    • ( 25 < 4) ~= true
    • int8('a') + 10 >= 107 Check your answers using MATLAB. Exercise 16. Create two variables x and y and store numbers in them. Write a logical expression that will be true if the value of x is higher than 5 or the value of y is lower than 10 , but not if both cases are true.

Bachelor in Aerospace Engineering.

pa = 2; % paper <- 2; st = 3; % stone <- 3; li = sp = myChoice = input('Choose one option, paper(1), rock(2), scissors(3), … : '); botChoice = randi( 5 ); Iwin = ... ??? % Note: in following lab sessions we are going to repeat this exercise with % conditional instructions (if-then-else)