why the loop is giving repeated array

조회 수: 1 (최근 30일)
Offroad Jeep
Offroad Jeep 2016년 2월 4일
편집: Stephen23 2016년 2월 5일
Attach please find the code . it is repeatedly giving the value where i only want one time random dtheta and it should not repeat the value for 25 times... Thanks

채택된 답변

Guillaume
Guillaume 2016년 2월 4일
You need to learn to debug your program. There are many things wrong with your code:
First problem,
%...
B = 2;
%...
for i = 1:length(B)
length(B) is 1, the i loop is therefore only executed once.
2nd problem,
theta = 0
theta_initial = ones(nrows) * theta
theta is initialised as scalar, theta_initial as vector. Later,:
%...
theta_inital = theta;
theta_initial goes from being a vector to being a scalar. I'm fairly certain that's not the intention.
3rd problem,
%...
for i = ...
for j = ...
theta_new = theta + dtheta(j);
%bunch of code that only modifies thetha_initial and zeeman_old
%none of it depends on i
end
After the j loop, you have in effect
theta_new = theta + dtheta(100)
regardless of what happens in the loop. Hence the loop is not needed. This is followed by
magnetization_new(i) = magnetization * cosd(theta_new);
So, in effect
magnetization_new(i) = magnetization * cosd(theta + dtheta(100));
for all i.
Reread your code, and if it is not enough for you to spot the mistake step through it using the debugger.
  댓글 수: 2
Offroad Jeep
Offroad Jeep 2016년 2월 4일
편집: Offroad Jeep 2016년 2월 4일
Opps, I attached the wrong file.........now i have attached the correct one in which i need correction
Guillaume
Guillaume 2016년 2월 4일
No file attached, I'm afraid

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by