jueves, 8 de marzo de 2012

Aproximación de Padé de tiempos muertos en Octave

Padé approximation of delays for GNU Octave


--- pade.m ---
## -*- texinfo -*-
## @deftypefn  {Function File} {} pade (@var{q})
## @deftypefnx {Function File} {} pade (@var{q}, @var{n})
##
## Calcula la aproximacion de Pade de orden @var{n} para exp(-q.s) (s = variable compleja),
## y devuelve una funcion de transferencia continua.
##
## @var{q} es un numero entero.
##
## @var{n} es el orden de la aproximacion de Pade. Si se omite se asume @samp{"1"}.
##
## La aproximacion se calcula segun (OZBAY, Hitay. Introduction to Feedback Control Theory. USA: CRC Press, 1999. 232 p. ISBN 0-8493-1867-X):
##
##    $\textrm{e}^{-\Theta\,t}\approxeq\dfrac{\sum_{k=0}^{n}\,(-1)^{k}\, c_{k}\,(\theta s)^{k}}{\sum_{k=0}^{n}\, c_{k}\,(\theta s)^{k}}$
##
##    donde: $c_{k}=\dfrac{(2n-k)!\, n!}{(2n)!\, k!\,(n-k)!}$
##
##    para $k=1,2,\ldots,n$
##
## @end deftypefn

## Author: Alejandro Regodesebes -
## v.1.0 - Copyright (C) 2012 Alejandro Regodesebes

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .
 
function p = pade(q,n=1)
  if (not(isnumeric(q)))
    error("Los argumentos deben ser constantes enteras.");
  endif
  if (nargin == 0 || nargin > 2)
    print_usage ();
  endif
  s = tf("s"); % s es la variable de una función de transferencia
  Num = 0; % Inicializo el numerador
  Den = 0; % Inicializo el denominador
  nn = prod(1:n); % nn = n!
  n2 = prod(1:(2*n)); % n2 = (2n)!
  for k=0:n
    c = (prod(1:(2*n-k))*nn)/(n2*prod(1:k)*prod(1:(n-k)));
    Num = Num + ((-1)**k)*c*(q*s)**k;
    Den = Den + c*(q*s)**k;
  end;
  p = Num/Den;
endfunction
--- fin pade.m ---

miércoles, 7 de marzo de 2012

Transformada y transformada inversa de Laplace en Octave

Symbolic transform and inverse Laplace transform in Octave.
Los dos scripts que siguen utilizan GNU Maxima para calcular la transformada y la transformada inversa de Laplace desde Octave. El resultado es un string que puede ser convertido y utilizado en Octave.

The next two scripts uses GNU Maxima for calculate symbolic transform and inverse Laplace transform from Octave. The result is a string that can be converted and used in Octave.

--- laplace.m ---
## -*- texinfo -*-
## @deftypefn  {Function File} {} laplace (@var{funcion})
## @deftypefnx {Function File} {} laplace (@var{funcion}, @var{t})
## @deftypefnx {Function File} {} laplace (@var{funcion}, @var{t}, @var{s})
##
## Utiliza GNU-Maxima para calcular la transformada de Laplace del string @var{funcion}.
## La salida es un string con la funcion transformada.
## GNU-Maxima puede encontrarse en http://maxima.sourceforge.net/
##
## @var{t} es la variable de la funcion @var{funcion}, si se omite se asumira @samp{"t"}.
## @var{s} es la variable de la transformada, si se omite se asumira @samp{"s"}.
##
## Ejemplos:
##
## @example
## @group
## laplace ("sin(2*t)")
##    @result{} 2/(s^2+4)
## @end group
## @group
## laplace ("sin(2*x)","x")
##    @result{} 2/(s^2+4)
## @end group
## @group
## laplace ("sin(2*x)","x","r")
##    @result{} 2/(r^2+4)
## @end group
## @end example
##
##
## @seealso{ilaplace, maxima}
## @end deftypefn
##
##
## Ejemplo:
##
## @example
## @group
## s = tf ("s");
## @end group
## @group
## g = eval(laplace ("sin(2*t)"))
##
##    @result{} Transfer function 'g' from input 'u1' to output ...
##
##    @result{}          2 
##    @result{}  y1:  -------
##    @result{}       s^2 + 4
##
##    @result{} Continuous-time model.
## @end group
## @end example

