Prev: saving figure
Next: scripts, simulink, masks
From: Robert on 27 May 2010 08:44 Dear all, I've got a question I'm quite sure most of you'll be able to answer, but I simply cannot find the correct command. The problem: I've got an array y, where the value of y(k+1) is a function of y(k) - in other words, it is a backward Euler problem. I'd now like to simulate this function from some initial value. The simplified code: y(1) = 10; % initial value for i=1:1000; y(i+1) = y(i) + func(y(i), p(i)) end where func is an arbitrary, nonlinear function and p is a set of parameters. The thing is that I need this simulation within an optimization objective function, hence it is too (computationally) heavy as is. Is there a way to remove the loop and simulate it in an implicit way? Thanks a lot for any hints. Regards, Robert
From: Robert on 31 May 2010 04:12 anyone?
From: James Allison on 3 Jun 2010 11:15 What you describe below is the forward Euler method. The backward Euler method requires you to solve numerically for y(i+1) at each time step (you could use fzero/fsolve for this within the for loop for example). http://en.wikipedia.org/wiki/Numerical_ordinary_differential_equations#The_backward_Euler_method There is no way to simulate the system that involves an arbitrary function without some type of loop. Higher-order methods can be more efficient, requiring fewer function calls and less time to simulate the system to a given accuracy. Try ode45, or ode15s if the problem is stiff. When using optimization with simulations, you need to account for the non-smoothness in the simulation response due to discretization. If you are using a gradient-based optimization algorithm (such as fminunc for fmincon), you need to make sure the finite difference step size is large enough to 'skip over' the non-smoothness. Reducing the simulation step size can improve smoothness, but at the expense of longer simulation times. If you are using a gradient-free method (such as fminsearch) smoothness is not an issue. -James Robert wrote: > Dear all, > > I've got a question I'm quite sure most of you'll be able to answer, but > I simply cannot find the correct command. > The problem: > I've got an array y, where the value of y(k+1) is a function of y(k) - > in other words, it is a backward Euler problem. I'd now like to simulate > this function from some initial value. The simplified code: > > y(1) = 10; % initial value > for i=1:1000; > y(i+1) = y(i) + func(y(i), p(i)) > end > > where func is an arbitrary, nonlinear function and p is a set of > parameters. The thing is that I need this simulation within an > optimization objective function, hence it is too (computationally) heavy > as is. Is there a way to remove the loop and simulate it in an implicit > way? > > Thanks a lot for any hints. Regards, > > Robert
From: Robert on 21 Jun 2010 10:42 Dear James, thanks a lot for your extensive reply. You're absolutely correct on the forward / backward thing: apologies. Thanks a lot also for the point on the optimization smoothness: I'll definitely keep that in mind. It's unfortunate that a loop-free approach is impossible, but from a physical point of view seems illogical anyways... thanks! Robert James Allison <james.allison(a)mathworks.com> wrote in message <hu8guj$1q0$1(a)fred.mathworks.com>... > What you describe below is the forward Euler method. The backward Euler > method requires you to solve numerically for y(i+1) at each time step > (you could use fzero/fsolve for this within the for loop for example). > > http://en.wikipedia.org/wiki/Numerical_ordinary_differential_equations#The_backward_Euler_method > > There is no way to simulate the system that involves an arbitrary > function without some type of loop. Higher-order methods can be more > efficient, requiring fewer function calls and less time to simulate the > system to a given accuracy. Try ode45, or ode15s if the problem is stiff. > > When using optimization with simulations, you need to account for the > non-smoothness in the simulation response due to discretization. If you > are using a gradient-based optimization algorithm (such as fminunc for > fmincon), you need to make sure the finite difference step size is large > enough to 'skip over' the non-smoothness. Reducing the simulation step > size can improve smoothness, but at the expense of longer simulation > times. If you are using a gradient-free method (such as fminsearch) > smoothness is not an issue. > > -James
|
Pages: 1 Prev: saving figure Next: scripts, simulink, masks |