Curve fit with three points with a given slope - matlab

This might be of math problem than Matlab. Nonetheless, here is my problem.
so, I have a data set represented by the green curve. It's usually linear, but sometimes it can have a slight curvature. Then, I have two additional points: the red and the blue. The red is far out in the negative. Its amplitude is 30~100 bigger than the X value of the green circle and it's always on the X-axis. The blue circle is always on the Y-axis.
I need a curve that fits the red, the blue, and the green circles, but there are two more constraints:
Blue curve can't be negative
where the blue curve meets the green curve, I want the slopes of them to be the same (smooth transition)
The red point doesn't have to be on the Y-axis, but asymptotic to zero and very close to zero at the red circle.
I have tried different inverse functions such as exponential, polynomial, 1/something, etc., but without a slope constraint, the end-result tends to have a cusp at the green point and it causes a problem for an overall analysis in which this curve-fitting function is used.
Can this be done?

If you just need to roughly match the slope and you have access to the data points of the green curve (not just the green dot), you can use multiple points of the green curve in the fitting function together with the blue and red point. That should produce a curve that passes through all the points and pretty much match the slope in the green point.
Clearly, if you need to match the slope with mathematical precision, you need to set up another constraint equation as it was suggested by duffymo.

I'm taking an answer from
https://math.stackexchange.com/questions/2523269/three-point-curve-fit-with-a-specificed-slope-at-one-point][1]
This uses the fact that it passes three points and at one point, its slope is known. With the four equations and a generic irrational/exponential function, I was able to create a reasonable curve-fit.

Related

Can I compute contour orientation without using polygon area sign?

Most of the times, I determine contour orientation generating 2D points and computing the closed polygon area. Depending on the area value sign I can understand if the contour is oriented clockwise or not (see How to determine if a list of polygon points are in clockwise order?).
Would it be possible to do the same computations without generating 2D points? I mean, relying only on geometric curve properties?
We are interested in determining the orientation of contours like these ones without sampling them with 2D points.
EDIT: Some interesting solutions can be found here:
https://math.stackexchange.com/questions/423718/general-way-to-find-out-whether-a-curve-is-positively-oriented
Scientific paper: Determining the orientation of closed planar curves, DJ Filip (1990)
How are those geometric curves defined?
Do you have an angle for them? The radius doesn't matter, only the difference between entry-angle and exit-angle of each curve.
In that case, a trivial idea crossing my mind is to just sum up all the angles. If the result is positive, you know you had more curves towards the right meaning it's a clockwise contour. If it was negative, then more curves were leftwards -> anti-clockwise contour. (assuming that positive angels determine a right-curve and vica versa)
After thinking about this for awhile, for polygons that contain arcs I think there are three ways to do this.
One, is to break the arcs into line segments and then use the area formula as described above. The success of this approach seems to be tied to how close the interpolation of the arcs is as this could cause the polygon to intersect itself.
A quicker way than the above would be to do the interpolation of the arcs and then find a vertex in the corner (minimal Y, if tie minimal X) and use the sign of the cross product for that vertex. Positive CCW, negative CW. Again, this is still tied to the accuracy of the interpolation.
I think a better approach would be to find the midpoint of the arc and create two line segments, one from the beginning of the arc to the midpoint and another from the midpoint to the end of the arc and replace the arc with these line segments. Now you have a polygon with only line segments. Then you can add up all the normalized cross products of all the vertices. The sign will tell you the direction. Positive is counter-clockwise, negative is clockwise. In this case it doesn't matter if the polygon self-intersects.

How to find distance mid point of bezier curve?

