Find intersectionpoint between two functions

1 view (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 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 Comments
Jurij Rudenson
Jurij Rudenson on 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 on 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.)

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots 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!