Adding elements by elements
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!