Adding elements by elements
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!