Why am I getting gaps in my data when I attempt to interpolate my curve?
Mostrar comentarios más antiguos
I am trying to make finner steps (x) for my data file attached. I am using matlab's interp function. Upon doing so, I get the curve attached with a big gap in the data. Any idea what I am doing wrong?
xInterp=linspace(Dist(1),Dist(end),2500);%also tried less points (500->1000) and it was worse
yInterp=interp1(Dist,Power,xInterp,'spline');
figure
plot(xInterp,yInterp)
set(gca, 'YScale', 'log')
Respuesta aceptada
Más respuestas (2)
Umar
el 5 de Jul. de 2024
Editada: Walter Roberson
el 22 de Jul. de 2024
Hi Omar,
The 'spline' interpolation method in Matlab can sometimes introduce artifacts or gaps in the data, especially when the data points are not evenly distributed. This can lead to unexpected results in the interpolated curve. To address the gap issue in the interpolated data, we can switch to a different interpolation method that is more suitable for the given data. One alternative method that can provide a smoother interpolation without gaps is the 'pchip' method. Here is the updated code with the interpolation method changed to 'pchip':
xInterp = linspace(Dist(1), Dist(end), 2500);
yInterp = interp1(Dist, Power, xInterp, 'pchip');
figure
plot(xInterp, yInterp)
set(gca, 'YScale', 'log')
I am not sure if you have tried pchip function but it should help resolve your problem. For more information regarding this function, please refer to
Let me know if I can provide further assistance.
1 comentario
John D'Errico
el 5 de Jul. de 2024
Note this is not truly a question of the gaps in the data, or the distribution of the data. It is a question of the use of a spline to interpolate data with a rapid transition, thus a region with a very high slope to a region with virtually no slope at all.
But pchip is indeed a viable solution, because pchip is designed to handle those ituations more stably. HOWEVER, note that pchip is a somewhat lower order interpolant. It will not always be as smooth looking if you look very carefully at the result.
Umar
el 6 de Jul. de 2024
0 votos
Hi Omar,
You mentioned the following in your post about Thanks for the detailed reponse. So the sharp transitions caused spline to overshoot in the negative direction->yielding negative values and thus matlab ignored them when log plotting them as they were undefined. Are there other interpolation functions or different approaches you recommend that are better than pchip? I see your other comment about this being lower order and thus less accurate in some scenarios
First, I would like to tell John that he did a good job to tackle the problem. Since the purpose of this platform is to share knowledge and brainstorming ideas to help out each other. My response to your comments are listed below.
When dealing with data exhibiting rapid transitions, especially those causing overshooting and negative values during interpolation, it is crucial to explore alternative interpolation techniques that can handle such complexities more effectively than PCHIP. While PCHIP is a stable option, its lower order nature may lead to less visually smooth results, prompting the search for more suitable methods.
One alternative interpolation approach worth considering is the Cubic Spline Interpolation. Cubic splines are piecewise-defined polynomials that maintain smoothness and continuity up to the second derivative at the interpolation points. This property makes them particularly useful for capturing rapid transitions while ensuring a higher degree of smoothness compared to PCHIP. By utilizing cubic splines, you can potentially mitigate issues related to overshooting and negative values, providing a more accurate representation of the data.
Another technique to explore is Akima Interpolation, which is known for its ability to handle sharp transitions effectively. Akima interpolation calculates the interpolating polynomial using a weighted average of slopes at neighboring points, allowing for a more robust interpolation process in the presence of rapid changes. This method can offer improved accuracy and stability when dealing with data sets characterized by sudden transitions and high slopes.
Furthermore, Shape-Preserving Piecewise Cubic Interpolation methods such as PCHIPEND or PCHIPLIN can be considered as alternatives to traditional PCHIP. These variations aim to enhance the smoothness and accuracy of interpolation results, especially in regions with steep gradients or rapid changes. By incorporating shape-preserving constraints, these methods can better handle the challenges posed by data exhibiting sharp transitions, ensuring more reliable interpolation outcomes.
Now, John also shared an alternative approach about quadratic interpolant and I do agree that this type of interpolant offers the benefits of being twice differentiable and locally monotone, but there are some drawbacks to be aware of:
Implementing a rational quadratic interpolant can be more complex compared to simpler interpolation methods like linear or cubic splines. The additional parameters and constraints involved in ensuring twice differentiability and local monotonicity can increase the implementation.
The computational cost of evaluating a rational quadratic interpolant may be higher than that of lower-order interpolants. The additional calculations required to maintain the desired properties can lead to increased computational overhead.
Rational quadratic interpolants can be more sensitive to perturbations in the input data compared to lower-order interpolants. Small changes in the data points can have a more significant impact on the interpolant's behavior, potentially leading to unexpected results.
While rational quadratic interpolants offer certain properties like twice differentiability and local monotonicity, they may lack the flexibility to capture complex variations in the data. Higher-order interpolants can sometimes oversmooth the data, leading to a loss of detail or accuracy in the interpolation.
Due to the nature of rational functions, numerical stability issues may arise when using rational quadratic interpolants, especially when dealing with data that contains noise or outliers. Careful consideration and numerical analysis are required to ensure stability in the interpolation process.
As Andrew Singer, professor of electrical and computer engineering once said quoted verbatim, “The art of debugging is figuring out what you really told your program to do rather than what you thought you told it to do.”
So, as humans, not all of us are perfect, we all make mistakes and learn from it. That is quite evident from the bugs found in the software and upgrades to getting these bugs fixed. Again, John and myself are still here to assist you further. Hope, learning from these suggestions and guidance provided, you should be able to accomplish your goal now.
1 comentario
SnooptheEngineer
el 22 de Jul. de 2024
Categorías
Más información sobre Interpolation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






