![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/281700/image.jpeg)
How to find intersection between line and curve
12 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/281677/image.jpeg)
I am trying to find the places where each of my curves intersects the line y=0.01 on matlab.
How can I do this?
Here is what I have wrote so far to try:
%Finding Equations of Curves
p=polyfit(s, Receptive_Field,4);
p2=polyfit(s,0.5*GME(s, sigE),2);
%Here is what the plot contains
plot(s, (0.5*GME(s, sigE)), '-.b')
hold on
plot(s, Receptive_Field, 'r')
plot([-10 10],[0.01 0.01], 'k:')
hold off
0 个评论
采纳的回答
John D'Errico
2020-4-3
编辑:John D'Errico
2020-4-3
Just because the only tool you own is a hammer does not mean every problem must be treated as itf it is a nail. Similarly, just because the only tool you know how to use is polyfit, does not make that a remotely good idea to use here.
The simplest solution is to use a tool designed to solve your problem. Almost certainly, the proper tool here is a spline model, of SOME UNSPECIFIED SORT. However, you could also easily just use tools which use linear interpolation, as that would be pretty accurate. In fact though, linear interpolation can be viewed as just a low order spline.
You have not provided any data, so I cannot show you how to solve this directly, so I'll need to make up some data. Sadly, I'm not feeling very creative today, but there really is no need to be that creative. Simple will do.
x = -5:.1:5;
y = sin(x);
plot(x,y,'o')
yline(0.1);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/281700/image.jpeg)
OK. So how can we find the locations of the intersections of my simple function, with the horizontal line at y==0.1? Yes, I know that happens at the multiple solutions of the problem sin(x)==0.1. So what? In fact, there are multiple solutions, and we wish to find them all. However, for purpose of comparison, it will be useful to use the analytical solution from asin. The primary solution lives here:
x0 = asin(0.1);
x0 =
0.10016742116156
But there are also solutions at infinitely many locations, the three solutions we should find from the graphic lie at:
x0 = [-pi - x0, x0, pi - x0];
sin(x0)
ans =
0.0999999999999998 0.1 0.1
As you should see, all three elements of x0 yield the desired value for sin(x0). The tiny discrepancy in one of them is just floating point trash in the least significant bits.
How should we find those points now? DO NOT USE POLYFIT! DO NOT USE POLYFIT! DO NOT USE POLYFIT!!!! There, I've said it three times, so it must be true.
Instead, again, we need to use a valid tool for the purpose. The first one I'll suggest is Doug Schwarz's intersections tool. It lives on the file exchange. (Link provided below.)
[xlin,ylin] = intersections(x,y,[-5 5],[.1 .1])
xlin =
-3.24188910837421
0.10016854536593
3.04131030579016
ylin =
0.1
0.1
0.1
Intersections just uses linear interpolation to find those points of intersection. In fact, each of them looks to be correct to within roughly 4 significant digits.
Next, we could use a spline. Both spline or pchip will be acceptable here, though spline might be more accurate for many problems. pchip also will be good, sometimes best on problems with significant slope changes, or flat regions, where use of a spline may result in ringing behavior.
I'll use a tool of my own to generate the functional inverse of that spline to give the three solutions. (Again, you can download my SLM toolbox from the file exchange. I'll give a link at the end.)
spl1 = pchip(x,y);
xpchip = slmsolve(spl1,0.1)
xpchip =
-3.24176645744013 0.100167703314966 3.04143262180259
spl2 = spline(x,y);
xspline = slmsolve(spl2,0.1)
xspline =
-3.24176010343974 0.100167421255852 3.04142521171113
Now, how well did we do?
abs(x0 - xlin')
ans =
0.000129033622857655 1.12420437066441e-06 0.000114926638069335
abs(x0 - xpchip)
ans =
6.38268877750647e-06 2.82153406486185e-07 7.3893743612885e-06
abs(x0 - xspline)
ans =
2.86883903299895e-08 9.42918382262903e-11 2.0717103588197e-08
To be honest, probably any of the three solutions are probably adequate for most purposes. But for god sakes, DON'T USE POLYFIT! Just because Taylor series are effectively made from polynomials, does not mean they are a good choice.
Find intersections here for free download from the file exchange:
Find slmsolve (part of my SLM toolbox) here for free download from the file exchange:
Note that while parts of my SLM toolbox use the optimization toolbox, slmsolve uses nothing special itself.
1 个评论
Garvit Amipara
2021-4-9
I was following some other questions and their suggested solutions using polyfit, spent so many hours to fix problems and nothing worked! Your answer helped a lot! Thank you for the warning: DO NOT USE POLYFIT!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!