exact 2D Chebyshev Interpolation

12 Ansichten (letzte 30 Tage)
Marko
Marko am 13 Nov. 2023
Beantwortet: Karan Singh am 13 Dez. 2023
Hello community,
with pseudospectral methods it is possible to obtain a high resoltution solution with a small mesh.
I added a matlabfile which calculate the gradient of f(x,y).
The plot is generated with contourf and since my grid for the numeric solution is about (N+1)x(N+1) the output is miserable.
I have added the analytic derivative and plotted it also.
My question is how could i write a code that generate a autonomous function.
This function needs (N+1)^2 coeffcients, which has to be fitted. (is is based on pascals triangel
e.g.: N=2 the function should have the following structure:
function f = fAuto(x,y)
f = a0 ...
+ a1*x + a2*y ...
+ a3*x^2 + a4*x*y + a5*y^2 ...
+ a6*x^3 + a7*x^2*y + a8*x*y^2 + a9y^3;
end
Of course the real problem has a variable N between 8 and 32.
Is there a straight forward way?
Best regards,
MJ
Added File:
function exact2DInterpolation
% generate grid
N=2;
x = cheby(N)
y = cheby(N)
[X,Y]=meshgrid(x,y);
% create first and second derivative in 1 dimension
D1x = Du(x);
D1y = Du(y);
% testdata
[Z, ZxAna, ZyAna] = fAnalytic(X,Y);
% generate numerical result
ZxNum = reshape(kron(D1x,diag(ones(N+1,1)))*Z(:),N+1,[]);
ZyNum = reshape(kron(diag(ones(N+1,1)),D1y)*Z(:),N+1,[]);
% generate high resolution output
N2 = 65;
x2 = linspace(-1,1,N2);
y2 = linspace(-1,1,N2);
[X2,Y2]=meshgrid(x2,y2);
[~, ZxAna2, ZyAna2] = fAnalytic(X2,Y2);
figure(1)
clf
subplot(3,2,1)
contourf(X,Y,ZxAna)
title('F_x analytic at collocation points' )
hold on
plot(X(:),Y(:),'ro')
subplot(3,2,2)
contourf(X,Y,ZyAna)
title('F_y analytic at collocation points' )
subplot(3,2,3)
contourf(X,Y,ZxNum)
title('F_x numeric at collocation points' )
subplot(3,2,4)
contourf(X,Y,ZyNum)
title('F_x numeric at collocation points' )
subplot(3,2,5)
contourf(X2,Y2,ZxAna2)
title('F_x analytic high resolution' )
hold on
plot(X(:),Y(:),'ro')
subplot(3,2,6)
contourf(X2,Y2,ZyAna2)
title('F_y analytic high resolution' )
end
function D = Du(x)
N = numel(x)-1;
c = [2; ones(N-1,1); 2].*(-1).^(0:N)';
xx = repmat(x,1,N+1);
Dx = xx-xx';
D = (c*(1./c)')./(Dx+(speye(N+1)));
D = D-diag(sum(D,2));
end
function x = cheby(N)
x = -cos((0:N)*pi/N)';
end
function [Z, Zx, Zy] = fAnalytic(X,Y)
Z = (X-0.3).^2.*Y.*(Y-1);
Zx= 2*(X-0.3).*Y.*(Y-1);
Zy= (X-0.3).^2.*(2*Y-1);
end

Antworten (1)

Karan Singh
Karan Singh am 13 Dez. 2023
Hi Marco,
From what I understand you are trying to generate an autonomous function that fits a polynomial to a set of data points. The polynomial has a variable number of coefficients, which depends on the size of the input data. You have provided a MATLAB code that generates the input data and plots the results and asking for a way to generate an autonomous function that can be used to evaluate the polynomial at any point, without having to recompute the polynomial coefficients every time.
You can use the “polyfit function to fit the coefficients of the polynomial.
% generate grid
N = 2;
x = cheby(N);
y = cheby(N);
[X,Y] = meshgrid(x,y);
% create first and second derivative in 1 dimension
D1x = Du(x);
D1y = Du(y);
% testdata
[Z, ZxAna, ZyAna] = fAnalytic(X,Y);
% generate numerical result
ZxNum = reshape(kron(D1x,diag(ones(N+1,1)))*Z(:),N+1,[]);
ZyNum = reshape(kron(diag(ones(N+1,1)),D1y)*Z(:),N+1,[]);
% generate high resolution output
N2 = 65;
x2 = linspace(-1,1,N2);
y2 = linspace(-1,1,N2);
[X2,Y2] = meshgrid(x2,y2);
[~, ZxAna2, ZyAna2] = fAnalytic(X2,Y2);
% fit coefficients of polynomial
p = polyfit([X(:), Y(:)], ZxNum(:), (N+1)^2-1);
fAuto = @(x,y) polyval(p, [x(:), y(:)]);
% evaluate autonomous function
ZxAuto = reshape(fAuto(X,Y), N+1, []);
% plot results
figure(2)
clf
subplot(2,2,1)
contourf(X,Y,ZxAna)
title('F_x analytic at collocation points' )
hold on
plot(X(:),Y(:),'ro')
subplot(2,2,2)
contourf(X,Y,ZxNum)
title('F_x numeric at collocation points' )
subplot(2,2,3)
contourf(X,Y,ZxAuto)
title('F_x autonomous at collocation points' )
subplot(2,2,4)
contourf(X2,Y2,ZxAna2)
title('F_x analytic high resolution' )
hold on
plot(X(:),Y(:),'ro')
Attached below are the documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati

Kategorien

Mehr zu Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by