Problem with equation built with RANDN() function

조회 수: 2 (최근 30일)
Kaiser Ahmed Bhuiyan
Kaiser Ahmed Bhuiyan 2020년 3월 17일
댓글: Walter Roberson 2020년 3월 17일
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
Kaiser Ahmed Bhuiyan 2020년 3월 17일
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