applying for loop on variables

조회 수: 4 (최근 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