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.
![](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!