applying for loop on variables

8 views (last 30 days)
Jaehoon Bang
Jaehoon Bang on 2 Jun 2021
Answered: Rik on 2 Jun 2021
for i=1:100
x(i+1)=x(i)+Ka*(Xf-x(i))-Kr*(1/rho_r1(i)-1/ro)/(rho_r1(i)^3)*(Xo1-x(i))-Kr*(1/rho_r2(i)-1/ro)/(rho_r2(i)^3)*(Xo2-x(i))-Kr*(1/rho_r3(i)-1/ro)/(rho_r3(i)^3)*(Xo3-x(i));
y(i+1)=y(i)+Ka*(Yf-y(i))-Kr*(1/rho_r1(i)-1/ro)/(rho_r1(i)^3)*(Yo1-y(i))-Kr*(1/rho_r2(i)-1/ro)/(rho_r2(i)^3)*(Yo2-y(i))-Kr*(1/rho_r3(i)-1/ro)/(rho_r3(i)^3)*(Yo3-y(i));
end
can anyone tell me how to apply for loop on variable "roh_r1&r2&r3"?
i tried to use 'eval', but failed
this is rho function.
rho_r1(1)=sqrt((Yo1-y(1))^2+(Xo1-x(1))^2);
rho_r2(1)=sqrt((Yo2-y(1))^2+(Xo2-x(1))^2);
rho_r3(1)=sqrt((Yo3-y(1))^2+(Xo3-x(1))^2);

Answers (1)

Rik
Rik on 2 Jun 2021
You know how to do indexing, so why did you make the decision to store data in a variable name?
for k=1:3
rho_r{k}(1)=sqrt((Yo{k}-y(1))^2+(Xo{k}-x(1))^2);
% ^^^ ^^^ ^^^
end

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!