I am getting this warning "Matrix is singular to working precision." and my surf plot is not showing.

3 次查看(过去 30 天)
I want to plot the 3D plot for the following function , where and . I tried ploting with the code attached, but i am not getting a surface plot.

采纳的回答

Star Strider
Star Strider 2024-6-12
You need to do element-wise opeerations in your ‘u’ function, so:
u = @(x,y)(-4*pi*exp(-5*pi*pi*0.5).*cos(2*pi*x).*sin(pi*y))./(2 + exp(-5*pi*pi*0.5).*sin(2*pi*x).*sin(pi*y));
instead of:
u = @(x,y)(-4*pi*exp(-5*pi*pi*0.5)*cos(2*pi*x)*sin(pi*y))/(2 + exp(-5*pi*pi*0.5)*sin(2*pi*x)*sin(pi*y));
See Array vs. Matrix Operations for details.
With that change, it works —
exact_u
function exact_u
%u = @(x,y)(-4*0.01*pi*exp(-5*pi*pi*(0.01)*0.5)*cos(2*pi*x)*sin(pi*y))/(2 + exp(-5*pi*pi*(0.01)*0.5)*sin(2*pi*x)*sin(pi*y));
u = @(x,y)(-4*pi*exp(-5*pi*pi*0.5).*cos(2*pi*x).*sin(pi*y))./(2 + exp(-5*pi*pi*0.5).*sin(2*pi*x).*sin(pi*y));
[X,Y]=meshgrid(0:0.1:1);
Z = u(X,Y);
figure
surf(X,Y,Z);
title('U(x,y)');
xlabel('x');
ylabel('y');
zlabel('u');
grid on
box on
%Interpolation
[X1,Y1] = meshgrid(0:0.05:1);
Z1 = interp2(X,Y,Z,X1,Y1);
figure
surf(X1,Y1,Z1)
title('U(x,y)');
xlabel('x');
ylabel('y');
zlabel('u');
grid on
box on
end
.

更多回答(1 个)

Aquatris
Aquatris 2024-6-12
编辑:Aquatris 2024-6-12
You are doing a matrix mutiplication when you call the u() function, since you call u function with matrix arguments X and Y. Then something happens (!) and your whole matrix because NaN
I think you did not mean to do a matrix multiplication in the u = @(x,y) function. So change it to element wise multiplication and division and it seems to work.
exact_u
function exact_u
%u = @(x,y)(-4*0.01*pi*exp(-5*pi*pi*(0.01)*0.5)*cos(2*pi*x)*sin(pi*y))/(2 + exp(-5*pi*pi*(0.01)*0.5)*sin(2*pi*x)*sin(pi*y));
u = @(x,y)(-4*pi*exp(-5*pi*pi*0.5).*cos(2*pi*x).*sin(pi*y))./(2 + exp(-5*pi*pi*0.5).*sin(2*pi*x).*sin(pi*y));
[X,Y]=meshgrid(0:0.1:1);
Z = u(X,Y);
figure
surf(X,Y,Z);
title('U(x,y)');
xlabel('x');
ylabel('y');
zlabel('u');
grid on
box on
%Interpolation
[X1,Y1] = meshgrid(0:0.05:1);
Z1 = interp2(X,Y,Z,X1,Y1);
figure
surf(X1,Y1,Z1)
title('U(x,y)');
xlabel('x');
ylabel('y');
zlabel('u');
grid on
box on
end

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by