How can I find the coefficients of the 2D interpolated function?

조회 수: 3 (최근 30일)
SPerera
SPerera 2024년 8월 1일
댓글: SPerera 2024년 8월 1일
My function is . I used following code to interpolation. How can I find the coefficients of the interpolated function ?
Eta = load('Eta.txt');
t = 0:5:(8.25*60); % time
x = [0,3750,7500,8000]; % distance
[X,T] = meshgrid(x,t);
% interpolation
[xq,tq] = meshgrid(0:2:8000,0:(8.25*60)) ;
Eta_intp = interp2(X,T,eta,xq,tq,'spline');

채택된 답변

Animesh
Animesh 2024년 8월 1일
편집: Animesh 2024년 8월 1일
To find the coefficients of the 2-D interpolated function, we can use polynomial fitting on the interpolated data. You can use the "polyfit" function to do so in MATLAB.
Here is something you can try:
  1. Flatten the matrices, since "polyfit" works only with vectors.
% Flatten the matrices
xq_flat = xq(:);
tq_flat = tq(:);
E_intp_flat = E_intp(:);
2. Perform polynomial fitting:
% Perform polynomial fitting
% Here, we assume a polynomial of degree 2 in both x and t
degree = 2;
p = polyfitn([xq_flat, tq_flat], E_intp_flat, degree);
disp(p.Coefficients);
  댓글 수: 1
SPerera
SPerera 2024년 8월 1일
@Animesh Thank you so much. I got it. After this I checked the difference between original data and polynomial data using following code. There is a difference between these two. Without adjusting the Polynomial Degree, how can I reduce this difference?
% Define a function for evaluating the polynomial
eta_poly = @(xq,tq) polyvaln(p,[xq,tq]);
% Evaluate the polynomial at the original grid points
eta_poly_values = arrayfun(@(x_val, t_val) eta_poly(x_val, t_val), X, T);
% Compare the polynomial values with the original eta data
% Calculate the difference
difference = eta_poly_values - eta;

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by