How to finde intersections ?

2 次查看(过去 30 天)
Hossein Alishahi
Hossein Alishahi 2021-3-9
Hi,
I hope you are Keeping safe.
I have a code as belows
clear all
clc
close all
alpha1=input('Enter positive "alpha1"?');
alpha2=input('Enter positive "alpha2"?');
beta1=input('Enter positive "beta1"?');
beta2=input('Enter positive "beta2"?');
lambda=input('Enter positive "lambda"?');
eta1=input('Enter positive "eta1"?');
eta2=input('Enter positive "eta2"?');
P=input('Enter positive "P"?');
%% Feasible Area
M=max(P,eta1); N=max(P,eta2);
a = -1:M;m = 0; c = eta2; b = m * a + c;
plot(a, b, '--','Color', 'black')
hold on
b = -1:N; m = 0; c = eta1; b = m * a + c;
plot(b, a, ':','Color', 'black','LineWidth',2)
eta1=min (eta1,P); eta2=min (eta2,P);
if eta1+eta2<P
X=[0 0 eta1 eta1]; Y=[0 eta2 eta2 0];
fill(X,Y,[0.85 0.85 0.85]);
else
X=[0 0 P-eta2 eta1 eta1]; Y=[0 eta2 eta2 P-eta1 0];
fill(X,Y,[0.85 0.85 0.85]); axis([-1 P+eta1+1 -1 P+eta2+1])
end
g = @(a,b) a+b-P;
fimplicit(g,[0 P 0 P],'-.','Color', 'black')
f = @(a,b) log((1+alpha1*a./(1+alpha2*b))) -lambda*log((1+beta1*a./(1+beta2*b)));
fimplicit(f,[-1 P+eta1+1 -1 P+eta2+1], 'Color','green')
hold on
and want to find intersection between lines . For example consider
alpha1=beta1=1, alpha2=0.2, beta2=2, ambda=2, eta1=3, eta2=4 and P=2
Could you please give me how to find and plot the intersections points.
MAny Thanks in advance
  4 个评论
Hossein Alishahi
Hossein Alishahi 2021-3-9
编辑:Walter Roberson 2025-1-2
please try it !
%% Input Parameters
clear all
clc
close all
alpha1=input('Enter positive "alpha1"?');
alpha2=input('Enter positive "alpha2"?');
beta1=input('Enter positive "beta1"?');
beta2=input('Enter positive "beta2"?');
lambda=input('Enter positive "lambda"?');
eta1=input('Enter positive "eta1"?');
eta2=input('Enter positive "eta2"?');
P=input('Enter positive "P"?');
%% Feasible Area
M=max(P,eta1); N=max(P,eta2);
a = -1:M;m = 0; c = eta2; b = m * a + c;
plot(a, b, '--','Color', 'black')
hold on
b = -1:N; m = 0; c = eta1; b = m * a + c;
plot(b, a, ':','Color', 'black','LineWidth',2)
eta1=min (eta1,P); eta2=min (eta2,P);
if eta1+eta2<P
X=[0 0 eta1 eta1]; Y=[0 eta2 eta2 0];
fill(X,Y,[0.85 0.85 0.85]);
else
X=[0 0 P-eta2 eta1 eta1]; Y=[0 eta2 eta2 P-eta1 0];
fill(X,Y,[0.85 0.85 0.85]); axis([-1 P+eta1+1 -1 P+eta2+1])
end
g = @(a,b) a+b-P;
fimplicit(g,[0 P 0 P],'-.','Color', 'black')
f = @(a,b) log((1+alpha1*a./(1+alpha2*b))) -lambda*log((1+beta1*a./(1+beta2*b)));
fimplicit(f,[-1 P+eta1+1 -1 P+eta2+1], 'Color','green')
hold on
grid on
%% Legend
xlabel('{\rho_{1}}');
ylabel('{\rho_{2}}');
legend('\eta_{2}','\eta_{1}','Feasible Area','{\rho_{1}}+{\rho_{2}}={\rho}','{log}({1}+({\alpha_{1}} {\rho_{1}}))/({1}+{\alpha_{2}} {\rho_{2}})={\lambda} {log}{1}+({\beta_{1}} {\rho_{1}})/({1}+{\beta_{2}} {\rho_{2}})','Location','northeast')
Jan
Jan 2021-3-9
You have asked some questions aboput the same problem already. It is still not clear, which intersections your are searching for. "intersection between lines" - between which lines? You post some code, which seems to work fine. But what exactly is your question?

请先登录,再进行评论。

回答(1 个)

Gautam
Gautam 2025-1-2
Hello Hossein,
If you want to find the point of intersection of any tow lines programmatically, you can do so following the steps below:
  1. Create handles to the line objects for which you want to find the intersection point, like:
line1 = line(X,Y,'Color','black');
line2 = line(Y,X,'Color','black');
2. Use the “XData” and “YData” properties of the line objects to find the point of intersection:
interX = line1.XData(line1.XData == line2.XData);>>interY = line1.YData(line1.YData == line2.YData);
3. You can show the point of intersection on the plot using
plot(interX, interY, 'rO', 'MarkerSize',6, 'MarkerFaceColor','red')
This would give you the intersection point like:
Following this, you can find the intersection etween any two lines
  1 个评论
Walter Roberson
Walter Roberson 2025-1-2
Comparing the XData and YData properties of two lines will generally not work.
X = [0 3];
Y = [1 2];
line1 = line(X,Y,'Color','black');
line2 = line(Y,X,'Color','black');
interX = line1.XData(line1.XData == line2.XData);
interY = line1.YData(line1.YData == line2.YData);
plot(interX, interY, 'rO', 'MarkerSize',6, 'MarkerFaceColor','red')
There is no-place that the XData from the one line happens to be exactly the XData from the other line.
The XData property does not interpolate. The result is not the same as
cla
X = 0:.1:3;
Y = linspace(1,2,numel(X));
line1 = line(X,Y,'Color','black');
line2 = line(Y,X,'Color','black');
interX = line1.XData(line1.XData == line2.XData);
interY = line1.YData(line1.YData == line2.YData);
plot(interX, interY, 'rO', 'MarkerSize',6, 'MarkerFaceColor','red')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by