Niko24
Niko24

Reputation: 81

Contour plot in MATLAB with constraints

I'm completely new to MATLAB and have some problems.

I need to make a contour plot of the following nonlinear optimization problem:

enter image description here

In order to determine the feasible region for the function. I've tried searching on Mathworks, but haven't had any luck so far. I also have the following example:

x = -5:0.005:5;
y = -5:0.005:5;
[X,Y] = meshgrid(x,y);
F = (X.^2+Y-11).^2 + (X + Y.^2 - 7).^2;
v = [0:2:10 10:10:100 100:20:200]
[c,h]=contour(X,Y,F,v,'linewidth',2);

colorbar
yc1 = (x+2).^2;
yc2 = (4*x)/10;

hold on
fill(x,yc1,[0.7 0.7 0.7],'facealpha',0.2)
fill([x x(end) x(1)],[yc2 -5 -5],[0.7 0.7 0.7],'facealpha',0.2)
hold off

But this is not shown the way it should either (it is suppose to look like this):

enter image description here

Upvotes: 4

Views: 5043

Answers (2)

Wolfie
Wolfie

Reputation: 30175

Your code works, you just need to set the limits

% your code here

% Set limits
ylim([-5,5])
% Set colours
colormap 'jet'

contour plot

Upvotes: 2

Sardar Usama
Sardar Usama

Reputation: 19689

You're already there. Only limits of y-axis need adjustment. Add the following line:

ylim([-5 5])

Upvotes: 1

Related Questions