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

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:

Related

Interpolation between curves that may overlap

I try to interpolate curves in the gaps of some already existing curves using matlab (and the interp1-function).
Start of edit 1
I already have the data of 5 Torque over rpm curves that I obtained with a number of simulations for each curve. Since simulation time is precious, I would like to save time with the interpolation of curves that "fill the gap" between the already existing ones.
I'm looking to form the following:
Further thoughts of me down below.
End of edit 1
I tried to follow the steps from the question in the thread Interpolation between two curves (matlab) but it does not seem to work with my code. I am not sure if the code is actually applicable since the curves might overlap...
I tried to edit the code from the link above like the following:
% Save the data in one array each. The original data is stored in the
% arrays "x/yOriginal" row-wise.
curve1_data = [xOriginal(:,1) yOriginal(:,1)];
curve2_data = [xOriginal(:,2) yOriginal(:,2)];
% This was to try if the code from the link works
yy = [0:1:60];
xx1 = interp1(curve1_data(:,2),curve1_data(:,1),yy,'spline');
xx2 = interp1(curve2_data(:,2),curve2_data(:,1),yy,'spline');
m = 3; % Curve_Offset
mm(:,m) = xx1 + (xx2-xx1)*(m/(8750-5000));
% With the following I tried to interpolate over x
xx = (xOriginal(1,1):1:xOriginal(1,2));
yy1 = interp1(curve1_data(:,1),curve1_data(:,2),xx,'spline');
yy2 = interp1(curve2_data(:,1),curve2_data(:,2),xx,'spline');
m = 3; % Curve_Offset
% From the original code:
mm(:,m) = xx1 + (xx2-xx1)*(m/(8750-5000));
% Interpolation over x
yINT = yy1 + (yy2-yy1)*(m/8750-5000);
None of the interpolation-techniques worked, the y-values are either 90% negative (with the code from the link) or way too high (10e8 with the interpolation over x).
What I expected was that it creates a curve a little less steep than the curve "to its left" and a bit steeper than the curve "to its right".
My further thoughts:
The existing curves are the product of big 3-dimensional arrays. I.e. it could be that it is more the way to go to interpolate the arrays and then "read out" the Torque-over-rpm-Curves. On the other hand, I don't see a way to interpolate between two 1001-by-7001-by-5 arrays...
Moreover, for the next steps with the programm, the curve-interpolation needs to be quite fine (it is necessary to have way more than 1 interpolated curve between two existing curves) which makes the problem even more difficult.
If I understand right, the plot is generated with something like this:
plot(xOriginal(:,1),yOriginal(:,1))
plot(xOriginal(:,2),yOriginal(:,2))
% ...
If so, you can plot an intermediate curve with
plot((xOriginal(:,1) + xOriginal(:,2))/2, (yOriginal(:,1) + yOriginal(:,2))/2)
That is, the average between each pair of coordinates forms a curve that is exactly half-way between the two original curves.
Use weighted averages to generate more of these curves. This is linear interpolation.
d = 0.2;
plot(d*xOriginal(:,1) + (1-d)*xOriginal(:,2), d*yOriginal(:,1) + (1-d)*yOriginal(:,2))
Setting d = 0.5 we go back to the half-way case above.
Example:
xOriginal(:,1) = linspace(0.2,0.5,100);
yOriginal(:,1) = 3 * cos(xOriginal(:,1)*10-2.5) + 3;
xOriginal(:,2) = linspace(0.6,0.8,100);
yOriginal(:,2) = cos(xOriginal(:,2)*20-13.5) + 1;
clf; hold on
plot(xOriginal(:,1),yOriginal(:,1))
plot(xOriginal(:,2),yOriginal(:,2))
plot((xOriginal(:,1)+xOriginal(:,2))/2,(yOriginal(:,1)+yOriginal(:,2))/2)
(the interpolated line is in orange)

ploting a function under condition with Matlab [duplicate]

