Search code examples
matlabplotsurface

How to plot surface with constraints using ezsurf in MATLAB R2014b?


I want to plot the surface of function f=((x1+1.5)^2+5*(x2-1.7)^2)*((x1-1.4)^2+0.6*(x2-0.5)^2); with constraints:

-x1<0
-x2<0
3*x1-x1*x2+4*x2-7<0
2*x1+x2-3<0
3*x1-4*x2^2-4*x2<0

in MATLAB R2014b.

I only can plot f using ezsurf but I don't know to define the constraints. How to plot f with constraints?

syms x1 x2;
f=((x1+1.5)^2+5*(x2-1.7)^2)*((x1-1.4)^2+0.6*(x2-0.5)^2);
ezsurf(f);
hold on;

ADDENDUM: As @Ander Biguri's comment, I have tried make the code as follows:

x=0:0.1:2;
y=0:0.1:2;
[X1,X2]=meshgrid(x,y);
constraint1 = -X1<=0;
constraint2 = -X2<=0;
constraint3 = 3*X1-X1*X2+4*X2-7<=0;
constraint4 = 2*X1+X2-3<=0;
constraint5 = 3*X1-4*X2^2-4*X2<=0;
X1(~constraint1) = NaN;
X2(~constraint1) = NaN;
X1(~constraint2) = NaN;
X2(~constraint2) = NaN;
X1(~constraint3) = NaN;
X2(~constraint3) = NaN;
X1(~constraint4) = NaN;
X2(~constraint4) = NaN;
X1(~constraint5) = NaN;
X2(~constraint5) = NaN;
f=((X1+1.5).^2+5*(X2-1.7).^2)*((X1-1.4).^2+0.6*(X2-0.5).^2);
surf(X1,X2,f);

But nothing appear in the figure.

enter image description here

What is my mistake?


Solution

  • The problem in your second version (numerical) is a typical one: use elementwise operations instead of matrix operations:

    f=((X1+1.5).^2+5*(X2-1.7).^2).*((X1-1.4).^2+0.6*(X2-0.5).^2);
    

    enter image description here