필터 지우기
필터 지우기

How to show the color changing while comparing with grid size

조회 수: 2 (최근 30일)
Michael Wang
Michael Wang 2020년 5월 30일
답변: Ameer Hamza 2020년 5월 30일
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
Ameer Hamza
Ameer Hamza 2020년 5월 30일
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?
Michael Wang
Michael Wang 2020년 5월 30일
Yep, that's what I mean for.

댓글을 달려면 로그인하십시오.

답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by