This question already has an answer here:
Multiple colors in the same line
(1 answer)
Closed 4 years ago.
I am looking for a solution to this problem:
consider a function f (x) = 2x + 1, with x belonging to [0, 1000]. Draw the representative curve of f as a function of x, so that if ||f (x)|| <3 the representative curve of f is in red color and else represent the curve of f in blue color.
Help me because I am a new user of Matlab software
The code below should do the trick:
% Obtain an array with the desired values
y = myfunc(x);
% Get a list of indices to refer to values of y
% meeting your criteria (there are alternative ways
% to do it
indInAbs = find((abs(y)<3));
indOutAbs = find((abs(y)>=3));
% Create two arrays with y-values
% within the desired range
yInAbs = y(indInAbs);
xInAbs = x(indInAbs);
% Create two arrays with y-values
% outside the desired range
yOutAbs = y(indOutAbs);
xOutAbs = x(indOutAbs);
% Plot the values
figure(1);
hold on;
plot( xInAbs, yInAbs, 'r')
plot( xOutAbs, yOutAbs, 'b')
legend('in abs', 'out abs', 'location', 'best')
There are alternative ways to do it which could be more efficient and elegant. However, this is a quick and dirty solution.
Your threshold cannot be too low, otherwise it has not enough data to plot (if threshold=3) or cannot see the blue part. Here I use 500 such that you can see.
function plotSeparate
clc
close all
k=0
threshold=500
for x=1:0.5:1000
k=k+1
y=f(x)
if abs(y)<threshold
t(k,:)=[x,y];
else
s(k,:)=[x,y];
end
end
plot(s(:,1),s(:,2),'-r',t(:,1),t(:,2),'-b')
end
function y=f(x)
y=2*x+1;
end

Plot region of the inequalities using matlab [duplicate]

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');

coloring specific point according to a given parameter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to matlab coding and I know it's simple that's why I'm trying to plot 2D datas read from a file,but I'm stuck,with my following code I'm reading the x y coordinates and trying to plot specific point with specific indexing according to given criteria ( which is p in my case I won't go too much into details), all I want to know is how can I modify the code so that I can give the correct index to the point that I want to color(i.e when the condition is satisfied plot this specific points with blue or watever),here is my code :
M=load('data1.XYZ');
x=M(:,1); %all x coordinates
y=M(:,2); %all y coordinates
xA=M(1:400,1); % x of particles A
yA=M(1:400,2); % y of particles A
xB=M(401:800,1); % x of particles B
yB=M(401:800,2); % y of particles B
Pos1=[x y]; % read in the x y coordinates
[num1,junk1] = size(Pos1);
PosA=[xA yA]; % read in the x y A coordinates
PosB=[xB yB]; % read in the x y B coordinates
[numA,junkA] = size(PosA);
[numB,junkB] = size(PosB); %no of all B particles
fprintf('Determining Distances between particles...\n');
r = zeros(numA,1);
psil_avg=0.0+0.0i;
psir_avg=0.0+0.0i;
for m=1:numA
for n=1:numA
cnt_l=0;
psi_l=0.0+0.0i;
if(m~=n)
r(m,n)=norm(PosA(m,:)-PosA(n,:));
if(r(m,n)< 1.44)
v1=PosA(m,:)-PosA(n,:);
u=[0 1];
dot=v1(:,1).*u(:,1)+v1(:,2).*u(:,2);
N=norm(v1);
cosinus=dot/N;
theta=acos(cosinus);
cnt_l=cnt_l+1;
psi_l=psi_l+(cos(theta)+6.0i*sin(theta));
psil_avg=psi_l/cnt_l;
for k=1:numA
cnt_r=0;
psi_r=0.0+0.0i;
if(m~k)
r(m,k)=norm(PosA(m,:)-PosA(k,:));
if(r(m,k)< 1.44)
v2=PosA(m,:)-PosA(k,:);
u2=[0 1];
dot2=v2(:,1).*u2(:,1)+v2(:,2).*u2(:,2);
N2=norm(v2);
cosinus2=dot2/N2;
theta2=acos(cosinus);
cnt_r=cnt_r+1;
psi_r=psi_r+(cos(theta2)+6.0i*sin(theta2));
psir_avg=psi_r/cnt_r;
p=sqrt(psi_r*psi_l);
if p > 0.94
% fprintf('bond order parameter is %f\n',p);
plot(xA(n),yA(n),'ro','Markersize',6);
hold on;
else
plot(xA(n),yA(n),'go','Markersize',8);
end
end
end
end
end
end
end
end
if anyone can help I'd be thankful
Use scatter and the following properties:
'MarkerEdgeColor' — Marker edge color
[0 0 1] (blue) (default) | 'auto' | 'none' | three-element RGB vector | string
'MarkerFaceColor' — Marker face color
'none' (default) | 'auto' | three-element RGB vector | string
Your code is a bit hard to read so I will adress this quiestion in general.
If you have groups of coordinates you would like to plot with different colors say X1,Y1 and X2,Y2 you may do the following
figure
plot(X1,Y1,'r*')
hold on
plot(X2,Y2,'b*')
hold off
This will color the first group in red with a dot and the second with blue and a dot.
The hold on, hold off is made to plot more then one plot on a single axis without clearing the previous one.
As suggested in the comments - you should probably try to avoid looping in matlab.
I could not understand your question clearly. Do you mean like this ? For example if I have two plots
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
Do you want different colours say red for 10,blue for 20, green for 30 degrees for both curves ? Is this what you want ? Please specify properly.

Matlab: ring-like graphs

is there a simple way in Matlab to create visualisations like the following?
Important are the 2 ring-like shapes and the attached vectors, (more or less) pointing to the center of the black spots. Hints to other visualisation tools creating vector images which might lead to similar results are also very appreciated! All my efforts to solve that task did not bring me any further...
Matlab would be a good platform for automatically generating this kind of visualisation for different "spot-scenarios"...
Thank you in advance,
M.
Here's part of the figure. The rest should be easy to figure out
%# define the ring
phi = linspace(0,2*pi,360);
innerRim = [cos(phi)',sin(phi)'];
outerRim = [cos(phi)',sin(phi)']*1.3;
xRing = [outerRim(:,1),innerRim(:,1),innerRim([2:end,1],1),outerRim([2:end,1],1)]';
yRing = [outerRim(:,2),innerRim(:,2),innerRim([2:end,1],2),outerRim([2:end,2],2)]';
%# create some data. 0 for black 0.5 for gray.
%# RingData has a value for each degree
ringData = ones(1,360) * 0.5;
ringData(25:30) = 0;
ringData(77:80) = 0;
ringData(240:255) = 0;
%# plot the ring
%# for an outer ring, add 1 to xRing, yRing
figure
patch(xRing,yRing,ringData,'EdgeColor','none');
set(gca,'cLim',[0 1]);
axis square
axis off
set(gcf,'color','w');
%# plot three arrows at the origin
hold on, qh=quiver(zeros(3,1),zeros(3,1),[0.4;0.3;-0.5],[0.7;-0.1;0.3])
set(qh,'LineWidth',3)
You can start with a compass plot: http://www.mathworks.com/help/techdoc/ref/compass.html or a polar plot: http://www.mathworks.com/help/techdoc/ref/polar.html . Another option is: http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/ .