Need Help fixing a for loop for iteration

1 次查看(过去 30 天)
Hi,
I have the following code but it is not giving me the values I want. The loop is not working. Basically, I want the variables J1, J2, J3, J4 and J5 calculated from each coordinates of Mx and My which is used to calculate P in an existing Simscape file. The first value of Length is 500 (with an increment of 10) and Prod_Length = J5-Length.
If P > 6000, then the next values of J1, J2, J3, J4, J5, Length and Prod_Length are calculated and P checked. This process is to go on until P < 6000.
The issue I am having is - it's only calculating my P based on the first values of Length and Prod_Length - without considering the increment of 10 for Length.
#1 for Mx = 50:50:500;
#2 for My = 50:50:400;
#3 %
#4 J1 = [sqrt((Mx-100)^2+(My-20)^2)];
#5 J2 = [sqrt((Mx-20)^2+(My-150)^2)];
#6 J3 = [sqrt((Mx-50)^2+(My-450)^2)];
#7 J4 = [sqrt((Mx-150)^2+(My-450)^2)];
#8 J5 = [sqrt((Mx-7000)^2+(My-200)^2)];
#9 %
#10 %%I have a simscape file that runs with the mfile to check the value of P
#11 while P > 6000
#12 Length = Length + 10
#13 Prod_Length = J5 - Length;
#14 end
#15 end
#16 end
Thank you
  1 个评论
Star Strider
Star Strider 2015-4-28
Fun Dan’s additional Comment (moved from an Answer Comment):
The mfile is linked to an external Simulink file which calculates 'P' based on the different values of J1, J2, J3 J4 and J5. My issues is getting the loop to work for the stated condition. Thanks

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2015-4-28
In the code you posted, you don’t seem to be changing the value of ‘P’.
  4 个评论
Fun Dan
Fun Dan 2015-4-28
The mfile is linked to an external Simulink file which calculates 'P' based on the different values of J1, J2, J3 J4 and J5. My issues is getting the loop to work for the stated condition. Thanks
Star Strider
Star Strider 2015-4-28
Simulink is not an area of my expertise. I added Simulink to the ‘Products’ tags, and your latest Comment as a Comment to your original Quesiton. I will delete my Answer (and all the subsequent Comments to it) in a few minutes, since I can’t help.

请先登录,再进行评论。

类别

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