Plot closed convex set in 2D
Show older comments
Hello,
this seems like a pretty basic question, but I have not found an answer yet.
I want to plot a set S in 2D, such that
S = { (x1,x2): f(x1, x2) <= 0 }
where f is a given convex function, so that S will be convex. S will also be closed.
Say, x1 and x2 both should go from say -10 to 10 in increments of 0.1
I can easily write a code to evaluate f, for a given x1 and x2, but f is complicated enough, so it is not easy to ``vectorize" it. It is more complicated, than say x1^2 + x2^2-1 (which would give a closed disk.
Similar question is posted for
but that is not quite what I want.
A first approximation is this code, but it is slow, and the plot it generates looks jagged.
lb1=-5; %lower bound of x1
ub1=5; %upper bound of x1
N1=(ub1-lb1)/0.1;
lb2=-5; %lower bound of x2
ub2=5; %upper bound of x2
N2=(ub2-lb2)/0.1;
syms x1 x2
f(x1, x2)=x1^2+x2^2-1;
A=[]; % x1 of points in feasible set
B=[]; % x2 of points in feasible set
for i=0:N1
a=lb1+i*0.1;
for j=0:N2
b=lb2+j*0.1;
if f(a,b)<=0
A=[A,a];
B=[B,b];
end
end
end
scatter(A,B,'filled');
-----------------------------------------------------
Thank you
Answers (1)
If f is only one function and not a vector of functions, use "fimplicit".
fimplicit(@(x,y) x.^2+y.^2-1);
4 Comments
gabor pataki
on 8 Jul 2022
Doesn't matter. The f doesn't need to be given as a "one-liner". You can define your f in a separate function file.
Then just do
fimplicit(@fun)
function value = fun(x,y)
value = x.^2+y.^2-1;
end
gabor pataki
on 8 Jul 2022
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
