Plot region of the inequalities using matlab [duplicate] - matlab

This question already has answers here:
How to plot inequalities
(3 answers)
Closed 8 years ago.
I have a question about matlab. It seems simple but I can't find the solution for inequalities linear region plot in matlab. For example, I want to plot the regions of y-x, and y>x and show the color for each one. For any x, y, but we can assume x = [-50:50].
Thank you.
I tried this one but don't know how to show the color for the third parameter.
[X,Y]=meshgrid(-1:0.01:1,-1:0.01:1);
ineq1 = Y<X;
ineq2 = Y>X;
ineq3 = Y>-X;
colors = zeros(size(X))+ineq1+ineq2+ineq3;
scatter(X(:),Y(:),3,colors(:),'filled')

[X,Y]=meshgrid(-1:0.01:1,-1:0.01:1);
x1=reshape(X,[size(X,1)*size(X,2) 1]);
y1=reshape(Y,[length(x1) 1]);
a=[x1 y1];
ineq1=a(a(:,1)>a(:,2),:);
ineq2=a(a(:,1)<a(:,2),:);
ineq3=a(-a(:,1)<a(:,2),:);
scatter(ineq1(:,1),ineq1(:,2),3,'b','filled');
hold on;
scatter(ineq2(:,1),ineq2(:,2),3,'r','filled');
scatter(ineq3(:,1),ineq3(:,2),3,'g','filled');

Related

Unexpected output of plot [duplicate]

This question already has answers here:
What is the advantage of linspace over the colon ":" operator?
(3 answers)
Closed 4 years ago.
I am wanting to do a very simple plot of a polynomial using the plot(x,y) function. Here is my full code
a = linspace(-3, 0.1, 3);
plot(a, a.^3 - 3*a - 2);
ax = gca;
c = ax.Color;
grid on;
Which outputs the following picture
Why doesn't the graph extend from -3 to 3 on the x-axis? Why does it stop just after $0$?
As the documentation states, in linspace(x1,x2,n) x1 is the begin value, x2 is the end value and n is the number of points. That's exacly what you see on your plot: 3 points: -3, 0.1 and one halfway (because of the linear spacing).
Since you want to have a particular spacing between your points, and not a certain number of points, I suggest you build your vector as:
a = -3:.1:3;

Matlab ploting an arrow between two points (not just a line) [duplicate]

This question already has answers here:
How do I display an arrow positioned at a specific angle in MATLAB?
(3 answers)
How to add arrows to line plots in Matlab?
(5 answers)
How to draw an arrow in Matlab?
(5 answers)
Closed 5 years ago.
Following up on this lovely answer https://stackoverflow.com/a/45442483/2798895 how to draw a line between two sets of points I need to have an arrow (--->) between the corresponding points. Is there a way of doint it?
I found some examples of using quiver but they don't connect from (x,y) to (u,v). They are adding only a short arrow to (x,y) that is pointing in the direction of (u,v) but not touching it.
My line plot (from the above mentioned answer):
set1 = [1,2; 3,4; 5,6];
set2 = [10,20; 30,40; 50,60];
figure;
plot(set1(:,1),set1(:,2),'b+',set2(:,1),set2(:,2),'g+')
hold on
x = [set1(:,1) set2(:,1)].';
y = [set1(:,2) set2(:,2)].';
plot(x,y,'r')
hold off
drawnow
My quiver plot:
set1 = [1,2; 3,4; 5,6];
set2 = [10,20; 30,40; 50,60];
figure;
plot(set1(:,1),set1(:,2),'b+',set2(:,1),set2(:,2),'g+')
hold on
quiver(set1(:,1),set1(:,2),set2(:,1),set2(:,2));
hold off
drawnow
How would that be possible to connect the points with an arrow e.g. like quiver but with longer arrows?

I want to draw ellipse in matlab. I have ellipse parameters [duplicate]

