Getting a smooth curve instead of two linear functions
18 次查看(过去 30 天)
显示 更早的评论
So, I got this figure as a part of a physics problem, with two intersecting linear functions and due to an extra restriction, only the left side of the intersection makes physical sense. I would like to have a smooth curve that connects the points on the graph, so, I would like to join them with a smooth curve. The functions are y1 = -x+4.4735 and y2=1.42x-9.09. In other words, I'm asking how to make a stencil that connects these dots smmothly?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/220090/image.bmp)
1 个评论
Star Strider
2019-5-18
Arbër Lladrovci’s Comment to my Answer clarifying the Question moved here —
I know, that technically the graph is correct, but I would like a smother transition between the two lines, I guess my question is similar to this https://stackoverflow.com/questions/43063157/forming-a-curve-between-two-lines but being a beginner I'm unable to understand the code there
(I deleted my Answer because I cannot solve this.)
采纳的回答
David Goodmanson
2019-5-21
编辑:David Goodmanson
2019-5-21
Hi Arber,
This can be done reasonably well by a hyperola whose asymptotes are the two lines in question. The hyperbola of course is never precisely on the lines anywhere, but it can be pretty close. My PC went out, probably for about a week, and I can't run Matlab right now (life is hardly worth thinking about) but I think this code will either run or be close to it. I'll be interested to find out if it does what it is supposed to.
I labeled the upper line A and the lower lne B. The code uses y as the independent variable because it runs from -inf to inf, and x is a function of y but y is not a (single valued) function of x.
Two crossed lines produce a couple of angles but no scale factor. Lam provides an adjustable scale factor. The smaller the value of Lam, the closer the curve is to the point where the lines cross, but also the stronger the curvature and the more 'pointy' the curve fit is. It's a tradeoff.
intA = 4.4735;
intB = -9.09;
Lam = 1;
A = -1; % dxdy
B = 1/1.42; % dxdy
x0 = (intA - intB)/2.42;
y0 = -x0 + intA;
e = (A+B)/2;
f = sqrt(-A*B);
y = -10:.001:10;
ys = y - y0; % shift
% hyperbola with coordinates based at the origin:
xs = e*ys -sqrt(Lam^2+(e^2+f^2)*ys.^2);
x = xs + x0; % shift back
xA = (y -intA)/(-1); % lines
xB = (y -intB)/1.42;
plot(y,x,y,xA,y,xB)
.
3 个评论
David Goodmanson
2019-5-22
Hi Arber, I'm glad it worked. Thanks to StarStrider for the correction, which showed the instant-feedback advantage of working interactive code. Not having that it just never occurred to me that the plot would turn out sideways.
更多回答(1 个)
John D'Errico
2019-5-18
编辑:John D'Errico
2019-5-18
You have two lines that create what is NOT a function, in the sense that it is single valued. So, for any x, you get a y out. Here, for some values of x, you would get two values of y, and for other values of x, you get no values for y. At one point, there is a single solution. Sorry, but that is not a function that can be evaluated properly. As such, you cannot use standard interpolation tools for this. They are designed to work with single valued relationships, and you don't have that.
There are viable solutions available, but you need to accept that compromise must always exist. So, if you want a curve that passes through what might be 3 nonlinearly shaped points, then it will NOT be a LINEAR curve! And once you accept that the result will not be composed of straight lines, then you must also accept that it will deviate in ways that are potentially significantly different from straight lines. Is the result allowed to miss that corner? If it does pass through the intersection point exactly, AND be smooth, then it will probably belly out a lot. So, what are you willing to give up? What shape would you draw by hand?
Next, again, the result will not be a function of the form y = f(x). That does not exist. What can be done here, is to swap the relationship into one of the form x = f(y). Once you do that, then you can create a functional form.
Finally, those lines go on forever to the left. They are essentially rays, or half lines, intersecting at one point, but going to minus infinity along both of those half lines. Do you want something that can return a point for x at y=-100 pr at y=+100? Or are you just asking for a smooth curve of some ilk?
I might draw a picture, but in order to do even that, I need to know how far those lines extend. You do not have three points. You have two half-lines. (In order for this to be a question about MATLAB, I'll use MATLAB here.) They intersect at:
syms x y
y1 = y == -x+4.4735;
y2 = y == 1.42*x-9.09;
[xint,yint] = solve(y1,y2);
[vpa(xint),vpa(yint)]
ans =
[ 5.6047520661157024793388429752066, -1.1312520661157024793388429752066]
The intersection happens around the (x,y) pair (5.60475, -1.13125).
But do the lines stop at x (or log(E)) around roughly x=3.5? How far out is minus infinity? And where would you have those lines no longer be linear?
vpa(subs(y1,x,3.5))
ans =
y == 0.9735
vpa(subs(y2,x,3.5))
ans =
y == -4.12
So, before I try to answer this question in any serious way, I would need to know the answer to all of those questions. What exactly are you looking to see as a result? Just a "smooth" curve that passes through the xy pairs:
[3.5, -4.12]
[5.60475, -1.13125]
[3.5, 0.9375]
Or what? What deviation from those lines is acceptable? How far to the left do those lines extend?
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!