MATLAB Answers

How to show the color changing while comparing with grid size

1 view (last 30 days)
Michael Wang
Michael Wang on 30 May 2020
Answered: Ameer Hamza on 30 May 2020
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 Comments

Show 5 older comments
Michael Wang
Michael Wang on 30 May 2020
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. : (
Ameer Hamza
Ameer Hamza on 30 May 2020
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?

Sign in to comment.

Answers (1)

Ameer Hamza
Ameer Hamza on 30 May 2020
You can use rescale() function. Change the contourf() line to this
contourf(x,y,rescale(phi.', 0, 1));

  0 Comments

Sign in to comment.


Translated by