# Looking for visualization method to present finite difference numerical solution in 2D

10 views (last 30 days)
Kaveh Amini on 6 Mar 2022
Answered: Fabio Freschi on 7 Mar 2022
I am looking for a method to visualize the results of finite diference numerical solution in 2D. I used to use 'surf' and 'colormap' and I rotated the view to see it in 2D, but the problem with surf is that 'surf' visualizes the values at the nodes while I want to show the values for blocks (each block is built by 4 notes). One thing to consider is that if there are n grid nodes along x-axis, there will be only (n-1) grid blocks along x-axis.
I don't think 'image' works for my purpose either, because my grid block sizes are not uniform and I would prefer to show the grids as well.
Thanks a lot for your help!

Fabio Freschi on 7 Mar 2022
You may use the low-level patch function. Since I don't have your data, please find below a self-consistent example showing the difference between a plot of node-based function and an element-based function (obtained by aveaging the nod values).
The construction of the connectivity matrix may seem complex, but I should be simple to understand if you look at the matrix nodMap and then the corresponding T matrix.
clear variables, close all
% discretization params
nx = 5;
ny = 5;
% my data
x = linspace(0,1,nx);
y = linspace(0,1,ny);
[X,Y] = meshgrid(x,y);
P = [X(:) Y(:)];
% node map
nodMap = reshape(1:nx*ny,nx,ny);
% connectivity (4 nodes defining a rectangle)
T = zeros((nx-1)*(ny-1),4);
T(:,1) = reshape(nodMap(1:nx-1,1:ny-1),(nx-1)*(ny-1),1);
T(:,2) = reshape(nodMap(2:nx,1:ny-1),(nx-1)*(ny-1),1);
T(:,3) = reshape(nodMap(2:nx,2:ny),(nx-1)*(ny-1),1);
T(:,4) = reshape(nodMap(1:nx-1,2:ny),(nx-1)*(ny-1),1);
% my node-based function to plot f = x^2+y^2
fNod = P(:,1).^2+P(:,2).^2;
% my element-based function
fEle = sum(fNod(T),2)/size(T,2);
% plot node-based function
figure, patch('Vertices',P,'Faces',T,'FaceVertexCData',fNod,'EdgeColor','none','FaceColor','interp');
colorbar % plot element-based function
figure, patch('Vertices',P,'Faces',T,'faceVertexCData',fEle,'EdgeColor','none','FaceColor','flat');
colorbar ### Categories

Find more on Colormaps 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!