How to minimize a nonlinear function?

조회 수: 31 (최근 30일)
Rafael Zanetti
Rafael Zanetti 2020년 11월 12일
답변: Star Strider 2020년 11월 12일
Hi everyone, I heve tried to solve a nonlinear equation in matlab where x0 = [0;1;2] is the initial value for search
So I wrote a function :
function f = myfun(x)
f = (1./(1+(x1-x2).^2)) + sin.*(0.5*pi.*x2.*x3) + exp(((-x1+x)./x2)-2).^2)
end
Then:
x1 = 0; x2 = 1; x3 = 2; x0 = [x1;x2;x3];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@myfun,x0,options)
... But the following error appears:
"Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue."
How can i fix that.
I thank you.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 12일
편집: Ameer Hamza 2020년 11월 12일
There are issues with the definition of 'myfun'. Compare it with the following to see the errors.
function f = myfun(x)
x1 = x(1);
x2 = x(2);
x3 = x(3);
f = (1./(1+(x1-x2).^2)) + sin(0.5*pi.*x2.*x3) + exp(((-x1+x)./x2)-2).^2;
end

추가 답변 (1개)

Star Strider
Star Strider 2020년 11월 12일
It took a bit to correct the parentheses problems in ‘myfun’.
With that solved:
myfun = @(x1,x2,x3) (1./(1+(x1-x2).^2)) + sin(0.5*pi.*x2.*x3) + exp((-(x1+x3)./x2-2).^2);
options = optimoptions('fsolve','Display','iter')
x1 = 0; x2 = 1; x3 = 2; x0 = [x1;x2;x3];
[x,fval] = fsolve(@(p)myfun(p(1),p(2),p(3)),x0,options)
produces:
x =
-0.823049966296178
1.775543504609500
1.177574125107562
fval =
1.262730886088113e+02
.
.

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by