spm_int_B.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. function [y] = spm_int_B(P,M,U)
  2. % Integrate a MIMO nonlinear system using a bilinear Jacobian
  3. % FORMAT [y] = spm_int_B(P,M,U)
  4. % P - model parameters
  5. % M - model structure
  6. % U - input structure or matrix
  7. %
  8. % y - (v x l) response y = g(x,u,P)
  9. %__________________________________________________________________________
  10. %
  11. % Integrates the MIMO system described by
  12. %
  13. % dx/dt = f(x,u,P,M)
  14. % y = g(x,u,P,M)
  15. %
  16. % using the update scheme:
  17. %
  18. % x(t + dt) = x(t) + U*dx(t)/dt
  19. %
  20. % U = (expm(dt*J) - I)*inv(J)
  21. % J = df/dx
  22. %
  23. % at input times. This integration scheme evaluates the update matrix (Q)
  24. % at each time point
  25. %
  26. %--------------------------------------------------------------------------
  27. %
  28. % SPM solvers or integrators
  29. %
  30. % spm_int_ode: uses ode45 (or ode113) which are one and multi-step solvers
  31. % respectively. They can be used for any ODEs, where the Jacobian is
  32. % unknown or difficult to compute; however, they may be slow.
  33. %
  34. % spm_int_J: uses an explicit Jacobian-based update scheme that preserves
  35. % nonlinearities in the ODE: dx = (expm(dt*J) - I)*inv(J)*f. If the
  36. % equations of motion return J = df/dx, it will be used; otherwise it is
  37. % evaluated numerically, using spm_diff at each time point. This scheme is
  38. % infallible but potentially slow, if the Jacobian is not available (calls
  39. % spm_dx).
  40. %
  41. % spm_int_E: As for spm_int_J but uses the eigensystem of J(x(0)) to eschew
  42. % matrix exponentials and inversion during the integration. It is probably
  43. % the best compromise, if the Jacobian is not available explicitly.
  44. %
  45. % spm_int_B: As for spm_int_J but uses a first-order approximation to J
  46. % based on J(x(t)) = J(x(0)) + dJdx*x(t).
  47. %
  48. % spm_int_L: As for spm_int_B but uses J(x(0)).
  49. %
  50. % spm_int_U: like spm_int_J but only evaluates J when the input changes.
  51. % This can be useful if input changes are sparse (e.g., boxcar functions).
  52. % It is used primarily for integrating EEG models
  53. %
  54. % spm_int: Fast integrator that uses a bilinear approximation to the
  55. % Jacobian evaluated using spm_bireduce. This routine will also allow for
  56. % sparse sampling of the solution and delays in observing outputs. It is
  57. % used primarily for integrating fMRI models
  58. %___________________________________________________________________________
  59. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  60. % Karl Friston
  61. % $Id: spm_int_B.m 5219 2013-01-29 17:07:07Z spm $
  62. % convert U to U.u if necessary
  63. %--------------------------------------------------------------------------
  64. if ~isstruct(U), U.u = U; end
  65. try, dt = U.dt; catch, dt = 1; end
  66. % state equation; add [0] states if not specified
  67. %--------------------------------------------------------------------------
  68. try
  69. f = fcnchk(M.f,'x','u','P','M');
  70. catch
  71. f = inline('sparse(0,1)','x','u','P','M');
  72. M.n = 0;
  73. M.x = sparse(0,0);
  74. end
  75. % and output nonlinearity
  76. %--------------------------------------------------------------------------
  77. try
  78. g = fcnchk(M.g,'x','u','P','M');
  79. end
  80. % Initial states and inputs
  81. %--------------------------------------------------------------------------
  82. u = U.u(1,:);
  83. try
  84. x = M.x;
  85. catch
  86. x = sparse(0,1);
  87. M.x = x;
  88. end
  89. % check for delay operator
  90. %--------------------------------------------------------------------------
  91. try
  92. [fx,dfdx,D] = f(x,u,P,M);
  93. catch
  94. D = 1;
  95. end
  96. % get Jacobian and its derivatives
  97. %--------------------------------------------------------------------------
  98. [dJdx,J] = spm_diff(f,x,u,P,M,[1 1]);
  99. [dJdu,J] = spm_diff(f,x,u,P,M,[1 2]);
  100. % integrate
  101. %==========================================================================
  102. x0 = spm_vec(M.x);
  103. for i = 1:size(U.u,1)
  104. % input
  105. %----------------------------------------------------------------------
  106. u = U.u(i,:);
  107. % motion
  108. %----------------------------------------------------------------------
  109. fx = f(x,u,P,M);
  110. % dx(t)/dt and Jacobian df/dx
  111. %----------------------------------------------------------------------
  112. dx = spm_vec(x) - x0;
  113. dfdx = J;
  114. for j = 1:length(dJdx)
  115. dfdx = dfdx + dJdx{j}*dx(j);
  116. end
  117. for j = 1:length(dJdu)
  118. dfdx = dfdx + dJdu{j}*u(j);
  119. end
  120. % update dx = (expm(dt*J) - I)*inv(J)*fx
  121. %----------------------------------------------------------------------
  122. x = spm_unvec(spm_vec(x) + spm_dx(D*dfdx,D*fx,dt),x);
  123. % output - implement g(x)
  124. %----------------------------------------------------------------------
  125. try
  126. y(:,i) = spm_vec(g(x,u,P,M));
  127. catch
  128. y(:,i) = spm_vec(x);
  129. end
  130. end
  131. % transpose
  132. %--------------------------------------------------------------------------
  133. y = real(y');