In the following sample. The result of the graphic is one point What is wrong here please

1 回表示 (過去 30 日間)
zina shadidi
zina shadidi 2018 年 3 月 17 日
コメント済み: Image Analyst 2018 年 3 月 18 日
energy = energy + deltaE;
for A = 1:nPart
th=A*L;
end
plot(energy,A,'b*')
xlabel('energy')
ylabel('( th)')
end
  3 件のコメント
Stephen23
Stephen23 2018 年 3 月 18 日
編集済み: Image Analyst 2018 年 3 月 18 日
zina shadidi's "Answer" moved here:
thank you Image Analyst and Walter Roberson
nPart = 100; % Number of particles
density = 0.85; % Density of particles
maxDr = 0.1;
printFreq = 100;
nSteps = 500;
function [coords, L] = initCubicGrid(nPart,density)
coords = zeros(3,nPart);
L = (nPart/density)^(1.0/3);
nCube = 2;
while (nCube^3 < nPart)
nCube = nCube + 1;
end
index = [0,0,0]';
for part=1:nPart
coords(:,part) = (index+[0.5,0.5,0.5]')*(L/nCube);
index(1) = index(1) + 1;
if (index(1) == nCube)
index(1) = 0;
index(2) = index(2) + 1;
if (index(2) == nCube)
index(2) = 0;
index(3) = index(3) + 1;
end
end
end
end
energy = LJ_Energy(coords,L);
for step = 1:nSteps
if (mod(step,printFreq)==0)
step;
end
for i=1:nPart
rTrial = coords(:,i) + maxDr*(rand(3,1)-0.5);
rTrial = PBC3D(rTrial,L);
deltaE = LJ_EnergyChange(coords,rTrial,i,L);
if (rand < exp(-beta*deltaE))
coords(:,i) = rTrial;
energy = energy + deltaE;
% A= number of layers, th=thin film thickness
for A = 1:nPart
th=A*L;
figure;
plot(th,energy, 'b*'); % Plot the calculated solution
ylabel('energy');
xlabel('th');
end
end
end
end
Image Analyst
Image Analyst 2018 年 3 月 18 日
Where do you call initCubicGrid() and what do you pass in for nPart and density?

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 3 月 17 日
Each iteration of the loop, you are overwriting all of th.
But that does not matter because you do not use the value of th later.
When you use a for loop, after the loop is done, the loop variable is left at the last value it had within the loop, which would be a scalar in this case (and in most cases.) We cannot tell whether energy is a scalar or a vector, but A will be a scalar at the time of the plot()
  2 件のコメント
zina shadidi
zina shadidi 2018 年 3 月 17 日
Its th in this line plot(energy,th,'b*') sorry it was a Typing error (just here) However, I have the same problem
Image Analyst
Image Analyst 2018 年 3 月 17 日
Put these lines before that and tell us what it says
whos energy
whos th

サインインしてコメントする。


Image Analyst
Image Analyst 2018 年 3 月 17 日
Maybe you meant
for A = 1:nPart
th(A) = A * L;
end
plot(energy, th,'b*-');
grid on;
assuming both energy and th are both vectors. If energy is a single number, you'd get an error trying to plot th against it.
But we can't tell because you left out most of the code. Attach complete code if you want a complete answer.

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by