I am getting this warning "Matrix is singular to working precision." and my surf plot is not showing.
1 次查看(过去 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1714211/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1714216/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1714221/image.png)
0 个评论
采纳的回答
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));
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
.
0 个评论
更多回答(1 个)
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!