## Author: Alejandro Regodesebes -
## v.1.0 - Copyright (C) 2012 Alejandro Regodesebes

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .

function resultado = laplace(comando,t="t",s="s")
    if (not(ischar(t)))
        error("El segundo argumento debe ser un string con la variable de la función a transformar.");
    endif
    if (not(ischar(s)))
        error("El tercer argumento debe ser un string con la variable de la función transformada.");
    endif
    if (nargin == 0 || nargin > 3)
        print_usage ();
        error("Cantidad de argumentos incorrecta.");
    endif
    if (ischar(comando))
        comando = strtrim(comando);
        [nn,salida] = system(['maxima -q --batch-string="display2d:false;laplace(',comando,',',t,',',s,');"']);
        resultado = strtrim(substr(salida,rindex(salida,"(%o")+5));
        resultado(resultado == "%") = "";
    else
        printf("ERROR: el argumento debe ser un string\n");
        print_usage ();
    endif
endfunction
--- fin laplace.m ---

--- ilaplace.m ---
## -*- texinfo -*-
## @deftypefn  {Function File} {} ilaplace (@var{funcion})
## @deftypefnx {Function File} {} ilaplace (@var{funcion}, @var{s})
## @deftypefnx {Function File} {} ilaplace (@var{funcion}, @var{s}, @var{t})
##
## Utiliza GNU-Maxima para calcular la transformada inversa de Laplace del string @var{funcion}.
## La salida es un string con la funcion transformada inversa.
## GNU-Maxima puede encontrarse en http://maxima.sourceforge.net/
##
## @var{s} es la variable de la funcion @var{funcion}, si se omite se asumira @samp{"s"}.
## @var{t} es la variable de la transformada inversa, si se omite se asumira @samp{"t"}.
##
## Ejemplos:
##
## @example
## @group
## ilaplace("10/(s^2+2*s+5)")
##    @result{} 5*e^-t*sin(2*t)
## @end group
## @group
## ilaplace("10/(f^2+2*f+5)","f")
##    @result{} 5*e^-t*sin(2*t)
## @end group
## @group
## ilaplace("10/(f^2+2*f+5)","f","x")
##    @result{} 5*e^-x*sin(2*x)
## @end group
## @end example
##
##
## @seealso{laplace, maxima}
## @end deftypefn

## Author: Alejandro Regodesebes -
## v.1.0 - Copyright (C) 2012 Alejandro Regodesebes

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .

function resultado = ilaplace(comando,s="s",t="t")
    if (not(ischar(s)))
        error("El segundo argumento debe ser un string con la variable de la función transformada.");
    endif
    if (not(ischar(t)))
        error("El tercer argumento debe ser un string con la variable de la función transformada inversa.");
    endif
    if (nargin == 0 || nargin > 3)
        print_usage ();
        error("Cantidad de argumentos incorrecta.");
    endif
    if (ischar(comando))
        comando = strtrim(comando);
        [nn,salida] = system(['maxima -q --batch-string="display2d:false;ilt(',comando,',',s,',',t,');"']);
        resultado = strtrim(substr(salida,rindex(salida,"(%o")+5));
        resultado(resultado == "%") = "";
    else
        printf("ERROR: el argumento debe ser un string\n");
        print_usage ();
    endif
endfunction

--- fin ilaplace.m ---

Llamar a Maxima desde Octave

Calling Maxima from Octave.

El siguiente script define una función en Octave que envía una lista de comandos a Maxima y devuelve un string con la última salida de Maxima.

No es muy elegante, y necesita algo de trabajo, pero cumple su función.

The following script defines a function in Octave that sends a list of commands to Maxima and returns a string with the last departure from Maxima.

It's not elegant, and needs some work, but it does its job.



