



Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!




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.
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
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?
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
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.
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.
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)