How to find the point(s) when two plot curves cross?

1 回表示 (過去 30 日間)
tomer polsky
tomer polsky 2018 年 7 月 1 日
コメント済み: Image Analyst 2018 年 7 月 1 日
Hello. I want to plot these 2 plots:
y_1=a*x+b;
y_2=a*(x^2);
and find to point where they meet? The exact values of x. This is my code so far, but I don't know how to finish the statement :
clc;
clear all;
a=5;
b=10;
x=linspace(0,10,50);
y_1=a*x+b;
y_2=(x.^2);
for i=1:length(x);
if(a*x+b==(x.^2));
printf(x)
end
end
plot(y_1);
hold on;
plot(y_2);

回答 (1 件)

Image Analyst
Image Analyst 2018 年 7 月 1 日
Use roots():
a=5;
b=10;
x=linspace(-4,10,50);
y_1=a*x+b;
y_2=(x.^2);
for i=1:length(x)
if(a*x+b==(x.^2))
printf(x)
end
end
plot(x, y_1,'b-', 'LineWidth', 2);
hold on;
plot(x, y_2),'c-', 'LineWidth', 2;
grid on;
xRoots = roots([1, -a, -b])
y1 = a * xRoots(1) + b;
y2 = a * xRoots(2) + b;
plot(xRoots, [y1, y2],'ro', 'LineWidth', 2, 'MarkerSize', 13);
  4 件のコメント
tomer polsky
tomer polsky 2018 年 7 月 1 日
ok lets so that matlab cant handle do equality with floating numbers . but if I represnt x as x=1:1:50 means that that there are not floating numbers .
and my question is there any way with out using roots command to find when y_1==y_2?
Image Analyst
Image Analyst 2018 年 7 月 1 日
You can use ismembertol() like the FAQ says, but why? What's wrong with using roots? Why deny yourself the simplest, one-line solution? Usually people don't ask and prefer to do things the more complicated less accurate way so I'm not sure why you do.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by