How to plot the diffusion curve for different timesteps?

조회 수: 15 (최근 30일)
Emily
Emily 2022년 12월 3일
댓글: Emily 2022년 12월 6일
I would like to plot the diffusion curve produced in the for loop at different time steps, for example every 500 timesteps. I would like these all plotted on the same graph. What is the best way to do this? I have been plotting plot(C_new) for the final curve.
This is my for loop:
for t = 1:n_timesteps
function
end

채택된 답변

William Rose
William Rose 2022년 12월 3일
I assume this is 1D diffusion, and you want to plot concentration versus position at t=0, t=500, t=1000, ...
Before you cna plot it, you need to compute it. What is your plan for that?
I recommend you create a 2D array to contain the concentration values at each time and at each position. For example:
M=201; %number of spatial locations; adjust as desired
N=2501; %number of time steps; adjust as desired
dt=1; dx=1; %time step size, spatial step size; adjust as desired
c=zeros(N,M); %allocate array
Each row of c() is the concentraitons at all the locations, at a single time. Each column of c() is the concentration one location, at all the different times.
First you will want to initialize row 1 to be the initial concentration distribution. Then you will use one or more for loops to calculate the concentration at the subsequent times.
Once you have computed c() for all the positions and rows, you plot the results from the rows (i.e. at the times) you select. For example,
x=dx*(1:M); %vector of location values
plot(x,c(1,:),'r',x,c(500,:),'--r',x,c(1000,:),'g',x,c(1500,:),'--g',x,c(2000,:),'b',x,c(2500,:),'--b');
legend('step 1','501','1001','1501','2001','2501');
You could also do the plotting with a for loop, as long as you include "hold on" so the previous plots don;t get erased by the new ones.
for i=1:500:2501
plot(x,c(i,:),'--r');
hold on;
end
Good luck!

추가 답변 (1개)

Torsten
Torsten 2022년 12월 3일
Save C in the loop for the values of t where you want to plot the curves. You should get a matrix like C(ntimes,nx).
Then outside the loop plot C as
plot(x,C)
where x is the 1 x nx row vector of spatial coordinates where the diffusion curve is computed.

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by