필터 지우기
필터 지우기

Writing functions f(x,y)

조회 수: 45 (최근 30일)
Kaleina
Kaleina 2023년 9월 4일
댓글: Alexander 2023년 9월 6일
How do I correctly write f(x,y)=4(x-1)^2+3(y-2)^2+2(x-2)^2(y-3)^2 in MatLab code for a fiminsearch? I have been trying for days and I still cant get it right. Please help!!!

답변 (4개)

Torsten
Torsten 2023년 9월 5일
편집: Torsten 2023년 9월 5일
f = @(x,y)4*(x-1)^2+3*(y-2)^2+2*(x-2)^2*(y-3)^2;
z0 = [1 1]
z0 = 1×2
1 1
sol = fminsearch(@(z)f(z(1),z(2)),z0)
sol = 1×2
1.1963 2.3010
f(sol(1),sol(2))
ans = 1.0571
  댓글 수: 1
Kaleina
Kaleina 2023년 9월 5일
Thanks so much.

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


Stephen23
Stephen23 2023년 9월 5일
편집: Stephen23 2023년 9월 5일
fh1 = @(x,y) 4*(x-1).^2 + 3*(y-2).^2 + 2*(x-2).^2.*(y-3).^2;
fh2 = @(v) fh1(v(1),v(2)); % function with one input
sol = fminsearch(fh2,[1,1])
sol = 1×2
1.1963 2.3010
val = fh2(sol)
val = 1.0571
An important part of any calculation is checking the result. Lets do that now:
fsurf(fh1)
hold on
plot3(sol(1),sol(2),val,'*r')
view(54,31)
  댓글 수: 1
Kaleina
Kaleina 2023년 9월 5일
Thank you for the insight.

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


Sam Chak
Sam Chak 2023년 9월 5일
For a static function, especially a function of two variables, this is how I systematically find the minimum point.
% STEP 1: Find the approximate location of the minimum point via contour plot
[X, Y] = meshgrid(-1:0.1:5); % adjust these until you see the basin
Z = staticfun(X, Y); % scroll to the bottom of the script
figure(1)
contour(X, Y, Z, 30), xline(1.5, '--'), yline(2.5, '--')
xlabel('x'), ylabel('y'), colorbar
% STEP 2: Find minimum of unconstrained function f(x, y)
funhd = @(v) staticfun(v(1), v(2)); % create a function handle to pass it to fminsearch
v0 = [1.5, 2.5]; % initial guess values based on the contour plot
solution = fminsearch(funhd, v0) % applying fminsearch
solution = 1×2
1.1963 2.3010
% STEP 3a: Plot the surface and the minimum point
figure(2)
surf(X, Y, Z), hold on
minValue = funhd(solution) % insert xsol & ysol into static funtion to find the min value
minValue = 1.0571
plot3(solution(1), solution(2), minValue, 'r.', 'MarkerSize', 25) % plot the red dot
% STEP 3b: rotate for better viewing
[az, el] = view;
az = az + 180;
view(az, el)
xlabel('x'), ylabel('y'), zlabel('f(x,y)')
% Part of STEP 1: to create a function for the surface f(x, y).
function fun = staticfun(x, y)
fun = 4*(x - 1).^2 + 3*(y - 2).^2 + 2*(x - 2).^2.*(y - 3).^2;
end
  댓글 수: 1
Kaleina
Kaleina 2023년 9월 5일
Thats very detailed thank you.

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


Alexander
Alexander 2023년 9월 6일
편집: Alexander 2023년 9월 6일
The solutions above are very good and should be used to avoid loops. But anyway I post my little script here, because I think it is good for the understanding how a multible dimension function is built up.
X = 0:0.1:4; % Taylor it to your needs (precision, boundary)
Y = X; % Y = -1:0.1:3;
for(ix = 1:length(X));
for(iy = 1:length(Y))
Z(ix,iy) = 4*(X(ix) - 1).^2 + 3*(Y(iy) - 2).^2 + 2*(X(ix) - 2).^2.*(Y(iy) - 3).^2;
end
end
figure(1); contour(X,Y,Z);xlabel('X');ylabel('Y');
figure(2); mesh(X,Y,Z);xlabel('X');ylabel('Y');ylabel('Z');
[C, ind] = min(Z); [zMin, xInd] = min(C);
[C, ind] = min(Z'); [zMin, yInd] = min(C);
fprintf('Minimum of Z: %3.4f at pos. [X,Y] = [%i, %i], value = [%3.4f, %3.4f]\n',zMin,xInd,yInd,X(xInd),Y(yInd))
  댓글 수: 2
Stephen23
Stephen23 2023년 9월 6일
편집: Stephen23 2023년 9월 6일
Also without nested FOR loops:
[X,Y] = meshgrid(-1:0.1:5);
Z = X;
Z(:) = 4*(X(:) - 1).^2 + 3*(Y(:) - 2).^2 + 2*(X(:) - 2).^2.*(Y(:) - 3).^2;
contour(X,Y,Z, 30)
Alexander
Alexander 2023년 9월 6일
Great!

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

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by