Computação e Programação 2ª Aula de Problemas Instituto Superior Técnico, Dep. de Engenharia Mecânica - ACCAII Exercícios Resolvidos [Livro 1] (Ver referências no slide 20) 3.3 Write a program to convert a Fahrenheit temperature to Celsius. Test it on the following data (where the reverse conversion is done). 2
Problema 1. Contexto do Problema Converter temperaturas de graus Fahrenheit para graus Celsius. 3 Análise 2.2 Especificações e Requisitos 2.3 Métodos de Processamento 2.1 Entradas 2.1 Saídas Conversão Temperaturas 4
Análise 2.1 Esquema de processamento Entradas: Temperatura em graus Fahrenheit (T_Fahrenheit) Saídas: Temperatura em graus Celsius (T_Celsius) 5 Análise 2.2 Especificações funcionais e Requisitos Édada uma temperatura em graus Fahrenheit O programa calcula e devolve a temperatura correspondente em graus Celsius Precisão do resultado até à segunda casa decimal. 6
Análise 2.3 Fluxo de dados Utilizador Introduz T_Fahrenheit Programa Apresenta mensagem a pedir temperatura para conversão Apresenta T_Celsius 7 Análise 2.3 Métodos de Processamento Conversão de temperaturas: T_Celsius = (T_Fahrenheit 32) * 5/9 8
Algoritmo 3.1 Abordagem Top-Down 9 Algoritmo 3.2 Lista de tarefas Programa pede Temperatura Fahrenheit Utilizador escreve Temperatura Fahrenheit Fazer conversão de temperaturas Apresentar Temperatura Celsius 10
Algoritmo 3.3 Algoritmo i. Programa pede T_Fahrenheit ii. Utilizador escreve T_Fahrenheit iii. T_Celsius = (T_Fahrenheit 32)* 5/9 iv. Apresentar T_Celsius 11 Testes (Algoritmo) 4 Testes T_Fahrenheit Esperado T_Celsius Algoritmo 68 69.8 20 21 20 21 12
Codificação 5 Codificação em Matlab T_Fahrenheit = input('introduza Temp Fahrenheit: '); T_Celsius = (T_Fahrenheit - 32)*5/9; fprintf('temperatura em Celsius: %6.2f º Celsius \n', T_Celsius) 13 Testes (Matlab) 6 Testes à codificação Introduza Temp Fahrenheit: 68 Temperatura em Celsius: 20.00º Celsius Introduza Temp Fahrenheit: 69.8 Temperatura em Celsius: 21.00º Celsius Introduza Temp Fahrenheit: 86 Temperatura em Celsius: 30.00º Celsius 14
Exercícios Resolvidos [Livro 1] (Ver referências no slide 20) 3.4 Write a script which inputs any two numbers (which may be equal) and displays the larger one with a suitable message. 15 Exercícios Propostos [Livro 1] (Ver referências no slide 20) 3.5 Write a script for the general solution of the quadratic equation ax 2 + bx+ c = 0. Your script should be able to handle all possible values of the data a, b, and c. Try it out on the following values of a, b and c: (a) 1, 1, 1 (complex roots); (b) 2, 4, 2 (equal roots of 1.0); (c) 2, 2, 12 (roots of 2.0 and 3.0). 28
Exercícios Propostos [Livro 1] (Ver referências no slide 20) 3.6 Develop a program that computes the solution of two simultaneous linear equations. Test your program using the following equations: x + y = 3 2x y = 3 Solution: x=2, y=1 Hint: begin by deriving an algebraic formula for the solution of the system ax + by = c dx+ ey = f The program should input the coefficients a, b, c, d, e and f. 29 Exercícios Propostos [Livro 2] (Ver referências no slide 20) 1. Write a simple script that will calculate the volume of a hollow sphere which is, where is the inner radius and is the outer radius. Assign a value to a variable for the inner radius, and also assign a value to another variable for the outer radius. Then, using these variables, assign the volume to a third variable. Include comments in the script. 2. The atomic weight is the weight of an atom of a chemical element. For example, the atomic weight of oxygen is 15.9994 and the atomic weight of hydrogen is 1.0079. Write a script that will calculate the molecular weight of hydrogen peroxide, which consists of two atoms of hydrogen and two atoms of oxygen. Include comments in the script. Use help to view the comment in your script. 30
Exercícios Propostos [Livro 2] (Ver referências no slide 20) 2. Write an inputstatement that will prompt the user for a string. Then, find the length of the string. 6. Experiment, in the Command Window, with using the fprintf function for real numbers. Make a note of what happens for each. Use fprintfto print the real number 12345.6789 in the following cases: i. without specifying any field width ii. in a field width of 10 with 4 decimal places iii.in a field width of 10 with 2 decimal places iv.in a field width of 6 with 4 decimal places v. in a field width of 2 with 4 decimal places 9. Write a script to prompt the user for the length and width of a rectangle, and print its area with 2 decimal places. Put comments in the script. 31 Exercícios Propostos [Livro 2] (Ver referências no slide 20) 15. Write a script that assigns values for the x coordinate and then y coordinate of a point, and then plots this using a green +. 17. Create a vector x with values ranging from 1 to 100 in steps of 5. Create a vector y which is the square root of each value in x. Plot these points. Now, use the bar function instead of plot to get a bar chart instead. 32
Referências [Livro 1] Capítulo 3 de EssentialMATLAB for EngineersandScientists, BrianHahne Dan Valentine(2007) Elsevier. [Livro 2] Capítulo 2 de Matlab: A Practical Introduction to Programming and Problem Solving, Stormy Attaway(2009) Elsevier. 33