MATLAB Answers

0

Getting a smooth curve instead of two linear functions

Asked by Arbër Lladrovci on 17 May 2019
Latest activity Commented on by David Goodmanson on 22 May 2019
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?

  1 Comment

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.)

Sign in to comment.

2 Answers

Answer by David Goodmanson on 21 May 2019
Edited by David Goodmanson on 21 May 2019
 Accepted Answer

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 Comments

@David — I believe Arbër actually wants something that would use a lightly edited version of your plot call:
plot(x,y,xA,y,xB,y)
although with a few other changes in your code that I do not feel comfortable doing myself.
I never thought of using a hyperbola, so quite definitely +1!
I hope your computer gets well soon!
Hi David,
Thank you very much for your amazing help. This was exactly what I was lookink for, and together with Star Strider's commentary on this and another thread I was able to modify the code to get the exact form I wanted.
Before this I was able to get some curve, by extracting some point on the graph and than using spcrv, although the curve looked good, I wasn't satisfied because it was hard to modify for any physical parameter change.
Your code did the job perfectly, so thanks a ton and all the best and hope your computer get's functional soon.
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.

Sign in to comment.


Answer by John D'Errico
on 18 May 2019
Edited by John D'Errico
on 18 May 2019

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 Comments

Sign in to comment.