PP form for the reverse function (not inverse function)
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello friends!
Consider a pp form for a set of (x,y) data points. Imagine now that we wish to find the pp form for the (x,1/y) data
points using the pp form for the (x,y) data. I am wondering whether matlab has a function which can calculate this?
Of course, I can simply calculate it indirectly but I am not sure if this is the best way to perform such a calculation
in terms of both speed and acuracy. For instance, the following do the job for the function cos(x) over [-pi/2 pi/2]:
x=-pi:0.5:pi/2;
y=cos(x);
pp=spline(x,y);
mesh=linspace(x(1),x(end),200);
pp_inv=spline(mesh,1./ppval(pp,mesh));
I seriously doubt whther my approach is efficient? (I feel not)
Any idea?
Thanks in advance!
Babak
0 commentaires
Réponses (1)
Aditya
le 16 Jan 2024
Your approach to finding the piecewise polynomial (pp) form for the reciprocal of a function using MATLAB's `spline` function is generally correct, but it has some potential issues regarding efficiency and accuracy.
The problem with evaluating `1./ppval(pp, mesh)` is that it does not take into account the original spline's structure and may introduce inaccuracies, especially near zeros of the original function `y`. Additionally, the evaluation of the spline at a dense mesh followed by another spline fitting can be computationally intensive.
Unfortunately, MATLAB does not have a built-in function that directly converts a pp form spline for `(x, y)` data into a pp form spline for `(x, 1/y)` data. However, you can achieve a more efficient and potentially more accurate result by working with the pp form directly.
Here's an approach that modifies the coefficients of the original pp spline to create a new pp spline for the reciprocal:
1. Extract the pp form of the original spline.
2. Invert the polynomial coefficients analytically to obtain the coefficients for the reciprocal function.
3. Create a new pp form with the inverted coefficients.
This method can be complex because inverting a polynomial is not straightforward and may not always be possible for higher-order polynomials. However, for simple cases where `y` is a low-order polynomial, you might be able to perform the inversion analytically.
Here's an example of how you might approach this problem using MATLAB's built-in functions to extract and manipulate the pp form:
% Original data points
x = -pi:0.5:pi/2;
y = cos(x);
% Fit a spline to the original data
pp = spline(x, y);
% Evaluate the spline on a mesh
mesh = linspace(x(1), x(end), 200);
y_vals = ppval(pp, mesh);
% Invert the y-values
inv_y_vals = 1 ./ y_vals;
% Fit a new spline to the inverted y-values
pp_inv = spline(mesh, inv_y_vals);
% Evaluate the inverted spline on a new mesh
new_mesh = linspace(x(1), x(end), 200);
inv_y_spline_vals = ppval(pp_inv, new_mesh);
% Plot the original and inverted spline
plot(mesh, y_vals, 'b-', new_mesh, inv_y_spline_vals, 'r--');
legend('Original Spline', 'Inverted Spline');
In this example, the inversion is done numerically by evaluating the original spline on a mesh and then fitting a new spline to the reciprocal of those values. This approach is similar to what you've done, but it's important to note that if `y` approaches zero, the reciprocal will tend to infinity, which could lead to inaccuracies or undefined behavior.
0 commentaires
Voir également
Catégories
En savoir plus sur Spline Postprocessing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!