How do I use a quintic spline function (spapi)?
8 ビュー (過去 30 日間)
古いコメントを表示
I'd like to perform the quintic spline to my data and know that Matlab function's "spapi" is able to use it. However I tried many times to run the examples sources in the explanation in Matlab homepage, I got the errors and don't know the reason. I'm not English native, so I might be unable to understand the explanations well.
Anyone tell me the way to use the quincit spline in spapi. Sample data: x=[0:0.5:100*0.5]; y=[0:100];
x2=[0:5:100*0.5]; y2=[0:10:100];
I'd like to perform that y2 correspond to y.
Matlab homepage's example: 1. sp = spapi(4,[x x(s)], [sin(x) cos(x(s))]);
2. x = -2:.5:2; y = -1:.25:1; [xx, yy] = ndgrid(x,y); z = exp(-(xx.^2+yy.^2)); sp = spapi({3,4},{x,y},z); fnplt(sp)
2 件のコメント
Abdul Mannan
2015 年 9 月 8 日
Unfortunately, there is no way out you can do it like yy = spline(x,y,xx) command. Using spline command you can have yy value at a give interpolant xx but spapi does not do that. I am also finding a solution for this purpose.
Jon F
2018 年 4 月 17 日
編集済み: Jon F
2018 年 4 月 17 日
(I know this is some time after the event, but perhaps this will still be helpful for someone else.)
If I understand what you are wanting to do, then the function you need is fnval. Example code:
x=[0:0.5:100*0.5]; y=[0:100]; x2=[0:5:100*0.5]; y2=[0:10:100];
figure; plot(x,y,'bx'); hold on
sp1=spapi(5,x,y); fnplt(sp1,'k-')
y2i=fnval(sp1,x2); plot(x2,y2i,'ro')
I agree that Matlab's documentation for spapi/spline could be more helpful on this - I'm not sure why a simple example like this doesn't appear in it.
回答 (1 件)
Aditya
2025 年 8 月 20 日 6:23
Hi Kazumichi,
To perform quintic spline interpolation with your data in MATLAB using the spapi function, you first need to decide which points you want to interpolate. In your case, you have a set of coarse points x2 and corresponding values y2, and you want to fit a quintic spline (degree 5, so order 6) through these points. You can then evaluate this spline at a finer set of points x to get a smooth curve. To do this, use spapi(6, x2, y2) to create the spline, then use fnval to evaluate the spline at your desired points. Finally, you can plot both the original data points and the interpolated curve for visualization. Make sure that the lengths of x2 and y2 match, and that x2 is strictly increasing.
Following is an example code:
x = 0:0.5:50; % Fine grid for evaluation
x2 = 0:5:50; % Coarse grid (data points)
y2 = 0:10:100; % Values at coarse grid
% Fit a quintic spline (order 6) through the data points
sp = spapi(6, x2, y2);
% Evaluate the spline at fine grid points
y_fit = fnval(sp, x);
% Plot the original data points and the fitted spline
plot(x2, y2, 'ro', 'MarkerSize', 10, 'DisplayName', 'Data Points');
hold on;
plot(x, y_fit, 'b-', 'LineWidth', 2, 'DisplayName', 'Quintic Spline');
legend show
xlabel('x');
ylabel('y');
title('Quintic Spline Interpolation using spapi');
hold off;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spline Postprocessing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!