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

2 views (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 12 Jun 2024
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
.

More Answers (1)

Aquatris
Aquatris on 12 Jun 2024
Edited: Aquatris on 12 Jun 2024
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

Categories

Find more on Discrete Data Plots 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!