This question already has an answer here:
Draw ellipse in MATLAB
(1 answer)
Closed 6 years ago.
Any ellipse can be uniquely defined by five parameters i.e. center x0 and y0, semi-major aixs length a, semi minor axis length b, and orientation angle theta. I have parameters x0, y0, a, b, and theta. How can I exactly draw the ellipse?
Some researches are necessary before to ask this kind of question. Mainly if the question was asked too much time.
You can do something like this :
Let (x1,y1) and (x2,y2) be the coordinates of the two vertices of the ellipse's major axis, and let e be its eccentricity.
a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
b = a*sqrt(1-e^2);
t = linspace(0,2*pi);
X = a*cos(t);
Y = b*sin(t);
w = atan2(y2-y1,x2-x1);
x = (x1+x2)/2 + X*cos(w) - Y*sin(w);
y = (y1+y2)/2 + X*sin(w) + Y*cos(w):
plot(x,y,'y-')
axis equal
I don't have time to test it, but it should work.
Next time, please read this section : How do I ask a good question

MATLAB: scatter plot from where each point has its own color [duplicate]

This question already has answers here:
3D scatter plot with 4D data
(2 answers)
Closed 6 years ago.
Suppose I have a situation as follows:
clc; clear;
n = 1001;
m = 1000;
X = linspace(0,1,n);
Y = linspace(0,1,n);
randcolor = rand(m,3);
colorcode = randi(m,m,m);
For i = 1, ..., n and j = 1, ...,n, I would like to plot the points (X(i),Y(j))'s where the RBG color for (X(i),Y(j)) is randcolor(colorcode(i,j),:). I tried to do this the silly way: first declare
figure; hold on;
then do 2 nested loops, n steps each, and use plot to plot a single point n x n times:
for i = 1:n
for j = 1:n
plot(X(i),Y(j),'Marker','o',...
'MarkerEdgeColor',randcolor(colorcode(i,j),:),...
'MarkerFaceColor',randcolor(colorcode(i,j),:));
end
end
This technically worked but it was slow and MATLAB ate up all of my memory when n was increased. What's a better way to do this please?
p.s. In my actual problem, colorcode isn't actually randomly assigned. Rather, it's assigned based on some divergence criterion for a filled Julia set.
You want to use scatter instead of plot which allows you to specify the size and color of each point individually.
colors = rand(numel(X), 3);
S = scatter(X, Y, 100, colors);

Matlab - Shade the surface between 2 vertical curves [duplicate]

This question already has answers here:
MATLAB, Filling in the area between two sets of data, lines in one figure
(4 answers)
Closed 8 years ago.
I'm trying (without success so far) to colorize or to shade the surface between two curves but that are in the vertical direction.
More specifically, I'd like to shade the surface between the red and the blue curves, knowing that they sometimes cross each others.
The final purpose of this is to show the domain of incertitude (the nominal curve is the black one), represented by the red and the blue curves.
I don't know if that can help, I have for each curve a vector of 100 values.
Do you have any idea on how to do this ?
Thanks a lot,
Best regards,
Antoine
Edit :
Thanks for the quick answer !
I tried with your method but it doesn't seem to work unfortunately..
Here's my code :
filename = '/Users/Antoine/Dropbox/TFE/Post-processing/100planes_Z_HT/case1/HT_tip_int.dat';
delimiterIn = ' ';
case1_int = importdata(filename,delimiterIn);
filename = '/Users/Antoine/Dropbox/TFE/Post-processing/100planes_Z_HT/case4/HT_tip_int.dat';
delimiterIn = ' ';
case4_int = importdata(filename,delimiterIn);
filename = '/Users/Antoine/Dropbox/TFE/Post-processing/100planes_Z_HT/case5/HT_tip_int.dat';
delimiterIn = ' ';
case5_int = importdata(filename,delimiterIn);
figure
HT_tip_int1=plot(case1_int.data,Z2,'k-','Linewidth',1);
hold on;
HT_tip_int4=plot(case4_int.data,Z2,'r');
HT_tip_int5=plot(case5_int.data,Z2,'c');
area( [case4_int.data fliplr(case5_int.data)], [Z2 fliplr(Z2)],'FaceColor','red'); hold off
It is the right way to do it ?
Thanks for your help !
Antoine
How about using area the following way:
Take the first dataset and it's argument vector, and concatenate it with the second dataset backwards. That way you get a closed polygon.
% example data
t = 1:100;
x1 = sin(pi*t/10).*t
x2 = 0.1*sin(pi*t/10).*t*2 + 25
plot(x1,t, x2, t,'linewidth',5); hold on
area( [x1 x2(end:-1:1)], [t t(end:-1:1)],'FaceColor','red'); hold off
% or
% area( [x1 fliplr(x2)], [t fliplr(t)],'FaceColor','red'); hold off
gives: