Problem with equation built with RANDN() function

2 次查看(过去 30 天)
Hi , I have a problem with below program which actually using "randn(); function to update the values by using a for loop.
the problem is that the second part of equation ("i mean the equation---> pm(i,1)=pm(i,1)-(pm(i,1)/.005)+(sig*0.005*randn()); " ) which contains the randn() , isn't producing any values , all values in pm matrix are same !! (which shouldn't be)
I am in the middle of a project and it needs to be solved very soon , a prompt answer will be of great help.
a=(2*10^-9)/2; %particle diameter
n=1; %time step counter
ttc=0;
f=(6*1.82*10^-5*a);
sig=sqrt(2*1.38*10^-23*300*.005/f);
pm=zeros(30,1);
pm(:,1)=-.005;
% while()
for i=1:30
pm(i,1)=pm(i,1)-(pm(i,1)/.005)+(sig*0.005*randn());
end

回答(1 个)

Walter Roberson
Walter Roberson 2020-3-17
Give the command
format long g
and then look at your pm values again.
Your pm values are different, but they are the same to 4 decimal places and your default format short is only displaying 4 decimal places.
  2 个评论
Kaiser Ahmed Bhuiyan
Do in need to write this code at begining ?
or put it in the for loop ?
Walter Roberson
Walter Roberson 2020-3-17
You just need it once at the command line to prove to yourself that the pm values really are different. It does not affect the calculation in any way. It does not need to be present in your code at all, as long as you do not want to display the values.
If you do want to display the values, then you can use fprintf() or num2str() or compose() telling it exactly what output format you want.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by