How i add a line in 2d plot
조회 수: 2 (최근 30일)
이전 댓글 표시
I'd like to add lines where the values of a(alpha) and b (beta) for which the max value is 4 and for which the max value is greater than 4 as shown by red dashed line and how i label them like in that box in the graph below.
My code
objfun file
function f = threestate2(x,a,b)
c1 =cos(x(1))*(cos(x(5))*(cos(x(9))+cos(x(11)))+cos(x(7))*(cos(x(9))-cos(x(11))))+ ...
cos(x(3))*(cos(x(5))*(cos(x(9))-cos(x(11)))-cos(x(7))*(cos(x(9))+cos(x(11))));
c2=sin(x(1))*(sin(x(5))*(sin(x(9))*cos(x(2)+x(6)+x(10))+sin(x(11))*cos(x(2)+x(6)+x(12))) ...
+sin(x(7))*(sin(x(9))*cos(x(2)+x(8)+x(10))-sin(x(11))*cos(x(2)+x(8)+x(12))))+ ...
sin(x(3))*(sin(x(5))*(sin(x(9))*cos(x(4)+x(6)+x(10))-sin(x(11))*cos(x(4)+x(6)+x(12))) ...
-sin(x(7))*(sin(x(9))*cos(x(4)+x(8)+x(10))+sin(x(11))*cos(x(4)+x(8)+x(12))));
A1=a^2-b^2;
A2=2*a*b;
f1=A1*c1 +A2*c2;
f=-(f1^2);
my main file
clear
close
clc
%x=[x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10),x(11),x(12)]; % angles;
lb=[0,0,0,0,0,0,0,0,0,0,0,0];
ub=[pi,2*pi,pi,2*pi,pi,2*pi,pi,2*pi,pi,2*pi,pi,2*pi];
options = optimoptions(@fmincon,'TolX',10^-12,'MaxIter',1500,'MaxFunEvals',10^8,'Algorithm','sqp','TolFun',10^-8);
a=0:0.01:1;
% b=sqrt(1-a.^2);
w=NaN(size(a));
ww=NaN(size(a));
for k=1:30
x0=rand([1,12]).*ub*.9986;%7976
for i=1:length(a)
bhelp=1-a(i)^2;
if (bhelp>0 || bhelp==0)
b=sqrt(bhelp);
[~,fval]=fmincon(@(x)threestate2(x,a(i),b),x0,[],[],[],[],lb,ub,[],options);
w(i)=sqrt(-fval);
else
w(i)=nan;
end
ww=max(w,ww);
end
x=a.^2;
end
plot(x,ww)
grid on
ylabel('\fontname{Times New Roman} S_{max}(\Psi_{gs})')
xlabel('\fontname{Times New Roman}\alpha^2')
댓글 수: 0
답변 (1개)
Thorsten
2016년 1월 18일
You can plot the horizontal line using
line([min(xlim) max(xlim)], [4 4], 'Color', 'r', 'LineStyle', '--')
If you have computed a and b, you can add the vertical line analogously using
for x = [a, b]; line([x x], [min(ylim) 4], 'Color', 'r', 'LineStyle', '--'); end
To add the text, use
ypos = -diff(ylim)/12; % 1/12 of the y size of the plot; adapt if necessary
for x = [a, b]; text(x, ypos, num2str(x), 'HorizontalAlignment', 'center'); end
댓글 수: 7
Thorsten
2016년 1월 21일
편집: Thorsten
2016년 1월 21일
y = [4.0000 3.9992 3.9968 3.9928 3.9872 3.9800 3.9712 3.9608 3.9488 3.9352 3.9200...
3.9032 3.8848 3.8648 3.8432 3.8200 3.7952 3.7688 3.7408 3.7112 3.6800 3.6472...
3.6128 3.5768 3.5392 3.5000 3.4592 3.4168 3.3728 3.3272 3.2800 3.2312 3.4300...
3.5244 3.6175 3.7093 3.7999 3.8890 3.9767 4.0630 4.1477 4.2308 4.3123 4.3922...
4.4703 4.5466 4.6210 4.6935 4.7641 4.8326 4.8990 4.9632 5.0252 5.0848 5.1421...
5.1968 5.2491 5.2986 5.3455 5.3895 5.4306 5.4686 5.5036 5.5353 5.5636 5.5885...
5.6097 5.6272 5.6408 5.6504 5.6557 5.6567 5.6530 5.6446 5.6312 5.6125 5.5883...
5.5584 5.5223 5.4798 5.4306 5.3741 5.3100 5.2376 5.1565 5.0659 4.9651 4.8531...
4.7289 4.5912 4.4384 4.2686 4.0793 3.8674 3.6284 3.3561 3.3728 3.5272 3.6832...
3.8408 4.0000];
x = linspace(0,1,numel(y));
% find the zero crossings
yval = 4;
x0 = findzeros(x, y-yval);
alpha = x0(2:3); % exclude zero crossings at the begin and end
% do the plot
plot(x, y)
line([min(xlim) max(xlim)], [yval yval], 'Color', 'r', 'LineStyle', '--')
ypos = min(ylim)-diff(ylim)/20; % 1/12 of the y size of the plot; adapt if necessary
for xi = alpha
line([xi xi], [min(ylim) yval], 'Color', 'r', 'LineStyle', '--');
text(xi, ypos, num2str(xi), 'HorizontalAlignment', 'center');
end
using my function findzeros
function x0 = findzeros(x, y)
%FINDZEROS find location of zero crossings with interpolation
%
% X0 = FINDZEROS(X, Y) finds location X0 of zero crossings of function
% Y = f(X) given by vectors X and Y, with interpolation.
%
% Thorsten.Hansen@psychol.uni-giessen.de 2016-01-21
% solution adapted from
% http://www.mathworks.com/matlabcentral/answers/118921-fast-location-of-zero-crossings-with-interpolation
% sample data
if nargin == 0
x = 1:0.2:2e5;
y = sin(x);
%noise = -0.03 + (0.06).*rand(size(clean));
%y = y + noise;
x = x(1:100); y = y(1:100);
end
% find position of zero crossings
ind_y0 = find(diff(y>0) ~= 0);
% originall solution
% ind_y0 = find(y .* circshift(y,[0 1]) < 0); % add - 1 to match my
% ind_y0 used above
N = numel(ind_y0); % number of zero crossings
x0 = nan(1,N); % preallocate for speed
% interpolate zeros
for i = 1:N
%ixrng = y0(i)-2:y0(i)+2;% non-symmetric surround in original solution
ind_y0i = ind_y0(i):ind_y0(i) + 1;
X = x(ind_y0i);
Y = y(ind_y0i);
b = [ones(size(X)); X]'\Y';
x0(i) = -b(1)/b(2);
end
% find zeros at the first and last position
if y(1) == 0, x0 = [x(1) x0]; end
if y(end) == 0, x0 = [x0 x(end)]; end
if nargout == 0
plot(x, y, 'o-')
hline(0)
vlines(x0, 'Color', 'r')
clear x0
end
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!