How to show the color changing while comparing with grid size

Hi, there.
I am a little confused about some technical issues about color changing while doing the plotting.
When I change the the value of h, the plotting does not change the color but it shows some speicific values with color bar.
What I want to do is to see the color change with contour method when I change the h value, but not really sure how to do with this : (
Here is the sample coding about Finite Difference Method for Poisson Equation.
clear;clc;close all;
% Reference: https://www.youtube.com/watch?v=gkqt9dwc8Bo
P1 = 2; % degree celcus
P2 = -4; % degree celcus
X = 2; % metres [m] % length of the x dimensions
Y = 0.5; % metres [m] % length of the y dimensions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
h = 0.005; %%%% Grid Size value % this is the one need to discuss with plotting color changing.%%%%
dx = h; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dy = h;
nx = X/h + 1; % Number of grid points in x
ny = Y/h + 1; % Number of grid points in y
x = linspace(0,X,nx); % Vector of grid points in x
y = linspace(0,Y,ny); % Vector of grid points in y
% Initialize matrices
N = (nx-2)*(ny-2); % Number of unknowns
M = sparse(N,N); % N rows, N columns
B = sparse(N,1); % N rows, 1 columns
%% Interior points
for i = 2 : nx-1
for j = 2 : ny-1
n = i+(j-1)*nx; % convert ij grid to the nth grid point
M(n,n) = -4; % main diagonal
M(n,n-1) = 1; % diagonal to the left
M(n,n+1) = 1; % diagonal to the right
M(n,n-nx) = 1; % Far off diagonal to the left
M(n,n+nx) = 1; % Far off fiagonal to the right
B(n,1) = P1*exp(-(x(i)-1)^2/0.1-((y(j)-0.5)^2/0.05))+...
+P2*exp(-(x(i)-2)^2/0.1-((y(j)-0.5)^2/0.05)); % source term
end
end
%% Boundary condition Left BC
i = 1;
for j = 1:ny
n = i+(j-1)*nx;
M(n,n)=1;
B(n,1) = 0;
end
%% Boundary condition Right BC
i = nx;
for j = 1:ny
n = i+(j-1)*nx;
M(n,n)=1;
B(n,1) = 0;
end
%% Boundary condition Bottom BC
j = 1;
for i = 1:nx
n = i+(j-1)*nx;
M(n,n) = 1;
B(n,1) = 0;
end
%% Boundary condition Top BC
j = ny;
for i = 1:nx
n = i+(j-1)*nx;
M(n,n) = 1;
B(n,1) = 0;
end
% Solve for te potential
phi_vector = M\B;
for i = 1:nx
for j =1:ny
n = i+(j-1)*nx;
phi(i,j) = phi_vector(n);
end
end
contourf(x,y,phi.');
shading interp;
title('Temperature');
xlabel('x length [m]');
ylabel('height [m]'),colorbar;

8 Commenti

Why do you expect the color to change with the grid resolution? The color of contourf only depends on the value of the third input 'phi'
So is that okay just describe the change of grid value as the contour lines changing and the color bar's range changing?
No, the change of the grid will not change the colorbar. The range of colorbar will still remain the same. To change the range of colorbar, you need to change the 'phi' somehow. For example, try
contourf(x,y,(phi.').^2);
Yes, you're right, but the range is changing while doing
Here is the plotting diagram shown while running.
This is the diagram when h = 0.1
The figure 2 shows the h = 0.01
Ah! Yes, you are right. I didn't notice that 'h' is also being used in the calculation of phi. In that case, the range will change depending on the value of 'h'.
Are you trying to change the colors then? From blue-yellow to something else?
What I want to do is to compare those diagram with a specific color range. But I am not really sure how to do this. : (
If I understand correctly, you want to map all the contour plots to the same color range. Say color always changes from 0 to 1 for all value of h. Right?
Yep, that's what I mean for.

Accedi per commentare.

Risposte (1)

You can use rescale() function. Change the contourf() line to this
contourf(x,y,rescale(phi.', 0, 1));

Categorie

Scopri di più su Surfaces, Volumes, and Polygons in Centro assistenza e File Exchange

Richiesto:

il 30 Mag 2020

Risposto:

il 30 Mag 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by