Mesh Grid of a 3d array
Show older comments
I want to plot the error when I compare a numerical approximation and an exact solution. I want to use the matlab mesh function (mesh(X,Y,Z)) to visualise the error approximation. With the mesh function the Z component has to be a matrix. My numerical approximation and exact solution are all vectors but they were 3d arrays convected to vectors. How do I use the mesh function to plot the error given that the Z component has to be a matrix? I tried the code below but I am not sure if it's right. Any help will be greatly appreciated. In the code xx (numerical approximation) and yy(exact solution) are vectors of dimension xd^3 and zz is the difference between xx and yy.
xd = nthroot(length(zz),3);
yd = xd;
u = zeros(xd,yd,1);
v = zeros(xd,yd,1);
xstart = -3.0;
xend = 3.0;
h = (xend-xstart)/(xd-1);
x = zeros(xd,1);
y = zeros(xd,1);
for i = 1:xd
x(i) = xstart + (i-1)*h;
y(i) = xstart + (i-1)*h;
end
err = 0.0;
for i = 1:xd
for j = 1:yd
u(j,i,1) = xx((j-1)*xd+i);
v(j,i,1) = yy((j-1)*xd+i);
if abs(u(j,i,1)-v(j,i,1)) > err %error
err=abs(u(j,i,1)-v(j,i,1));
end
end
end
mesh(x,y,u-v);
xlabel('x');
ylabel('y');
zlabel('maxerror');
Accepted Answer
More Answers (0)
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!