Error in T12 (line 35) surf(X, Y, fplot) and I have no idea what I was wrong

1 view (last 30 days)
Ngo Ha Gia Bao
Ngo Ha Gia Bao on 17 May 2022
Answered: KSSV on 17 May 2022
clc
clear
close all
syms x y
f = @(x,y)(x.^2+y.^2+x.*y-12*log(x)-10*log(y));
fx = diff(f,x);
fy = diff(f,y);
[xc,yc] = solve(fx,fy,x,y,'real',true);
[m,n] = size([xc,yc]);
fxx = diff(fx,x);
fyy = diff(fy,y);
fxy = diff(fx,y);
D = fxx*fyy - fxy^2;
%print
for i = 1:m
if subs(D,{x,y},{xc(i),yc(i)}) > 0
if subs(fxx,{x,y},{xc(i),yc(i)}) > 0
fprintf('(%f,%f,%f) is local minimum\n',xc(i),yc(i),f(xc(i),yc(i)));
else
fprintf('(%f,%f,%f) is local maximum\n',xc(i),yc(i),f(xc(i),yc(i)));
end
elseif subs(D,{x,y},{xc(1),yc(1)}) < 0
fprintf('(%f,%f,%f) is saddle point\n',xc(i),yc(i),f(xc(i),yc(i)));
else
fprintf('(%f,%f,%f) is unknown\n',xc(i),yc(i),f(xc(i),yc(i)));
end
end
%Sketch
xx = linspace(-1.2, 1.2, 100);
yy = linspace(-1.2, 1.2, 100);
[X,Y] = meshgrid(xx, yy);
fplot = X^2+Y^2-12*log(X)-10*log(Y)+X*Y;
figure
surf(X, Y, fplot)
hold on
for i = 1:m
plot3(xc(i),yc(i),f(xc(i),yc(i)),'r*')
end

Accepted Answer

KSSV
KSSV on 17 May 2022
clc
clear
close all
syms x y
f = @(x,y)(x.^2+y.^2+x.*y-12*log(x)-10*log(y));
fx = diff(f,x);
fy = diff(f,y);
[xc,yc] = solve(fx,fy,x,y,'real',true);
[m,n] = size([xc,yc]);
fxx = diff(fx,x);
fyy = diff(fy,y);
fxy = diff(fx,y);
D = fxx*fyy - fxy^2;
%print
for i = 1:m
if subs(D,{x,y},{xc(i),yc(i)}) > 0
if subs(fxx,{x,y},{xc(i),yc(i)}) > 0
fprintf('(%f,%f,%f) is local minimum\n',xc(i),yc(i),f(xc(i),yc(i)));
else
fprintf('(%f,%f,%f) is local maximum\n',xc(i),yc(i),f(xc(i),yc(i)));
end
elseif subs(D,{x,y},{xc(1),yc(1)}) < 0
fprintf('(%f,%f,%f) is saddle point\n',xc(i),yc(i),f(xc(i),yc(i)));
else
fprintf('(%f,%f,%f) is unknown\n',xc(i),yc(i),f(xc(i),yc(i)));
end
end
(-2.044027,-1.782708,-3.360401) is local minimum (3.389486,-3.238614,-15.399595) is local minimum (2.044027,1.782708,-3.360401) is local minimum (-3.389486,3.238614,-15.399595) is local minimum
%Sketch
xx = linspace(-1.2, 1.2, 100);
yy = linspace(-1.2, 1.2, 100);
[X,Y] = meshgrid(xx, yy);
Z = X.^2+Y.^2-12*log(X)-10*log(Y)+X.*Y; %<---- element by element operations
figure
surf(X, Y, abs(Z))
hold on
for i = 1:m
plot3(xc(i),yc(i),f(xc(i),yc(i)),'r*')
end
Warning: Imaginary parts of complex X, Y, and/or Z arguments ignored
Warning: Imaginary parts of complex X, Y, and/or Z arguments ignored
Warning: Imaginary parts of complex X, Y, and/or Z arguments ignored

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by