Adding elements by elements

7 views (last 30 days)
Offroad Jeep
Offroad Jeep on 6 Oct 2015
Edited: Offroad Jeep on 6 Oct 2015
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.

Answers (1)

Sebastian Castro
Sebastian Castro on 6 Oct 2015
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 Comment
Offroad Jeep
Offroad Jeep on 6 Oct 2015
Edited: Offroad Jeep on 6 Oct 2015
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!