4 views (last 30 days)

Jeff Miller
on 12 Jun 2020

Sorry, I am having trouble relating your code to your equation, and I would have no idea how to do this with syms. But maybe good old fminsearch will help, something like this:

m = rand(10,1)*3; % I generate some random values to use for m & t.

t = rand(10,1)*3; % I guess you have real values to use.

startXs = [1 1 1 1 1]; % starting values for u,k,alpha,c,P, in that order (wild guesses)

% here is the function you want to minimize.

% fminsearch will adjust the x values

% m & t are your "data" set above

fun2Minimize = @(x) dblSumFun(x(1),x(2),x(3),x(4),x(5),m,t);

% call fminsearch and it will try to find the best values

% for u,k,alpha,c,P

ests = fminsearch(fun2Minimize,startXs)

function dblSum = dblSumFun(u,k,alpha,c,P,m,t)

% the function that you want to minimize

dblSum = 0;

mc = 2.5;

for i=1:length(m)

jSum = 0;

for j=1:i-1

jTerm = k * exp( alpha*(m(j) - mc) ) / (t(i) - t(j) + c)^P;

jSum = jSum + jTerm;

end

iTerm = log(u + jSum);

dblSum = dblSum + iTerm;

end

end

Note that I have ignored the bounds that you specified. Maybe you won't need those if the best parameters are inside the boundaries. If you do need them, a little more code will be required inside dblSumFun. But I don't want to elaborate because maybe this is not at all what you want.

By the way, don't start the parameter values near the boundary as you seem to have done in your sample code. Start at the most plausible values you can think of, which are presumably more in the middle of the range.

Jeff Miller
on 13 Jun 2020

Well, maybe you can just use the dblSumFun with simannealbnd. But if not...

A standard trick for enforcing bounds with search routines is to use a function that maps any real number into a desired range. For example,

function trans = Real2Bounded(minimum,maximum,real)

% Convert an arbitrary real number to a number within certain bounds.

trans = (maximum - minimum) ./ (exp(-real*0.005) + 1) + minimum;

if isnan(trans)

fprintf('Real2Bounded error at min = %f, max = %f, real = %f\n',minimum,maximum,real);

end

end

One (pretty ugly) way to use this function is to change

function dblSum = dblSumFun(u,k,alpha,c,P,m,t)

to

function dblSum = dblSumFun(uReal,kReal,alphaReal,cReal,Preal,m,t)

u = Real2Bounded(0.001,1000,uReal);

k = Real2Bounded(0.001,1000,kReal); % and so on

If you do that, then you have to realize that fminsearch is not modifying your actual parameter values but instead modifying these reals that are transformed into your parameter values. To accommodat that, you have to use the reverse transform in dealing with fminsearch:

function real = Bounded2Real(minimum,maximum,trans)

% Inverse of Real2Bounded: Convert a real number within certain bounds to an arbitrary real number.

real = log( (trans-minimum) ./ (maximum-trans)) / 0.005;

if isnan(real)

fprintf('Bounded2Real error at min = %f, max = %f, trans = %f\n',minimum,maximum,trans);

end

end

For example, the starting values that you pass to fminsearch need to be transformed into the real scale that it will be using:

uStart = 1; % the actual u to start with

uRealStart = Bounded2Real(0.001,1000,uStart); % the arbitrary real for that u

startXs = [uRealStart, etc]

Then, after fminsearch runs, you need to transform its final x's back into your desired parameter estimates:

ests = fminsearch(fun2Minimize,startXs)

uFinalest = Real2Bounded(0.001,1000,ests(1)); % and so on

It's tedious to do this for all the parameters, but it works.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/546704-how-can-i-do-a-double-summation-with-dependent-indices#comment_894729

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/546704-how-can-i-do-a-double-summation-with-dependent-indices#comment_894729

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/546704-how-can-i-do-a-double-summation-with-dependent-indices#comment_894819

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/546704-how-can-i-do-a-double-summation-with-dependent-indices#comment_894819

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/546704-how-can-i-do-a-double-summation-with-dependent-indices#comment_895536

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/546704-how-can-i-do-a-double-summation-with-dependent-indices#comment_895536

Sign in to comment.