I would like to plot my SOCP that looks something like this:
Link for the example
How can I do that?
I tried playing with this code:
[X,Y] = meshgrid(-2:0.1:2);
Z1 = sqrt(X.^2 + Y.^2);
Z2 = sqrt(X.^2 + 2*Y.^2);
Z3 = 2*X+Y;
figure(1)
surf(X,Y,Z1)
hold on
surf(X,Y,Z2)
hold on
surf(X,Y,Z3)
hold off
grid on
How can I plot constraint with norm? Also I would like to know if it is somehow with colors possible to show intersection of those constraints?
Related
I am trying to extend my polyfit line of best fit in Matlab. I want it to extend to y = 200 and x = 64. What would be the best way of doing this?
Cheers Tom
Link to image of polyfit through data
Plot your polyval over a different range. Something like that should work:
% Make up data
x = 1:100;
y = -0.03.*x.^2 + 2*x + 10*randn(size(x));
x2 = -50:150;
P = polyfit(x,y,2);
plot(x,y,'o',x2,polyval(P,x2),'--');
grid on;
I would like to overplot a comet3 plot over a surf plot, is this possible? hold on did not work.
It should work with the appropriate x,y and z values for the surface you are trying to display.
Simple example:
clc
clear
t = -2*pi:pi/100:2*pi;
x = (cos(2*t).^2).*sin(t);
y = (sin(2*t).^2).*cos(t);
%// Create x and y data for the surface plot
[xx,yy] = meshgrid(x,y);
zz = xx.^2 + yy.^2 - 7;
surf(xx,yy,zz)
hold on
comet3(x,y,t);
output:
Im new to matlab and I want to plot a ContourPlot together with two inequalities but I dont know how. The function that I want to plot its ContourPlot is something like this:
Z = (X-2).^2 + (Y-1).^2;
and here are the two inequalities:
ineq1 = X.^2 - Y <= 2;
ineq2 = X + Y <= 2;
this is what I have dodne so far:
[X,Y] = meshgrid(-4:.2:4,-4:.2:4);
Z = (X-2).^2 + (Y-1).^2;
[C,h] = contour(X,Y,Z);
clabel(C,h)
ineq1 = X.^2 - Y <= 2;
ineq2 = X + Y <= 2;
range = (-4:.2:4);
hold on
plot(range,ineq1, range, ineq2)
hold off
but this does not feel right.
What I want to do is to visualize an optimization problem. First I want to plot the ContourPlot of the function and then the possible areas in that same plot, It would be great if I could show the intersection areas too.
If you want to draw the boundaries of the inequalities onto the contour plot, you can do this with line.
[X,Y] = meshgrid(-4:.2:4,-4:.2:4);
Z = (X-2).^2 + (Y-1).^2;
[C,h] = contour(X,Y,Z);
clabel(C,h)
x_vect = (-4:.05:4);
y_ineq1 = x_vect.^2 - 2;
y_ineq2 = 2 - x_vect;
line(x_vect, y_ineq1);
line(x_vect, y_ineq2);
Coloring the intersection area is a bit more tricky, and I'm not sure if it's exactly what you want to do, but you could look into using patch, something like
% Indexes of x_vect, y_ineq1 and y_ineq2 for region where both satisfied:
region_indexes = find(y_ineq1<y_ineq2);
region_indexes_rev = region_indexes(end:-1:1);
% To plot this area need to enclose it
% (forward along y_ineq1 then back along y_ineq2)
patch([x_vect(region_indexes),x_vect(region_indexes_rev)],...
[y_ineq1(region_indexes),y_ineq2(region_indexes_rev)],...
[1 0.8 1]) % <- Color as [r g b]
% May or may not need following line depending on MATLAB version to set the yaxes
ylim([-4 4])
% Get the original plot over the top
hold on
[C,h] = contour(X,Y,Z);
clabel(C,h)
hold off
I've managed to plot on normal graph using the plot function in matlab. I then generated a symbolic function and I managed to plot that using ezplot. I would like to plot the two graphs into one set of x,y axis. I'm not sure how to do this... Here is the code:
a = [50; 100; 150;200;250;300;350];
b = [56;23;22;18;14;15;21];
plot(a,b);
y = polyfit(a,b,2);
syms x;
f = y(1)*x^2 + y(2)*x + y(3);
g = diff(f);
u = solve(g);
subplot(2, 2, 2);
ezplot(f);
Are you familiar with the hold command?
hold command
I have plotted a surface from some data. In the same plot I want to have a 3D line (I have the [x,y,z] values for the line I want to plot). When I try to do this using plot3(x,y,z) in the same figure, the line is always below the surface.
Is there any way to fix this? I don't know if this problem appears in Matlab as well.
Minimal example:
figure;
hold all;
y = x = 0:35;
z = ones(1,36).*0.5;
plot3(x,y,z);
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
surf(Z);
The result (the blue line is below the surface):
To answer part of your question, you don't get this problem in MATLAB with the following code:
figure;
hold all;
x = 0:35;
y = x;
z = ones(1,36).*0.5;
plot3(x,y,z);
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
surf(Z);
I also had this problem with surf in Octave, so I used the mesh function instead. It is not as pretty, and has different parameters but does allow lines to overlay it:
I created that with the same code as above but replacing surf with:
mesh ((X+8)*2.2, (Y+8)*2.2, Z);
Because mesh needed its parameters to be scaled up. The result is roughly the same.