--- maxima.m ---
## -*- texinfo -*-
## @deftypefn  {Function File} {} maxima (@var{comando})
##
## Ejecuta uno o mas comandos en GNU-Maxima y devuelve un string con el resultado del ultimo comando.
## GNU-Maxima puede encontrarse en http://maxima.sourceforge.net/
## Executes one or more commands in GNU-Maxima and returns a string containing the result of last command.
## GNU-Maxima can be found at http://maxima.sourceforge.net/
##
## @var{comando} es un string conteniendo el o los comandos a ejecutar en GNU-Maxima. Estos comandos deben separarse mediante @samp{";"}.
## @var{comando} is a string containing the command(s) to run on GNU-Maxima. These commands must be separated by @samp{";"}.
##
## Ejemplos/Examples:
##
## @example
## @group
## maxima("1+1;laplace(%,t,s)")
##    @result{} 2/s
## @end group
## @end example
##
##
## @seealso{ilaplace, maxima}
## @end deftypefn

## Author: Alejandro Regodesebes -
## v.1.0.1 - Copyright (C) 2012 Alejandro Regodesebes

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .

function resultado = maxima (comando)
    if ((nargin == 1) && ischar(comando))
        comando = strtrim(comando);
        if (substr(comando, -1) != ";")
            comando = [comando,';'];
        endif
        #{ Llama a Maxima en modo silencioso (-q), para procesar por lotes una lista de argumentos (--batch-string="..."), y le envía como argumentos la orden de mostrar la salida en una sola línea (display2d:false;) más el comando deseado. La salida de Maxima se almacena en el string "salida":#}
        [nn,salida] = system(['maxima -q --batch-string="display2d:false;',comando,'"']);
        #{ Remueve los espacios al principio y al final (strtrim) de la parte del string "salida" (substr) que comienza 5 caracteres después (+5) del primer carácter de del último "(%o" (rindex):#}
        resultado = strtrim(substr(salida,rindex(salida,"(%o")+5));
        #{ Elimina todos los signos "%":#}
        resultado(resultado == "%") = "";
    else
        printf("ERROR: el argumento debe ser un string\n");
        print_usage ();
    endif
endfunction


--- fin maxima.m ---

martes, 6 de marzo de 2012

Acentos en Texmaker

Para poder escribir acentos en Texmaker (y posiblemente en cualquier programa para KDE) en Ubuntu, instalar:

sudo apt-get install ibus-qt4

Además, en Texmaker ir a Opciones -> Configurar Texmaker -> Editor y poner la codificación del editor en la usada para el documento (UTF-8 me funciona).

Actualización para Ubuntu 11.10:

La versión de Texmaker de los repositorios es obsoleta, y no permite escribir acentos cuando se utiliza ibus.

1) Desinstalar Texmaker.

2) Instalar la última versión, descargada desde la página oficial de Texmaker:  http://www.xm1math.net/texmaker/download.html#linux

3) Instalar ibus-qt4:

sudo apt-get install ibus-qt4

4) En Texmaker, verificar que se utilice la codificación UTF-8:
Opciones -> Configurar Texmaker -> Editor

5) A la hora de escribir, en Texmaker hacer clic con el botón derecho del ratón sobre un área de la página, y en el menú contextual elegir:
Seleccionar IM  -> ibus

viernes, 6 de mayo de 2011

Montar particiones en Ubuntu 11.04

1) Dash > Utilidad de discos

2) Seleccionar la unidad y montarla.

Para hacer que la unidad se monte automáticamente al inicio:

3) Anotar el nombre de la unidad (ej: /dev/sda4).

4) Dash > Terminal:

$ sudo gedit /etc/mtab

5)  Buscar el nombre de la unidad y copiar la línea (ej.: /dev/sda4 /media/main ext4 rw,nosuid,nodev,uhelper=udisks 0 0 ).

6) Abrir una nueva pestaña en la terminal:

$ sudo gedit /etc/fstab

7) Pegar la línea y guardar. Cerrar todas las ventanas.

8) En una terminal nueva, testear con:

$ sudo mount -a

(Si no da error, reiniciar tranquilo.)

Fuente: http://www.muktware.com/man/1075?page=0,1

viernes, 22 de abril de 2011

Cómo restaurar el panel de gnome en Ubuntu.

Para restaurar el panel de gnome a su estado original, ejecutar el comando:


gconftool --recursive-unset /apps/panel && killall gnome-panel

Probado en Ubuntu 10.10.