Revista Brasileira de Computação Aplicada(ISSN 2176-6649), Passo Fundo, v. 7, n. 2, p. 16-25, mai. 2015 16 A Matlab code to fit periodic data Ruth da Silva Brum 1 Jairo Valões de Alencar Ramalho 2 Luiz Alberto Oliveira Rocha 1 Liércio André Isoldi 3 Elizaldo Domingues dos Santos 3 Abstract: This paper presents a computer method to find the best sine-based function, in the sense of least squares, to fit periodic data. Even though the least squares method is not a novelty, there is a void in the literature about its use to find trigonometric functions, particularly when it gives rise to nonlinear systems, as it is done in this article. The respective code, implemented in the Matlab programming language, is detailed and analyzed exploring experimental data from the air and soil temperatures measured along the year in earth air heat exchangers (EAHE) built in the facilities of a case study house in the Brazilian state of Rio Grande do Sul. The fitting curves have been employed by the authors in different works to define boundary conditions to study new computer models of EAHE. Keywords: Earth air heat exchanger. Least squares. Trigonometric curve fitting. Resumo: Este artigo apresenta um método computacional para encontrar uma função do tipo senoidal que melhor se adequa, no sentido dos mínimos quadrados, a um conjunto de dados periódicos. Embora o método dos mínimos quadrados não seja uma novidade, foi percebida uma lacuna na literatura relativa ao seu uso para a obtenção de funções trigonométricas, particularmente envolvendo sistemas não lineares, como é feito neste trabalho. O código proposto, escrito na linguagem de programação do Matlab, é detalhado e avaliado por meio da exploração de dados empíricos e numéricos das temperaturas do ar e do solo medidos em trocadores de calor solo-ar (TCSA) instalados em uma casa experimental no interior do estado brasileiro do Rio Grande do Sul. As curvas ajustadas estão sendo empregadas pelos autores em diferentes pesquisas na definição de condições de contorno para novos modelos computacionais de TCSA. Palavras-chave: Ajuste de funções trigonométricas. Mínimos quadrados. Trocadores de calor solo-ar. 1 Introduction To face the increasing energy consumption, it is necessary to consider not only alternative/renewable energy sources and devices, but also to review the building construction models. As a remarkable example, in the summer of 2014, there was a change in the electrical energy demand peak in Brazil. It occurred at noon, instead of late afternoon, due to a heavy use of air conditioners during a heat wave [1]. In this context, the Earth Air Heat Exchangers (EAHE) stand out as a natural solution to improve air conditioning at low energy cost. In general, EAHE are composed by a system of fans and buried ducts. The fans blow the air inside the ducts, where it exchanges heat with the soil and returns at a milder temperature [2, 3, 4]. 1 Programa de Pós-Graduação em Engenharia Mecânica, UFRGS, Porto Alegre (RS), Brasil. {ruth.silva.brum@ufpel.edu.br, luizrocha@mecanica.ufrgs.br} 2 Departamento de Matemática e Estatística, UFPel, Capão do Leão (RS), Brasil. {jairo.ramalho@ufpel.edu.br} 3 Programa de Pós-Graduação em Modelagem Computacional, Furg, Rio Grande (RS), Brasil. {liercioisoldi,elizaldosantos@furg.br} http://dx.doi.org/10.5335/rbca.2015.4618