Matlab : how to represent a function submitted to a constraint in 2 variables with absolute values

51 Views Asked by At

I would like to plot a function on Matlab

$|x_1+2x_2|+|x_1|$ constraint to $|x_{1}| + |x_{2}| = 1$

I was searching but it has constraint with absolute and with two variables. Can anyone help me?

1

There are 1 best solutions below

6
On

enter image description here

Given by the Matlab program below :

    close all;hold on;box on
    [X1,X2]=meshgrid(-1.5:0.01:1.5);
    Z=abs(X1+2*X2)+abs(X1);
    surf(X1,X2,Z,'edgecolor','none');view([170,60]);
    %
    h=3;z=[0,0,h,h];
    fill3([1,0,0,1],[0,1,1,0],z,'g');
    fill3([0,-1,-1,0],[1,0,0,1],z,'r');
    fill3([-1,0,0,-1],[0,-1,-1,0],z,'b');
    fill3([1,0,0,1],[0,-1,-1,0],z,'y');

which has 2 parts,

  • one for the first equation as a surface plot of $z=f(x_1,x_2)$ looking like an inverted roof, and

  • one for the representation of the second (superimposed) constraint as another surface that can be considered as a "chimney" intersecting it (the underlying 2D curve with equation $|x_1|+|x_2|=1$ is the square with vertices $(1,0), \ (0,1), \ (-1,0), \ (0,-1)$).