필터 지우기
필터 지우기

Minimizing a multivariable function

조회 수: 5 (최근 30일)
Physiker192
Physiker192 2014년 8월 24일
댓글: Star Strider 2014년 8월 24일
Hello, i am trying to find the minimum of a multivariable function (the number of variables may vary also but there is a pattern to construct the function) the code i am running is the following:
function F = jac(x)
n=input('n=');
x0=random('unid',90,1,n,1);
x0=pi./x0;
E=input('E=');
h=3;
F=0;
for i=1:n
D= 0.5;
% V=0;
% b=0;
for j=1:n
if (mod(j,2)==0)
D = D + cos(h*x(j));
else
D = D - cos(h*x(j));
end
end
D=(2*E*sqrt(2)/(pi*h))*abs(D);
F = F + 1/h*(D^2);
h=h+2;
end
M=fminsearch(@(x)jac,x0);
the error i am getting is that the function x is undefined can anyone help me please? thanks in advance
  댓글 수: 2
John D'Errico
John D'Errico 2014년 8월 24일
Please learn to format your code to make it readable. I did it for you this time. (Look for the code button.)
John D'Errico
John D'Errico 2014년 8월 24일
편집: John D'Errico 2014년 8월 24일
Now that I can read your code, I see the call to random. random functions are quite difficult to minimize. However, it looks like you think that is a call to create starting values.
Worse, you have several input statements in the beginning of the function you are trying to minimize? WHY????
My guess from all these things (and others in your code) is you don't have a clue how an optimizer works or how to call one. Or maybe you don't understand how to write functions. It looks like you are confused about what the objective function called by an optimizer should be doing.
Time to start reading the help. Look at the examples. Then and only then, try solving a SMALL optimization problem where you know the answer. Work up to real world problems only when you understand how to solve basic ones.

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

채택된 답변

Star Strider
Star Strider 2014년 8월 24일
편집: Star Strider 2014년 8월 24일
You are going to encounter the recursion error when you run your code because you seem to be calling fminsearch inside your function.
I didn’t run your code however certain parts of it need to be corrected.
Define your ‘n’ and ‘E’ variables outside the function in your workspace and pass them as arguments. Redefine your function statement to accommodate them.
For example, the function statement becomes:
function F = jac(x,n,E)
then eliminate the input statements inside ‘jac’. Your script then becomes:
x0=random('unid',90,1,n,1);
x0=pi./x0;
n = ... % <— Define ‘n’
E = ... % <— Define ‘E’
M=fminsearch(@(x)jac(x,n,E),x0);
Your function becomes:
function F = jac(x,n,E)
h=3;
F=0;
for i=1:n
D= 0.5;
% V=0;
% b=0;
for j=1:n
if (mod(j,2)==0)
D = D + cos(h*x(j));
else
D = D - cos(h*x(j));
end
end
D=(2*E*sqrt(2)/(pi*h))*abs(D);
F = F + 1/h*(D^2);
h=h+2;
end
end
These changes should get you started. Again, I didn’t run this because it’s not obvious to me what you want to do.
  댓글 수: 3
Physiker192
Physiker192 2014년 8월 24일
thank you very much it works. I believe that my mistake was not defining f as a function of (x,n,E) the last code that i posted was an attempt to deal with it as a single file so it wouldn't ask me repetevely to enter E. Anyway thanks a lot.
Star Strider
Star Strider 2014년 8월 24일
My pleasure!

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

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by