Find intersectionpoint between two functions

1 Ansicht (letzte 30 Tage)
Jurij Rudenson
Jurij Rudenson am 16 Jun. 2019
Kommentiert: Star Strider am 16 Jun. 2019
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.

Akzeptierte Antwort

Star Strider
Star Strider am 16 Jun. 2019
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 Kommentare
Jurij Rudenson
Jurij Rudenson am 16 Jun. 2019
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 am 16 Jun. 2019
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.)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by