How do i use fminsearch to find the minimum or maximum of a function . x.^4-3.*x.*y+2.*y.^2

조회 수: 39 (최근 30일)
I trying to use fmin search for a function of 2 variable
  댓글 수: 2
Ibraheem
Ibraheem 2022년 10월 13일
fun = @(x) x.^4-3.*x.*y +2.*y.^2;
x1 =x;
x2 =y;
x0 = [-0.5,0.5];
x = fminsearch(fun,x0)
I don't really know what to do to find the local minimum

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

채택된 답변

James Tursa
James Tursa 2022년 10월 13일
편집: James Tursa 2022년 10월 13일
You need to have your function handle accept a vector and return a scalar. I.e., the x argument to the function handle is a vector of two elements representing your original x and y variables. Assuming x(1) and x(2) are your intended original x and y variables, that would mean something like this:
fun = @(x) x(1).^4-3.*x(1).*x(2) +2.*x(2).^2;
x0 = [-0.5,0.5];
x = fminsearch(fun,x0)
x = 1×2
-0.7500 -0.5625
  댓글 수: 3
James Tursa
James Tursa 2022년 10월 13일
편집: James Tursa 2022년 10월 13일
format longg
fun = @(x) x(1).^4-3.*x(1).*x(2) +2.*x(2).^2;
x0 = [-0.5,0.5];
x = fminsearch(fun,x0)
x = 1×2
-0.749979058202537 -0.562478134566371
fun(x)
ans =
-0.316406248937402
fun([3/4,9/16])
ans =
-0.31640625
Also note that fminsearch( ) can only find local minimums, of which there can be more than one depending on the function. So different starting points can result in different answers.
x = fminsearch(fun,[3/4+0.1,9/16-0.1])
x = 1×2
0.749992146496559 0.562470511151394
fun(x)
ans =
-0.316406248747428
the cyclist
the cyclist 2022년 10월 13일
The function is invariant in the transformation (x,y) --> (-x,-y)

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

추가 답변 (1개)

the cyclist
the cyclist 2022년 10월 13일
편집: the cyclist 2022년 10월 13일
Did you try reading the documentation for fminsearch? The very first example is exactly like your problem.
fun = @(x)(x(1)^4 - 3*x(1)*x(2) + 2*x(2)^2);
x0 = [-0.5, 0.5];
x = fminsearch(fun,x0)
x = 1×2
-0.7500 -0.5625

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by