I am making game in Unity engine, where car is moving along the bezier curve by percentage of bezier curve legth.
On this image you can see curve with 8 stop points (yellow spheres). Between each stop point is 20% gap of total distance.
On the image above everything is working correctly, but when I move handles, that the handles have different length problem occurs.
As you can see on image above, distances between stop points are not equal. It is because of my algorithm, because I am finding point of segment by multiplying segment length by interpolation (t). In short problem is that: if t=0.5 it is not in the 50% percent of the segment. As you can see on first image, stop points are in half of segment, but in the second image it is not in half of segment. This problem will be fixed, if there is some mathematical formula, how to find distance middle point.
As you can see on the image above, there are two mid points. T param mid point can be found by setting t to 0.5 (it is what i am doing now), but it is not half of the distance.
How can I find distance mid point (for cubic bezier curve, that have handles in different distance)?
You have correctly observed that the parameter value t=0.5 is generally not the point in the middle of the length. That is a good start but the difficulty lies in the mathematics beneath.
Denoting the components of your parametric curve by x(t) and y(t), the length of the curve
between t=0 (the beginning) and a chosen parameter value t = u is equal to
What you are trying to do is to find u such that l(u) is one half of l(1). This is sometimes possible but often difficult or impossible. So what can you do?
One possibility is to approximate the point you want. A straightforward way is to approximate your Bezier curve by a piecewise linear curve (simply by choosing many parameter values 0 = t_0 < t_1 < ... < t_n = 1 and connecting the values in these parameters by line segments). Now it is easy to compute the entire length (Pythagoras Theorem is your friend) as well as the middle point (walk along the piecewise linear curve the prescribed length). The more points you sample, the more precise you will be and the more time your computation will take, so there is a trade-off. Of course, you can use a more complicated numerical scheme for the approximation but that is beyond the scope of the answer.
The second possibility is to restrict yourself to a subclass of Bezier curves that will let you do what you want. These are called Pythagorean-Hodograph (shortly PH) curves. They have the extremely useful property that there exists a polynomial sigma(t) such that
This means that you can compute the integral above and search for the correct value of u. However, everything comes at a price and the price here is that you will have less freedom, where to put the control points (for me as a mathematician, a cubic Bézier curve has four control points; computer graphics people often speak of "handles" so you might have to translate into your terminology). For the cubic case you can find the conditions on slide 15 of this seminar talk by Vito Vitrih.
Denote:
the control points,
;
then the Bézier curve is a PH curve if and only if
.
It is up to you to figure out, if you can enforce this condition in your situation or if it is too restrictive for your application.

Distance between broken line and experimental values

I need to evaluate a set of broken lines (red line), returned by a forward model, and select the one best fitting a set of experimental values (green dots); example in the image below. Do you have any suggestion on how to calculate the distance of the points from the broken line?
A decent (I think) approach would be to compute the sum of square distances from the fit to the points.
Assuming that you describe the links between the red dots as straight lines, then you can do it by
Computing the equation of each line between red points.
Compute distances between green points and red points. Assign the line from (1.) between closest 2 red points to each green dot.
Compute distance between each green dot and corresponding (2.) line.
score=sum(sqrt(distances))
If you want interpolate between your red points with something other than linear you may need to find a bit fancier maths, but the process should be the same.

Polar 2D Interpolation

Say we are creating a calibration lookup table for a device, shown in the plot below. The theta represents different phase values, and the r represents different magnitude values. The calibration setpoints are shown in blue circles, and are taken at every N degrees of phase and N values of magnitude. For every setpoint, we measure the actual device output and obtain the red coordinates, which describe the resulting phase and magnitude. Thus for every blue setpoint, we observe the device outputting red points.
The question now is, I want to set the device to a value of the green circle with orange ring. How do I calculate what the setpoint should be (green circle) to set the device to in order to obtain green/orange on the output?
The issue I am having is that for every 2D setpoint (mag, phase), the resultant data is 2D (mag, phase). In addition, magnitude and phase are not independent variables (fixing phase and changing only magnitude, the resulting phase output does change).
So what basic math/logic should I use to perform the necessary interpolation?
How about treating this like a registration problem. For example, you could use an affine transformation as the model between the measured and calibrated points? For each cell (i.e., the 4 blue points in your figure) compute a least squares estimate of the affine transformation between the blue and red points. Then for new points apply the corresponding transformation to get the green point you want. Here and here are some SO questions that discuss this. In addition, you might consider estimating and applying the transformation directly in magnitude/phase space.

MATLAB flip definition of angles (or alternative angular metric)

I am doing work on symmetric images where I would like to define a symmetric (polar) coordinate space. Basically for the left image, I want 0 degrees to be defined along the right horizontal axis (as is the default). However, for the right image, I want 0 degrees to be defined along the left horizontal axis.
I know a phase shift of pi would do the trick. However, for comparison purposes, I am trying to keep the range of angles the same, [-pi : pi).
In the above color plot of the rotations in an object, note that they are both defined in the same direction. Ideally I'd like to see the colors of the right object flipped across its vertical axis.
I should note that these angles are calculated by taking the arctan(y/x) of the perimeter coordinates when measured from the centroid. Is there a different trig function that may result in the proper symmetry? I couldn't seem to come up with one while still claiming it was representative of direction.