How to plot the diffusion curve for different timesteps?

14 views (last 30 days)
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

Accepted Answer

William Rose
William Rose on 3 Dec 2022
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
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
hold on;
Good luck!

More Answers (1)

Torsten on 3 Dec 2022
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
where x is the 1 x nx row vector of spatial coordinates where the diffusion curve is computed.





Community Treasure Hunt

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

Start Hunting!

Translated by