필터 지우기
필터 지우기

Using fminsearch in a loop

조회 수: 3 (최근 30일)
Amandeep
Amandeep 2011년 9월 25일
Hi,
What I want to do is find the minimum value of the variable, penalty, below using fminsearch. I am trying to get one value of penalty for each simulation (each row of R), instead of doing it manually by repeatedly inserting the maximum value of opportunity, as the new penalty value.
R is a 2 by 60 matrix made up of rates, that represents 1 simulation per row with the initial value the same for each simulation.
R = R./100;
initialfixed = R(1,1);
PVfixedinitial = zeros(1,1);
PVfixednew = zeros(1,1);
opportunity = zeros(1,1);
principle = 1;
for i=1:size(R,1)
x = fminsearch(Difference,0.3);
end
for i=1:size(R,1)
for j=1:size(R,2)
PVfixedinitial(i,j) = (initialfixed*principle)/R(i,j);
PVfixednew(i,j) = (R(i,j)*principle)/R(i,j);
opportunity(i,j) = PVfixedinitial(i,j)-PVfixednew(i,j);
Difference(i,j) = @(penalty) opportunity(i,j) - penalty;
if Difference(i,j)>0
initialfixed = Rmatrix(i,j);
end
end
initialfixed = Rmatrix(1,1);
end
Any help will be much appreciated. Thanks.

채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 25일
Your first "for" loop overwrites "x" on each iteration, which isn't going to do you much good.
Your line,
Difference(i,j) = @(penalty) opportunity(i,j) - penalty;
attempts to set Difference(i,j) to a function handle, not to a numeric result. If my memory is correct, then you cannot assign a function handle as an array element in any version of MATLAB that supported anonymous functions. (It is legal to store function handles as cell array elments, though -- but that would require assigning to Difference{i,j} )
You do not show any initialization for Difference, and changing Difference after your run the fminsearch() is not going to change the result of the optimization.
fminsearch() must be passed a single function handle, not an array of handles. It is an error to attempt to assign a function handle array element in modern versions of MATLAB.
If Difference(i,j) did somehow get assigned a function handle, then in the next line where you attempt to check if Difference(i,j)>0, that would be a call to the function handle at Difference(1,1) with argument (i,j), rather than a retrieval of the function handles. And function handles cannot be compared to numeric values if you were able to retrieve the handle instead of calling the function.
Lastly... I do not seem to understand what you are asking to do? Perhaps you could restate it? fminsearch() cannot process multiple searches at the same time, if that is what you are implicitly asking.
  댓글 수: 1
Amandeep
Amandeep 2011년 9월 26일
Thanks for the reply. I have figured out I don't actually need to use fminsearch in the first place but again thanks for the reply.
What I wanted to do was minimize the value penalty so that difference can never be positive. But looking at the above code again I realise this setup is wrong!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by