Find intersectionpoint between two functions

2 ビュー (過去 30 日間)
Jurij Rudenson
Jurij Rudenson 2019 年 6 月 16 日
コメント済み: Star Strider 2019 年 6 月 16 日
hello, i need to fund a exact intersection point between two functions on the X-axis. the script is following:
clear
close all
figure
hold on;
% 4,8 mil / vecka
years = 5;
n = years*500; % antalet mil
x = linspace(0,n,n+1);
Bpris = 16; % bensin pris
%bil 1
k2 = Bpris*0.74; % Bensin
a2 = 15000; % Pris
b2 = years*392*12; % Försäkring
m2 = a2+b2;
p2 = m2 + k2.*x;
%Bil 2
k3 = Bpris*0.55; % Bensin
a3 = 22000; % Pris
b3 = years*370*12; % Försäkring
m3 = a3+b3;
p3 = m3 + k3.*x;
%plot(x,p1,"LineStyle","-")
plot(x,p2,"LineStyle","-")
plot(x,p3,"LineStyle","--")
xlabel('Körd sträcka [Mil]')
ylabel('Pris [Kr]')
hold off;
Now i have trubble making a code for intersection of the p2 and p3 functions (p2-p3=0) on the X-value, please help me.

採用された回答

Star Strider
Star Strider 2019 年 6 月 16 日
Add this line just before the plot calls:
intx = (m2 - m3) / (k3 - k2); % X-Intersection
and add:
plot(intx, m3 + k3*intx, '+g', 'MarkerSize',20)
to the plot calls.
  3 件のコメント
Jurij Rudenson
Jurij Rudenson 2019 年 6 月 16 日
but how do i make a line from the intersection to the Y-axis and from intersection to X-axis in the graph?
Star Strider
Star Strider 2019 年 6 月 16 日
My pleasure!
Add this line to the plot calls:
plot([min(xlim) intx], [1 1]*(m3 + k3*intx), '--g', [1 1]*intx, [min(ylim) (m3 + k3*intx)],'--g')
It will plot dashed green lines from the respective values for min(xlim) and min(ylim) to the intersection. (You did not mention originally that you wanted those lines, or I would have included the extra plot call in my Answer.)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by