I am getting Error: Error using * Arguments must be 2-D, or at least one argument must be scalar.
28 views (last 30 days)
Show older comments
I have a 3D function where I am testing taking derivative along x,y, and z direction. My issue is that taking derivative wrt z is giving an error
clearvars; clc; close all;
Nx = 4;
Ny = 4;
Nz = 4;
%-----
Lx = 2*pi; %8; %128;
Ly = 2*pi;
% Set the number of grid points
%Set-up grids:
x = (0:Nx-1)/Nx*2*pi;
y = (0:Ny-1)/Ny*2*pi;
kx = (2*pi/Lx)*([0:Nx/2-1 , -Nx/2:-1]);
ky = (2*pi/Ly)*([0:Ny/2-1 , -Ny/2:-1]);
zgl = -cos(pi*(0:Ny)/Ny)'; %Gauss-Lobatto chebyshev points
[X,Y,Z] = meshgrid(x,y,zgl);
%Chebyshev matrix for Gauss-Lobatto
VGL = cos(acos(zgl(:))*(0:Nz));
dVGL = diag(1./sqrt(1-zgl.^2))*sin(acos(zgl)*(0:Nz))*diag(0:Nz);
dVGL(1,:) = (-1).^(1:Nz+1).*(0:Nz).^2;
dVGL(Nz+1,:) = (0:Nz).^2;
%Diferentiation matrix for Gauss-Lobatto points
Dgl = dVGL/VGL;
D = Dgl; %first-order derivative matrix
%------------------
ubar = Z.^2 .* sin( (2*pi / Lx) * X) .* sin( (2*pi / Ly) * Y) ; %Y^2 * sin( (2*pi / Lx) * x);
uh = fft(fft(ubar,[],2),[],1); %fft along x and y only
duhdxk = derivk(uh, kx); %works
duhdx = real(ifft(ifft(duhdxk,[],2),[],1));
exactDx = (2*pi)/(Lx)* Z.^2 .* cos( (2*pi / Lx) * X) .* sin( (2*pi / Ly) * Y);
DEx = exactDx -duhdx;
% y derivative
duhdyk = derivk(uh, ky'); %works
duhdy = real(ifft(ifft(duhdyk,[],2),[],1));
exactDy = (2*pi)/(Ly)* Z.^2 .* sin( (2*pi / Lx) * X) .* cos( (2*pi / Ly) * Y);
DEy = exactDy - duhdy;
%%
%z derivative
for e = 1:Nz
duhdzk(:,:,e) = D * uh(:,:,e); % ERROR: D is 5x5 size and uh is 4x4x5 size
end
exactDz = 2 .* Z .* sin( (2*pi / Lx) * X) .* sin( (2*pi / Ly) * Y);
I am aware the error comes from multiplying D an uh with the incorrect sizes so I tried creating a for loop to change the uh size to 4x5 but it's not working.
Full error:
Error using *
Arguments must be 2-D, or at least one argument must be scalar. Use TIMES (.*) for elementwise
multiplication, or use PAGEMTIMES to apply matrix multiplication to the pages of N-D arrays.
Error in FourierCheby3D (line 77)
duhdzk(:,:,e) = D * uh(:,:,e);
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Bessel functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!