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
## @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
## @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
No hay comentarios:
Publicar un comentario