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!