필터 지우기
필터 지우기

How can I best compare the max error of b-spline interpolation?

조회 수: 3 (최근 30일)
Salvatore Bianco
Salvatore Bianco 2023년 6월 26일
답변: Sandeep 2023년 9월 5일
Hello, I am preparing a project for an exam. In a particular script, I am using various methods of cubic b-spline interpolation to best approximate a given function, say .
So i decided that, for each of the 5 methods, I'll use 20 sets of an increasing number of points, from say 50 to 500.
For every method, and for every set of points, I'll extract the maximum distance of the points in the partition of the interval (hmax) and the maximum distance of the spline curve to the exact curve (errmax).
This is the result I get:
Is it a good idea to plot (1/hmax, errmax), so five error curves, each of which contains 20 points? Is there a function in matlab that lets me visualize this better? Any suggestion? (The legend will be added later)
  댓글 수: 6
Salvatore Bianco
Salvatore Bianco 2023년 6월 26일
It is a function that, given a number of points, and a flag i =1, ..., 5, returns the max distance of the nodal points and the max distance of the spline to the given function in a sample of points. What I am looking for is a way to plot the errors in a way that can outline their convergence to zero, i. e. I want to visualize that graph better, and I don't know how.
Mathieu NOE
Mathieu NOE 2023년 6월 26일
I don't understand what your code is doing and also what is the meaning of
extract the maximum distance of the points in the partition of the interval (hmax)
I believe this should be a simple example of spline approximation of your fist test case, vs N points of sampling for the required x range.
you can build your solution on that :
%func:
% 1: sin(x)-sin(2x); [-pi, pi]
% 2: exp(x/2)*x; [-2, 2]
% 3: |x| + x/2 - x^2; [-2, 2]
% 4: 1/(1+x^2); [-5,5]
% 5: 1/(1+exp(x)); [-2 2]
N_tested = (50:50:500);
for ck = 1:numel(N_tested)
N = N_tested(ck);
x = linspace(-pi,pi,N);
y = sin(x)-sin(2*x);
% cubic spline interpolation
yi = interp1(x,y,x,'spline');
% add your own other secret spline interpolation function here (do a loop ?)
%figure(ck),plot(x,y,'-*',x,yi,'-*'); % for me
errmax(ck) = max(abs(y-yi)); % max error
end
% plot the rms error vs N
figure
plot(N_tested,errmax,'-*')

댓글을 달려면 로그인하십시오.

답변 (1개)

Sandeep
Sandeep 2023년 9월 5일
Hi Salvatore Bianco,
It is my understanding that you expect opinion on plotting five error curves each containing 20 points and suggestion of MATLAB function to visualize in a better way.
Yes, plotting (1/hmax, errmax) is a good approach to visualize the error curves for the different methods and sets of points. This plot will allow you to compare the performance of the methods and observe how the error changes with different numbers of points.
In MATLAB, you can use the plot function to create the error curves. To enhance the visualization, you may consider using a logarithmic scale for the x-axis (1/hmax) and the y-axis (errmax). This can be achieved using the semilogx function, which plots data with a logarithmic x-axis.
A Sample implementation to plot the error curves using semilogx in MATLAB is given below,
% Create a matrix to store the data points for each method
data = zeros(20, 5);
% Loop through each method and set of points
for method = 1:5
for numPoints = 50:50:500
% Calculate hmax and errmax for the current method and set of points
hmax = calculateHmax(method, numPoints);
errmax = calculateErrmax(method, numPoints);
% Store the values in the data matrix
data(numPoints/50, method) = errmax;
end
end
% Plot the error curves using semilogx
figure;
semilogx(1./data(:, 1), data(:, 1), 'r');
hold on;
semilogx(1./data(:, 2), data(:, 2), 'g');
semilogx(1./data(:, 3), data(:, 3), 'b');
semilogx(1./data(:, 4), data(:, 4), 'm');
semilogx(1./data(:, 5), data(:, 5), 'c');
hold off;
% Add labels and title to the plot
xlabel('1/hmax');
ylabel('errmax');
title('Error Curves for Cubic B-spline Interpolation Methods');
% Add a legend to identify the methods
legend('Method 1', 'Method 2', 'Method 3', 'Method 4', 'Method 5');
The Demonstration snippet assumes that you have already implemented the functions calculateHmax and calculateErrmax to compute the maximum distances. You can replace these functions with your own calculations based on the specific methods you are using.
The resulting plot will show the error curves for each method, with the x-axis representing 1/hmax and the y-axis representing errmax. The legend will help differentiate between the different methods.

카테고리

Help CenterFile Exchange에서 Splines에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by