How to get the intersection points of a line and a curve which was fit to data?

Hi,
I have a line and a curve that was fit to a data. I also get Coefficients of Equation of the Curve, but don't know how to find its equation to make two equations equal to find the points of the tangency. Could someone give me some recommends?
Here is my code:
clc
array=[515 525 561 600 632 700 761 800 900 1000 1014 1750;
0 150 300 394 450 540 600 631 696 745 750 865];
x=linspace(array(1,1),array(1,end),101)
y=interp1(array(1,:),array(2,:),x,'pchip')
x=transpose(x)
y=transpose(y)
%
f=fit(y,x,'pchip')
a=coeffvalues(f)
plot(f,y,x)
hold on
% Equation of line that pass through origin
x1=0:1000;
slope=tan(51.5*pi/180);
y1=slope*x1
plot(x1,y1)

 採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 10 月 24 日
編集済み: Andrei Bobrov 2014 年 10 月 25 日
one way with Curve Fitting Toolbox
array=[515 525 561 600 632 700 761 800 900 1000 1014 1750;
0 150 300 394 450 540 600 631 696 745 750 865];
x = array([2 1],:)';
f = fit(x(:,1),x(:,2),'cubicinterp');
df = fit(x(:,1),differentiate(f,x(:,1)),'cubicinterp');
xx = fzero(@(x)f(x)/x - df(x),[1 750]);
x1 = linspace(x(1,1),x(end,1),300);
plot(x1,f(x1),x1,x1*df(xx),xx,f(xx),'ro');
well, more
f = fit(x(:,1),x(:,2),'cubicinterp');
df = fit(x(:,1),differentiate(f,x(:,1)),'cubicinterp');
k = tand(10);
xx = fzero(@(x)df(x) - k,[1 x(end,1)]);
you line: y = k*x + b
b = f(xx) - k*xx;
x1 = linspace(x(1,1),x(end,1),300);
plot(x1,f(x1),x1,k*x1 + b,xx,f(xx),'ro');

8 件のコメント

Khanh
Khanh 2014 年 10 月 24 日
Hi Andrei,
Your code worked so great except one thing the tangent line which I desired had the slope is tan(10*pi/180). Sorry about my question. It was a little wrong. I would like to determine the tangent line which has the slope is tan(10*pi/180) . The tangent line will have the equation y=tan(10*pi/180)*x+b . The equation of the curve is y=f. So I need to solve the equation df=tan(10*pi/180) to find x coordinate of the point of tangency. I use function
x_tangency=solve(df-tan(10*pi/180)).
But it didn't work. Could you please take a look?
Andrei Bobrov
Andrei Bobrov 2014 年 10 月 24 日
see code after "well"
Chad Greene
Chad Greene 2014 年 10 月 24 日
Side note, tan(10*pi/180) is the same thing as tand(10).
Andrei Bobrov
Andrei Bobrov 2014 年 10 月 25 日
Hi Chad! I agree with you.
Khanh
Khanh 2014 年 10 月 25 日
Hi Andrei,
Really thank you for your help. It worked so perfectly. Could I ask you one more question? How can I determine two intersection points as the following picture? The equation of the line: y=tand(30)*x+400 and the curve as above. I tried to solve the equation: xx=fzero(@(x) f(x)-tand(30)*x-400,[x(end,1)]). Then I just get one intersection point instead of two.
Andrei Bobrov
Andrei Bobrov 2014 年 10 月 26 日
編集済み: Andrei Bobrov 2014 年 10 月 26 日
array=[515 525 561 600 632 700 761 800 900 1000 1014 1750;
0 150 300 394 450 540 600 631 696 745 750 865];
x = array([2 1],:)';
f = fit(x(:,1),x(:,2),'cubicinterp');
f1 = @(x)tand(30)*x+400;
xx = x(1):x(end,1);
[~,ii] = min(f(xx(:)) - f1(xx(:)));
x1 = fzero(@(x)f(x) - f1(x),[0,xx(ii)]);
x2 = fzero(@(x)f(x) - f1(x),[xx(ii),xx(end)]);
plot(xx(:),[f(xx(:)),f1(xx(:))],[x1,x2],f([x1,x2]),'ro');
Khanh
Khanh 2014 年 10 月 27 日
Wow. Perfect. Thank you so much.
Hussein Qenawy
Hussein Qenawy 2019 年 4 月 13 日
編集済み: Hussein Qenawy 2019 年 4 月 13 日
Good evening Mr andrie.. Iam studying matlab now... I have 4 problems need to solve by matlab. I need your help to solve these problems for money.. If it's OK for you.. Contact me whatsapp 0097430272448. Thanks

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

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 10 月 24 日

編集済み:

2019 年 4 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by