using fminsearch in a for loop
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I have data:
q = [0.96 0.993 0.994 0.998 0.999];
n = 0.7448;
v = [0:100000];
I am trying to minimize a function where for each value of q the function is summed for all values of v (from 0 to 100000). I am using fminsearch but I think the way I have it written it is only minimizing the function for the last q value in the array. I have tried:
phatK0 = [1 100];
for j=1:length(q)
x{j} = @(phatK) sum((((q(j)).^(v.^n))-((q(j)).^((v+1).^n))).*(1-beta(abs(phatK(1)),abs(phatK(2))+v)./(beta(abs(phatK(1)),abs(phatK(2))))));
DevK = @(phatK) -2.*sum(log(x{j})-log(1-x{j}));
end
[phatK,devK] = fminsearch(DevK,phatK0,options);
phatK are the parameters I am trying to find when the function is minimized. How can I find the parameters when the function is minimized using the entire q array rather than just the last value?
채택된 답변
Star Strider
2020년 2월 28일
I am not certain what you want to do.
Try this:
q = [0.96 0.993 0.994 0.998 0.999];
n = 0.7448;
v = [0:100000];
phatK0 = [1 100];
for j=1:length(q)
x = @(phatK) sum((((q(j)).^(v.^n))-((q(j)).^((v+1).^n))).*(1-beta(abs(phatK(1)),abs(phatK(2))+v)./(beta(abs(phatK(1)),abs(phatK(2))))));
DevK = @(phatK) -2.*sum(log(x(phatK)-log(1-x(phatK))));
[phatK(j,:),devK(j,:)] = fminsearch(DevK,phatK0);
end
It does the optimisation in every iteration, and produces a matrix of ‘phatK’ and a vector of ‘devK’.
댓글 수: 10
Mary Jacketti
2020년 2월 28일
So I would like only one phatK matrix with both values of phatK. So I want to iterate the function to sum the v for each q. But then I want to optimize the entire function with all qs to get one phatK array with 2 values.
I completely fail to understand what ‘... sum the v for each q’ means here. I assume that is what the ‘x’ function does. Note that the ’q’ values are so close to each other that ’phatK’ converges to the same values in every iteration, so one value is essentially what you get:
phatK_mean = mean(phatK)
phatK_var = var(phatK)
produces:
phatK_mean =
1.811938955660990e+00 -1.030794884954796e-08
phatK_var =
0 0
Mary Jacketti
2020년 2월 28일
Say I have a simple function:

This function needs to be summed for each value of v= 0,1,2,3... for each q. The x will be an array that is the same size as the q array. I am trying to implement a more complicated version of this in the code where there are 2 unknown parameters for the entire function. I am trying to find the parameters for the entire x array, not just each value.
In some cases the q values will not be so close so I don't think taking the average is reasonable.
Star Strider
2020년 2월 28일
I am lost.
So ‘v’ is known, and ‘q’ and ‘n’ are not known? Is ‘x’ also known?
I might help to have the original equations (preferably in LaTeX form), and some description of the problem you want to solve.
Mary Jacketti
2020년 2월 28일
v is known and is a 1x100001 array, q is known and is a 1x5 array, n is known and = 0.7448. The phatK parameters are unknown so x is also unknown. I am trying to optimize x to find the phatK parameters. However, I am struggling to write the fminsearch function because q and v are different sizes and I need to sum the function over all v values for each q value. The way I have it written now only finds the parameter values that optimize x using only the last q value. I need the parameter phatK values that optimize x using all of the q values.
Star Strider
2020년 2월 28일
Can we please see the original equations (preferably in LaTeX format)?
Mary Jacketti
2020년 2월 28일
Sorry I don't have LaTeX, but this is the equation I am trying to optimize in matlab. I am summing from v=0 to v=100,000. Alpha and Beta are the unknown parameters.

Try this:
function x = fcn(phatK)
f = @(q,n,v,a,b) sum((q.^v.^n - q.^(v+1).^n) .* (1 - beta(a,b+v)./beta(a,b)));
qv = [0.96 0.993 0.994 0.998 0.999];
n = 0.7448;
v = 0:1E+8;
phatK = phatK(:);
for k = 1:numel(qv)
xv(:,k) = f(qv(k),n,v,phatK(1),phatK(2));
end
x = norm(xv)
end
phatK0 = [10 100];
[phatK,x] = fminsearch(@fcn, phatK0)
I believe it does what the equation you posted does (although I have no idea what that equation is or what you are doing). It takes forever because of the size of ‘v’, and occasionally throws this error:
Error using gammaln
Input must be nonnegative.
You will need to save the ‘fun’ function as ‘fun.m’ (or whatever you want to call it, however it is best if the function and the file it is saved to have the same names).
There may be better functions than fminsearch to optimise this. Since I have no idea what you are doing, I cannot determine that.
If this does not do what you want, post back to that effect and I will delete my Answer. I cannot help further.
Mary Jacketti
2020년 2월 28일
This works! Thank you!!
Star Strider
2020년 2월 28일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
