3

I searched and watched how to plot the 3 dimensions of nonlp program's

but I still don't know how to plot these constraints.

x^2+y^2+z^2=1

x>=2*y
2*y>=3*z
x>=3*z

I programmed like this but it doesn't work and I think this is wrong program for upper constraints.

func1 = @(x,y,z) sqrt(1-x.^2-y.^2);
func2 = @(x,y,z) max(x-2*y,0);
func3 = @(x,z) max(x-3*z,0);
ezsurf(func1,[0 1 0 1]);
hold on;
ezsurf(func2,[0 1 0 1]);
hold on;
ezsurf(func3,[0 1 0 1]);
axis([0 1 0 1 0 1]);
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
김종현
  • 33
  • 4

1 Answers1

4

A way of doing that is to work with volumetric data.

The idea is to create a 3D space, with a F value. F will be positive in one side of your main equation and negative in the other.

[X,Y,Z]=meshgrid(-2:0.05:2,-2:0.05:2,-2:0.05:2);

F=X.^2+Y.^2+Z.^2-1;

Then the conditions are applied to this F. If you want to see the object "cut" then substitute the place where conditions are not met with nan. If you want to see a filled object, then substitute it with a positive number (it may need to be negative in another situation, so check this for other examples.

% Conditions
cond1=X>=2*Y;
cond2=2*Y>=3*Z;
cond3=X>=3*Z;

% If you want the boundaries to show
F1=F;
F1(~cond1)=1;
F1(~cond2)=1;
F1(~cond3)=1;

% If you want the boundaries not to show
F2=F;
F2(~cond1)=NaN;
F2(~cond2)=NaN;
F2(~cond3)=NaN;

Then the idea is to create an isosurface in the zero level (your original function). Here in this code I created 2 plots so you can see the differences between filling and not filling the boundaries. Also some fancy coding in order to get nice figures. However the isosurface and patch parts are relevant to obtain the surface and plot it.

subplot(121)
iso1=isosurface(X,Y,Z,F1,0);
p=patch(iso1);
isonormals(X,Y,Z,F1,p);
set(p,'FaceColor','red','EdgeColor','none');
daspect([1 1 1])
axis equal

camlight
lighting gouraud


subplot(122)
iso2=isosurface(X,Y,Z,F2,0);

p=patch(iso2);
isonormals(X,Y,Z,F2,p);
set(p,'FaceColor','red','EdgeColor','none');
axis equal

daspect([1 1 1])
camlight headlight
lighting gouraud

Result:

enter image description here

Additionally, You can calculate the isosurface of the initial boundary F and plot it with a low alpha value, so you can better understand what piece of the domain you are actually selecting.

enter image description here

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • 1
    @rayryeng Thanks :D If there is something I most love about Matlab is the plotting possibilities, and this is just for pre-2014b! – Ander Biguri Mar 18 '15 at 15:31
  • 2
    Nice :) I'm still using R2013a because this is right at the tail end when things change significantly if you go to the next version after this and I want to provide solutions that generally work for everybody. If I migrated to R2014+, I would rely on a lot of new functions to help me solve questions when many may not have access to those functions. Basically, if I try and do `help ` and if I don't have it on my version of MATLAB, I won't use it for my answer and that probably means that a lot of other people won't have it either and so I won't use that in my solution. – rayryeng Mar 18 '15 at 15:33
  • 1
    @rayryeng Makes a lot of sense. As currently I am in academia, where if the code is legible means that you spent too much time on it (never happens) , I just use whatever the heck I want XD – Ander Biguri Mar 18 '15 at 15:36
  • Thanks for your help. I solved with your answer. Thanks again! – 김종현 Mar 25 '15 at 13:34
  • @김종현 No problem! Do not forget to accept the answer if it helped! – Ander Biguri Mar 25 '15 at 13:59
  • @AnderBiguri If I want to plot something like this? $\Omega = $ – roni Sep 09 '16 at 13:11
  • @roni I am quite sure " $\Omega = $" is not an equation that can be plotted – Ander Biguri Sep 09 '16 at 13:56
  • @AnderBiguri Sorry about that. The complete problem which I want to plot is this. f(x)=x2 constraint set is O = {x : x1^2+x2^2>=1}. What is the best way to show such a plot ? – roni Sep 09 '16 at 17:38