Adding elements by elements

조회 수: 2 (최근 30일)
Offroad Jeep
Offroad Jeep 2015년 10월 6일
편집: Offroad Jeep 2015년 10월 6일
Hi to all, Attach please find the code for correction
Initial values are given. Random values of dtheta are generated I want to add theta + dtheta so that if the first value of dtheta is added to theta = 0 (it is the initial value), it should check if the value is greater than zero it should accept otherwise reject. In the next step it should save the new value of theta_new in theta and then take the next value of dtheta......... if sum of theta+dtheta is greater then the new value of theta it should accept otherwise it should reject the value and so on........ I want to calculate it for 10 values of B. when 10 values for theta are obtained reject the remaining dtheta and show the accepted values. I hope I have explained satisfactorily.

답변 (1개)

Sebastian Castro
Sebastian Castro 2015년 10월 6일
So... what is the question? Your code runs fine. You should really describe your problem instead of asking everyone to "please correct it". Just a tip for future questions, if you would like people to answer them :)
Now, here is my best attempt. I'm guessing you want to keep track of your variable theta over time? If so, you should have theta be a vector instead of a scalar, like so:
theta = zeros(1,B_steps + 1) % angle between magnetization and magnetic field
for i = 1:B_steps
for j = 1:theta_nmc
if dtheta(j) > 0
theta(j+1) = theta(j) + dtheta(j)
else
theta(j+1) = theta(j) % it should be equal to previous theta
end
end
end
plot(theta)
There are probably ways to do this without one or both for-loops altogether, but this is a start.
- Sebastian
  댓글 수: 1
Offroad Jeep
Offroad Jeep 2015년 10월 6일
편집: Offroad Jeep 2015년 10월 6일
Ok thanks for advice. actually for every value of B (magnetic field) I want to get the value of theta. Random values for theta are generated. Actually I have 10 values of B for each B i want a value of new theta and when ten values are completed it should terminated. although I have generated 50 random values for dtheta which are negative as well as positive. RAZ

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by