tpavlic

example_num_opt_SHS_physical

Sep 20th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.53 KB | None | 0 0
  1. function [estFl, Fl, fval, exitflag] = example_num_opt_SHS_physical
  2. %
  3. % Physical-parameter numerical optimization example
  4. %
  5.  
  6. %% For testing, generate some random trajectories
  7.  
  8. % Sample once every dt seconds
  9. dt = 5;
  10. t = 0:dt:120;
  11.  
  12. % Noise scale
  13. noisesd = 0.05;
  14.  
  15. % Arbitrary physical parameters (to fit later)
  16.  
  17. % Not fitting these parameters
  18. global g mu mL Fp
  19.  
  20. % Gravity (constant)
  21. g = 9.8;
  22.  
  23. % Coefficient of friction (estimate from empirical measurements of load)
  24. mu = 0.1;
  25.  
  26. % Per-ant pulling force (estimate from empirical measurements)
  27. Fp = 0.1;
  28.  
  29. % Mass of load (weigh it)
  30. mL = 1;
  31.  
  32. % Per-ant lifting force (estimate from data)
  33. Fl = 0.1;
  34.  
  35. % Arbitrary rates for testing
  36. % states are (detached, pulling, lifting)
  37. lambda = [ 0 2 3 ; 5 0 9 ; 2 7 0 ];
  38. %lambda = lambda/max(max(lambda)) * 1/(2*dt);
  39.  
  40. % Graph laplacian (to generate fake count data)
  41. laplacian = lambda.*(1-eye(size(lambda))) - (1-eye(size(lambda)))*lambda.*eye(size(lambda));
  42.  
  43. % Random initial condition for mean-field trajectory
  44. meandata = zeros(length(lambda), length(t));
  45. meandata(:, 1) = randperm(length(lambda))';
  46.  
  47. % Initial position and velocity
  48. v = zeros(length(t),1);
  49. x = zeros(length(t),1);
  50.  
  51. % Generate mean-field trajectories based on Laplacian and physical
  52. % parameters
  53. % (NOTE: This is a naive model of motion that doesn't properly treat the
  54. %        stop-in-the-middle-of-a-sampling-interval case.)
  55. for i = 2:length(t)
  56.     meandata(:, i) = expm(laplacian*t(i))*meandata(:, 1);
  57.     num_pullers = meandata(2, i);
  58.     num_lifters = meandata(3, i);
  59.     Fn = mL*g - sign(v(i-1))*num_lifters*Fl;
  60.     Fg = num_pullers*Fp;
  61.     accel = (Fg - mu*Fn)/mL;
  62.     v(i) = v(i-1) + accel*dt + noisesd*randn;
  63.     x(i) = x(i-1) + v(i-1)*dt + 0.5*accel*dt^2 + noisesd*randn;
  64. end
  65.  
  66.  
  67. %% Use a least-squares fit to get physical parameters back out from meandata
  68.  
  69. % fmincon (non-linear optimization with constraints) arguments:
  70. %    1. Function handle defining the metric to minimize
  71. %    2. Initial condition (all of the off-diagonal elements of lambda matrix)
  72. %    3. Matrix "A" in the inequality constraint A*x <= b
  73. %    4. Vector "b" in the inequality constraint A*x <= b
  74. % In the linear inequality constraint, A*x <= b, "x" is a feasible solution
  75. % to the optimization problem.
  76.  
  77. % The output vector is the optimal parameters for the fit
  78. [estFl, fval, exitflag] = fmincon( @(fitFl)( lsmetric(fitFl, x, v, meandata, t) ), ...
  79.             rand, ... % initial conditions
  80.             [], [], ... % inequalities (A*x <= b)
  81.             [], [], ... % equalities (Aeq*x == beq)
  82.             [ 0 ], ... % lower bounds
  83.             [ Inf ] ... % upper bounds
  84.             );
  85.  
  86. end
  87.  
  88. %% Metric for least-squares fit
  89.  
  90. % This metric sums up the squared differences between the data (i.e.,
  91. % meandata at times t) and a trajectory defined by "lambdavec"
  92.  
  93. function lssum = lsmetric( Fl, x, v, meandata, t )
  94.  
  95.     % Pull in globals
  96.     global g mu mL Fp
  97.  
  98.     testv = zeros(length(t),1);
  99.     testx = zeros(length(t),1);
  100.    
  101.     % This sums up the squared differences over time
  102.     lssum = 0;
  103.     dt = t(2)-t(1);
  104.     for i = 2:length(t)
  105.         num_pullers = meandata(2, i);
  106.         num_lifters = meandata(3, i);
  107.         Fn = mL*g - sign(testv(i-1))*num_lifters*Fl;
  108.         Fg = num_pullers*Fp;
  109.         accel = (Fg - mu*Fn)/mL;
  110.         testv(i) = testv(i-1) + accel*dt;
  111.         testx(i) = testx(i-1) + testv(i-1)*dt + 0.5*accel*dt^2;
  112.  
  113.         % Could also fit the velocities too...
  114.         lssum = lssum + norm( testx(i) - x(i) )^2 + norm( testv(i) - v(i) )^2;
  115.     end
  116.  
  117. end
Advertisement
Add Comment
Please, Sign In to add comment