applying for loop on variables

1 次查看(过去 30 天)
Jaehoon Bang
Jaehoon Bang 2021-6-2
回答: Rik 2021-6-2
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);

回答(1 个)

Rik
Rik 2021-6-2
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

类别

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