Adding elements by elements

2 次查看(过去 30 天)
Offroad Jeep
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