How to plot a vertical line at f(x)=0.5
7 views (last 30 days)
Show older comments
Hello,
I am new to matlab and have some problems plotting the correct graphs. Here are my questions:
(1) How can I determine the x value at which it holds that my defined function f(x)=0.5? I want to draw a vertical line at this value and then colour the area to the right of the vertical line and below a function in a colour.
(2) How can I determine the y value at which it holds that the function is defined as follows f1 = f(0.5) with x=0.5. I want to draw a horizontal line at this value and color the area above and to the left of a function.
Maybe someone has an idea on how to define this in matlab to get the correct plots?
Thankful for any help!
Best AK
0 Comments
Accepted Answer
Star Strider
on 8 Aug 2021
xv = 0.5;
yv = 0.5;
figure
hold on
xline(xv)
yline(yv)
patch([[0 xv] fliplr([0 xv])], [[0 0] fliplr([yv yv])], 'g')
patch([[xv 1] fliplr([xv 1])], [[0 0] fliplr([yv yv])], 'r')
hold off
axis([0 1 0 1])
That illustrates the approach.
If you have a specific funciton and data to plot, I will help you adapt this code to that problem.
.
8 Comments
Star Strider
on 31 Mar 2022
I am lost.
Where does this go:
for i = 1:length(phi2)
phi1StarArr4(i) = phi1StarStar(phi2(i), p, 1-p, theta); %Line fl^u
end
in the code and in the plot calls?
I assume that the plot call using it would be:
plot(phi2, phi1StarArr4, '-m')
however I do not see that anywhere.
Perhaps —
p = 0.25;
thetaArr = [1.4; 2]
phi2 = 0.5:0.01:1;
for i1=1:2
theta = thetaArr(i1)
phi1StarArr1 = zeros(length(phi2), 1);
phi1StarArr2 = zeros(length(phi2), 1);
phi1StarArr3 = zeros(length(phi2), 1);
phi1StarArr4 = zeros(length(phi2), 1); %Update for line fl^u
phi1 = 0.5:0.01:1;
for i = 1:length(phi2)
phi1StarArr1(i) = phi1Star(phi2(i), 1-p, p, theta); %Line fl^s2
end
for i = 1:length(phi2)
phi1StarArr2(i) = phi1Star(phi2(i), p, p, theta); %Line fl^2
end
for i = 1:length(phi2)
phi1StarArr3(i) = phi1Star(phi2(i), p, 1-p, theta); %Line fl^s1
end
for i = 1:length(phi2)
phi1StarArr4(i) = phi1StarStar(phi2(i), p, 1-p, theta); %Line fl^u
end
figure
plot(phi2, phi1StarArr1, 'color', 'g')
hold on
plot(phi2, phi1StarArr2, 'color', 'b')
plot(phi2, phi1StarArr3, 'color', 'r')
plot(phi2, phi1StarArr4, '-m') % ADDED
yv1 = phi1StarArr3(1); % Lower Red Border
xvix = find(diff(sign(phi1StarArr1-0.5))); % Reference Index — Green Left Border
idxrng = (0:1)+xvix; % Index Range For Interpolation
xv1 = interp1(phi1StarArr1(idxrng),phi2(idxrng),0.5) % Interpolate
xv1 = xv1.*(i1==1) + 1.*(i1==2) % Conditionally Set 'xv1'
plot(xv1, yv1, 'color', 'k')
%Axis labels
xlabel('\phi_2')
ylabel('\phi_1', 'Rotation', 0)
tit = sprintf('Focusing Areas for theta = %.1f', round(theta,1))
title(tit)
axis([0.5 1.0 0.5 1.0])
%Colored patches
patch([phi2 fliplr(phi2)], [max([ones(size(phi2))*yv1; phi1]) ones(size(phi2))*max(ylim)], 'r', 'EdgeColor','none', 'FaceAlpha',0.25)
phi2xv1Lv = phi2 >= xv1; % Define 'phi2' Subset For Green Patch
patch([phi2(phi2xv1Lv) fliplr(phi2(phi2xv1Lv))], [phi1StarArr2(phi2xv1Lv); ones(size(phi1StarArr2(phi2xv1Lv)))*min(ylim)].', 'g', 'EdgeColor','none', 'FaceAlpha',0.25)
% Function von (0.5/0.5) bis (1/1)
plot(phi2, phi1, 'color', 'k')
hold off
end
%Unique root of g
function res = phi1Star(phi2, x, xPrime, theta)
function g2_res = g_in_phi1(phi1)
g2_res = g(phi1, phi2, x, xPrime, theta);
end
fun = @g_in_phi1;
root = fzero(fun, 0.7);
if root >= 0.5 & root <= 1.0
res = root;
else
res = 0.5;
end
end
%Unique root of h
function res2 = phi1StarStar(phi2, x, xPrime, theta)
function h2_res2 = h_in_phi1(phi1)
h2_res2 = h(phi1, phi2, x, xPrime, theta);
end
fun = @h_in_phi1;
root = fzero(fun, 0.7);
res2 = NaN; % Added To Avoid An Error When 'res2' Is Not Assigned
if root >= 0.5 & root <= 1.0
res2 = root;
end
end
% Funktion g
function g_res = g(phi1, phi2, x, xPrime, theta)
g_res = (f(phi1,x) - f(1-phi1, xPrime))*theta - (f(phi2,1-x) - f(1-phi2, 1-xPrime));
end
% Funktion h
function h_res = h(phi1, phi2, x, xPrime, theta)
h_res = (f(phi1,x) - f(1-phi1, 1-xPrime))*theta + (f(phi2,1-x) - f(1-phi2, xPrime));
end
% Funktion f
function f_res = f(y,z)
f_res = y.*z ./(y.*z + (1-y).*(1-z));
end
It appears that ‘phi1StarArr4’ is almost uniformly NaN because the value of ‘root’ are almost all outside the limits set for it. I have no idea what you are doing, so you need to fix that problem.
.
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!











