Find intersectionpoint between two functions

조회 수: 1 (최근 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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by