MATLAB Answers

0

Find intersectionpoint between two functions

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.

  0 件のコメント

サインイン to comment.

1 件の回答

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 2019 年 6 月 16 日
thanks!
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
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.)

サインイン to comment.



Translated by