Advertisement
Guest User

Untitled

a guest
May 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 11.75 KB | None | 0 0
  1. function E = ml(z,alpha,beta,gama)
  2. %
  3. % Evaluation of the Mittag-Leffler (ML) function with 1, 2 or 3 parameters
  4. % by means of the OPC algorithm [1]. The routine evaluates an approximation
  5. % Et of the ML function E such that |E-Et|/(1+|E|) approx 1.0e-15  
  6. %    
  7. %
  8. % E = ML(z,alpha) evaluates the ML function with one parameter alpha for
  9. % the corresponding elements of z; alpha must be a real and positive
  10. % scalar. The one parameter ML function is defined as
  11. %
  12. % E = sum_{k=0}^{infty} z^k/Gamma(alpha*k+1)
  13. %
  14. % with Gamma the Euler's gamma function.
  15. %
  16. %
  17. % E = ML(z,alpha,beta) evaluates the ML function with two parameters alpha
  18. % and beta for the corresponding elements of z; alpha must be a real and
  19. % positive scalar and beta a real scalar. The two parameters ML function is
  20. % defined as
  21. %
  22. % E = sum_{k=0}^{infty} z^k/Gamma(alpha*k+beta)
  23. %
  24. %
  25. % E = ML(z,alpha,beta,gama) evaluates the ML function with three parameters
  26. % alpha, beta and gama for the corresponding elements of z; alpha must be a
  27. % real scalar such that 0<alpha<1, beta any real scalar and gama a real and
  28. % positive scalar; the arguments z must satisfy |Arg(z)| > alpha*pi. The
  29. % three parameters ML function is defined as
  30. %
  31. % E = sum_{k=0}^{infty} Gamma(gama+k)*z^k/Gamma(gama)/k!/Gamma(alpha*k+beta)
  32. %
  33. %
  34. % NOTE:
  35. % This routine implements the optimal parabolic contour (OPC) algorithm
  36. % described in [1] and based on the inversion of the Laplace transform on a
  37. % parabolic contour suitably choosen in one of the regions of analyticity
  38. % of the Laplace transform.
  39. %
  40. %
  41. % REFERENCES
  42. %
  43. %   [1] R. Garrappa, Numerical evaluation of two and three parameter
  44. %   Mittag-Leffler functions, SIAM Journal of Numerical Analysis, 2015,
  45. %   53(3), 1350-1369
  46. %
  47. %
  48. %   Please, report any problem or comment to :
  49. %        roberto dot garrappa at uniba dot it
  50. %
  51. %   Copyright (c) 2015, Roberto Garrappa, University of Bari, Italy
  52. %   roberto dot garrappa at uniba dot it
  53. %   Homepage: http://www.dm.uniba.it/Members/garrappa
  54. %   Revision: 1.4 - Date: October 8 2015
  55.  
  56.  
  57. % Check inputs
  58. if nargin < 4
  59.     gama = 1 ;
  60.     if nargin < 3
  61.         beta = 1 ;
  62.         if nargin < 2
  63.             error('MATLAB:ml:NumberParameters', ...
  64.                 'The parameter ALPHA must be specified.');
  65.         end
  66.     end
  67. end
  68.  
  69. % Check whether the parameters ALPHA, BETA and GAMMA are scalars
  70. if length(alpha) > 1 || length(beta) > 1 || length(gama) > 1
  71.     alpha = alpha(1) ; beta = beta(1) ; gama = gama(1) ;
  72.     warning('MATLAB:ml:ScalarParameters', ...
  73.         ['ALPHA, BETA and GAMA must be scalar parameters. ', ...
  74.         'Only the first values ALPHA=%f BETA=%f and GAMA=%f will be used. '], ...
  75.         alpha, beta, gama) ;
  76. end
  77.  
  78. % Check whether the parameters meet the contraints
  79. if real(alpha) <= 0 || real(gama) <= 0 || ~isreal(alpha) || ...
  80.         ~isreal(beta) || ~isreal(gama)
  81.     error('MATLAB:ml:ParametersOutOfRange', ...
  82.         ['Error in the parameters of the Mittag-Leffler function. ', ...
  83.         'Parameters ALPHA and GAMA must be real and positive. ', ...
  84.         'The parameter BETA must be real.']) ;
  85. end
  86.  
  87. % Check parameters and arguments for the three parameter case
  88. if abs(gama-1) > eps
  89.     if alpha > 1
  90.         error('MATLAB:ml:ALPHAOutOfRange',...
  91.             ['With the three parameters Mittag-Leffler function ', ...
  92.             'the parameter ALPHA must satisfy 0 < ALPHA < 1']) ;
  93.     end
  94.     if min(abs(angle(z(abs(z)>eps)))) <= alpha*pi
  95.         error('MATLAB:ml:ThreeParametersArgument', ...
  96.             ['With the three parameters Mittag-Leffler function ', ...
  97.             'this code works only when |Arg(z)|>alpha*pi.']);
  98.     end
  99. end
  100.  
  101. % Target precision
  102. log_epsilon = log(10^(-15)) ;
  103.  
  104. % Inversion of the LT for each element of z
  105. E = zeros(size(z)) ;  
  106. for k = 1 : length(z)
  107.     if abs(z(k)) < 1.0e-15
  108.         E(k) = 1/gamma(beta) ;
  109.     else
  110.         E(k) = LTInversion(1,z(k),alpha,beta,gama,log_epsilon) ;
  111.     end
  112. end
  113.  
  114.  
  115. end
  116.  
  117.  
  118. % =========================================================================
  119. % Evaluation of the ML function by Laplace transform inversion
  120. % =========================================================================
  121. function E = LTInversion(t,lambda,alpha,beta,gama,log_epsilon)
  122.  
  123. % Evaluation of the relevant poles
  124. theta = angle(lambda) ;
  125. kmin = ceil(-alpha/2 - theta/2/pi) ;
  126. kmax = floor(alpha/2 - theta/2/pi) ;
  127. k_vett = kmin : kmax ;
  128. s_star = abs(lambda)^(1/alpha) * exp(1i*(theta+2*k_vett*pi)/alpha) ;
  129.  
  130. % Evaluation of phi(s_star) for each pole
  131. phi_s_star = (real(s_star)+abs(s_star))/2 ;
  132.  
  133. % Sorting of the poles according to the value of phi(s_star)
  134. [phi_s_star , index_s_star ] = sort(phi_s_star) ;
  135. s_star = s_star(index_s_star) ;
  136.  
  137. % Deleting possible poles with phi_s_star=0
  138. index_save = phi_s_star > 1.0e-15 ;
  139. s_star = s_star(index_save) ;
  140. phi_s_star = phi_s_star(index_save) ;
  141.  
  142. % Inserting the origin in the set of the singularities
  143. s_star = [0, s_star] ;
  144. phi_s_star = [0, phi_s_star] ;
  145. J1 = length(s_star) ; J = J1 - 1 ;
  146.  
  147. % Strength of the singularities
  148. p = [ max(0,-2*(alpha*gama-beta+1)) , ones(1,J)*gama ]  ;
  149. q = [ ones(1,J)*gama , +Inf] ;
  150. phi_s_star = [phi_s_star, +Inf] ;
  151.  
  152. % Looking for the admissible regions with respect to round-off errors
  153. admissible_regions = find( ...
  154.     (phi_s_star(1:end-1) < (log_epsilon - log(eps))/t) & ...
  155.     (phi_s_star(1:end-1) < phi_s_star(2:end))) ;
  156.  
  157. % Initializing vectors for optimal parameters
  158. JJ1 = admissible_regions(end) ;
  159. mu_vett = ones(1,JJ1)*Inf ;
  160. N_vett = ones(1,JJ1)*Inf ;
  161. h_vett = ones(1,JJ1)*Inf ;
  162.  
  163. % Evaluation of parameters for inversion of LT in each admissible region
  164. find_region = 0 ;
  165. while ~find_region
  166.     for j1 = admissible_regions
  167.         if j1 < J1
  168.             [muj,hj,Nj] = OptimalParam_RB ...
  169.                 (t,phi_s_star(j1),phi_s_star(j1+1),p(j1),q(j1),log_epsilon) ;
  170.         else
  171.             [muj,hj,Nj] = OptimalParam_RU(t,phi_s_star(j1),p(j1),log_epsilon) ;
  172.         end
  173.         mu_vett(j1) = muj ; h_vett(j1) = hj ; N_vett(j1) = Nj ;
  174.     end
  175.     if min(N_vett) > 200
  176.         log_epsilon = log_epsilon +log(10) ;
  177.     else
  178.         find_region = 1 ;
  179.     end
  180. end
  181.    
  182.  
  183. % Selection of the admissible region for integration which involves the
  184. % minimum number of nodes
  185. [N, iN] = min(N_vett) ; mu = mu_vett(iN) ; h = h_vett(iN) ;
  186.  
  187. % Evaluation of the inverse Laplace transform
  188. k = -N : N ;
  189. u = h*k ;
  190. z = mu*(1i*u+1).^2 ;
  191. zd = -2*mu*u + 2*mu*1i ;
  192. zexp = exp(z*t) ;
  193. F = z.^(alpha*gama-beta)./(z.^alpha - lambda).^gama.*zd ;
  194. S = zexp.*F ;
  195. Integral = h*sum(S)/2/pi/1i ;
  196.  
  197. % Evaluation of residues
  198. ss_star = s_star(iN+1:end) ;
  199. Residues = sum(1/alpha*(ss_star).^(1-beta).*exp(t*ss_star)) ;
  200.  
  201. % Evaluation of the ML function
  202. E = Integral + Residues ;
  203. if isreal(lambda)
  204.     E = real(E) ;
  205. end
  206.  
  207. end
  208.  
  209.  
  210. % =========================================================================
  211. % Finding optimal parameters in a right-bounded region
  212. % =========================================================================
  213. function [muj,hj,Nj] = OptimalParam_RB ...
  214.     (t, phi_s_star_j, phi_s_star_j1, pj, qj, log_epsilon)
  215.  
  216. % Definition of some constants
  217. log_eps = -36.043653389117154 ; % log(eps)
  218. fac = 1.01 ;
  219. conservative_error_analysis = 0 ;
  220.  
  221. % Maximum value of fbar as the ration between tolerance and round-off unit
  222. f_max = exp(log_epsilon - log_eps) ;
  223.  
  224. % Evaluation of the starting values for sq_phi_star_j and sq_phi_star_j1
  225. sq_phi_star_j = sqrt(phi_s_star_j) ;
  226. threshold = 2*sqrt((log_epsilon - log_eps)/t) ;
  227. sq_phi_star_j1 = min(sqrt(phi_s_star_j1), threshold - sq_phi_star_j) ;
  228.  
  229. % Zero or negative values of pj and qj
  230. if pj < 1.0e-14 && qj < 1.0e-14
  231.     sq_phibar_star_j = sq_phi_star_j ;
  232.     sq_phibar_star_j1 = sq_phi_star_j1 ;
  233.     adm_region = 1 ;
  234. end
  235.  
  236. % Zero or negative values of just pj
  237. if pj < 1.0e-14 && qj >= 1.0e-14
  238.     sq_phibar_star_j = sq_phi_star_j ;
  239.     if sq_phi_star_j > 0
  240.         f_min = fac*(sq_phi_star_j/(sq_phi_star_j1-sq_phi_star_j))^qj ;
  241.     else
  242.         f_min = fac ;
  243.     end
  244.     if f_min < f_max
  245.         f_bar = f_min + f_min/f_max*(f_max-f_min) ;
  246.         fq = f_bar^(-1/qj) ;
  247.         sq_phibar_star_j1 = (2*sq_phi_star_j1-fq*sq_phi_star_j)/(2+fq) ;
  248.         adm_region = 1 ;
  249.     else
  250.         adm_region = 0 ;
  251.     end
  252. end
  253.  
  254. % Zero or negative values of just qj
  255. if pj >= 1.0e-14 && qj < 1.0e-14
  256.     sq_phibar_star_j1 = sq_phi_star_j1 ;
  257.     f_min = fac*(sq_phi_star_j1/(sq_phi_star_j1-sq_phi_star_j))^pj ;
  258.     if f_min < f_max
  259.         f_bar = f_min + f_min/f_max*(f_max-f_min) ;
  260.         fp = f_bar^(-1/pj) ;
  261.         sq_phibar_star_j = (2*sq_phi_star_j+fp*sq_phi_star_j1)/(2-fp) ;
  262.         adm_region = 1 ;
  263.     else
  264.         adm_region = 0 ;
  265.     end
  266. end
  267.  
  268. % Positive values of both pj and qj
  269. if pj >= 1.0e-14 && qj >= 1.0e-14
  270.     f_min = fac*(sq_phi_star_j+sq_phi_star_j1)/...
  271.         (sq_phi_star_j1-sq_phi_star_j)^max(pj,qj) ;
  272.     if f_min < f_max
  273.         f_min = max(f_min,1.5) ;
  274.         f_bar = f_min + f_min/f_max*(f_max-f_min) ;
  275.         fp = f_bar^(-1/pj) ;
  276.         fq = f_bar^(-1/qj) ;
  277.         if ~conservative_error_analysis
  278.             w = -phi_s_star_j1*t/log_epsilon ;
  279.         else
  280.             w = -2*phi_s_star_j1*t/(log_epsilon-phi_s_star_j1*t) ;
  281.         end
  282.         den = 2+w - (1+w)*fp + fq ;
  283.         sq_phibar_star_j = ((2+w+fq)*sq_phi_star_j + fp*sq_phi_star_j1)/den ;
  284.         sq_phibar_star_j1 = (-(1+w)*fq*sq_phi_star_j ...
  285.             + (2+w-(1+w)*fp)*sq_phi_star_j1)/den ;
  286.         adm_region = 1 ;
  287.     else
  288.         adm_region = 0 ;
  289.     end
  290. end
  291.  
  292. if adm_region
  293.     log_epsilon = log_epsilon  - log(f_bar) ;
  294.     if ~conservative_error_analysis
  295.         w = -sq_phibar_star_j1^2*t/log_epsilon ;
  296.     else
  297.         w = -2*sq_phibar_star_j1^2*t/(log_epsilon-sq_phibar_star_j1^2*t) ;
  298.     end
  299.     muj = (((1+w)*sq_phibar_star_j + sq_phibar_star_j1)/(2+w))^2 ;
  300.     hj = -2*pi/log_epsilon*(sq_phibar_star_j1-sq_phibar_star_j)...
  301.         /((1+w)*sq_phibar_star_j + sq_phibar_star_j1) ;
  302.     Nj = ceil(sqrt(1-log_epsilon/t/muj)/hj) ;
  303. else
  304.     muj = 0 ; hj = 0 ; Nj = +Inf ;
  305. end
  306.  
  307. end
  308.  
  309. % =========================================================================
  310. % Finding optimal parameters in a right-unbounded region
  311. % =========================================================================
  312. function [muj,hj,Nj] = OptimalParam_RU (t, phi_s_star_j, pj, log_epsilon)
  313.  
  314. % Evaluation of the starting values for sq_phi_star_j
  315. sq_phi_s_star_j = sqrt(phi_s_star_j) ;
  316. if phi_s_star_j > 0
  317.     phibar_star_j = phi_s_star_j*1.01 ;
  318. else
  319.     phibar_star_j = 0.01 ;
  320. end
  321. sq_phibar_star_j = sqrt(phibar_star_j) ;
  322.  
  323. % Definition of some constants
  324. f_min = 1 ; f_max = 10 ; f_tar = 5 ;
  325.  
  326. % Iterative process to look for fbar in [f_min,f_max]
  327. stop = 0 ;
  328. while ~stop
  329.     phi_t = phibar_star_j*t ; log_eps_phi_t = log_epsilon/phi_t ;
  330.     Nj = ceil(phi_t/pi*(1 - 3*log_eps_phi_t/2 + sqrt(1-2*log_eps_phi_t))) ;
  331.     A = pi*Nj/phi_t ;
  332.     sq_muj = sq_phibar_star_j*abs(4-A)/abs(7-sqrt(1+12*A)) ;
  333.     fbar = ((sq_phibar_star_j-sq_phi_s_star_j)/sq_muj)^(-pj) ;
  334.     stop = (pj < 1.0e-14) || (f_min < fbar && fbar < f_max) ;
  335.     if ~stop
  336.         sq_phibar_star_j = f_tar^(-1/pj)*sq_muj + sq_phi_s_star_j ;
  337.         phibar_star_j = sq_phibar_star_j^2 ;
  338.     end
  339. end
  340. muj = sq_muj^2 ;
  341. hj = (-3*A - 2 + 2*sqrt(1+12*A))/(4-A)/Nj ;
  342.  
  343. % Adjusting integration parameters to keep round-off errors under control
  344. log_eps = log(eps) ; threshold = (log_epsilon - log_eps)/t ;
  345. if muj > threshold
  346.     if abs(pj) < 1.0e-14 , Q = 0 ; else Q = f_tar^(-1/pj)*sqrt(muj) ; end
  347.     phibar_star_j = (Q + sqrt(phi_s_star_j))^2 ;
  348.     if phibar_star_j < threshold
  349.         w = sqrt(log_eps/(log_eps-log_epsilon)) ;
  350.         u = sqrt(-phibar_star_j*t/log_eps) ;
  351.         muj = threshold ;
  352.         Nj = ceil(w*log_epsilon/2/pi/(u*w-1)) ;
  353.         hj = sqrt(log_eps/(log_eps - log_epsilon))/Nj ;
  354.     else
  355.         Nj = +Inf ; hj = 0 ;
  356.     end
  357. end
  358.  
  359. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement