How to minimize a function without using a loop

I have an equation with 3 constant (Nug & Sill, Lag) and 2 variable (R[ranges from 1 to 4000], alpha[ranges from 1 to 2, step of 0.1]). I need to find the value R and alpha that minimizes the function. Is there any Matlab function or any way i can do it without having to do a doublé for loop.
%q = 4000
for i=1:q
Theoritical(i,1) = Nug+Sill*(1-exp(-(Lag(i,1)/R)^alpha));
end
Thank you. Darl.

回答(1 个)

It doesn't look like your Lag is much of a constant if there's 4000 values for it. Does Lag changes with R (since there's also 4000 R values)?
You can use fminsearch or fminbnd but if there's only 4000 values for R and 11 for alpha, you could simply calculate the result for all of them at once and get the minimum :
[R, alpha] = ndgrid(1:4000, 1:0.1:2); %get all combinations of R and alpha
result = Nug + Sill * (1 - exp(-bsxfun(@rdivide, Lag, R) .^ alpha)); %assumes Lag is a column vector with 4000 elements
[minvalue, location] = min(result(:));
minR = R(location)
minalpha = alpha(location)

5 个评论

Thank you @Guillaume. Lag is vector with 11 elements ranging from 0 to 4000.
Oh! I'm not sure why you accepted the answer then as it's not going to work.
What is your equation then? In your question you're indexing Lag with i, i goes from 1 to q and q is said to be 4000. That's in direct contradiction with your statement above.
How do these 11 different values figure in the equation?
%q = 11
for i=1:q
Theoritical(i,1) = Nug+Sill*(1-exp(-(Lag(i,1)/R)^alpha));
end
I made a mistake in my initial question. I apologize.
Per the equation, i consider each element of Lag in the computation. Omitting the q for now and assuming Lag is a constant, with variables being only alpha and R.
So, for a given R and alpha, you've 11 different theoritical (theoretical?) values. What is it you want to minimise?
I have a function (ExpSemivariance) that creates a vector of 11 elements. This elements are then represented on a graph in a scattered diagram and I intend finding the curve (Theoretical) that best fit 'ExpSemivariance' by using mimimum residual
Step = 10;
q = 11;
for i=1:q
Theoretical(i,1) = Nug+Sill*(1-exp(-(Lag(i,1)/R)^alpha));
end
Error = sum(abs(Theoretical-ExpSemivariance));
while Step > 1
R = R + Step;
for i=1:q
Theoretical(i,1) = Nug+Sill*(1-exp(-(Lag(i,1)/R)^alpha));
end
error = sum(abs(Theoretical-ExpSemivariance));
if error > Error
R = R - Step;
Step = Step/2;
else
Error = error;
end
end
The variables are R and alpha. Initially, i made alpha constant and used the code above to calculate min®. My problem is how to calculate both min® and min(alpha) for each i of the fun(Theoretical) such that Theoretical would be the best function that minimizes the error (between ExpSemivariance and Theoretical)

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by