Alternative ways to perform the same task
显示 更早的评论
I have a code that I used to simulate the diffusion of a particle. I have done it using for loops. My next step will involve creating four simulations (4 particles) and adding interaction terms. I believe that writing a code to do that using for loops will be get messy pretty fast. My plan to make things easier to do involves creating an array, where the cells will be matrices for each simulation. I can then use cellfun to manipulate the entries of each cell. However, I am concerned with a beginning step. Looking at the first cell, how would I go about calculating the mean (sum column / #columns), of the first cell. Below is my code, showing how I did this with a for loop.
mu = 0;
kb=physconst('Boltzmann');
sig = sqrt((2*kb*300));
dt = 1e-3;
t = 0:dt:1;
P=2000;% Time vector
x = zeros(length(t),P); % Allocate output vector, set initial conditionrandn
rng('Default')
rng(1);
for p=1:P;
for i = 1:length(t)-1
x(i+1,p) = x(i,p)+sig*sqrt(dt)*randn;
end
end
figure;
plot(t,x);
X=x.';
A=mean(X);
B=mean(X.^2);
figure;
plot(t,A);
figure;
plot(t,B)
slope=polyfit(t,B,1)
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!