# If I want to have x as a 3 element vector say [2,3,2], how to I write a loop to iterate over each value in the array for x?

조회 수: 2(최근 30일)
답변: Jan 2018년 4월 30일
n = n + 1;
t(n+1) = t(n) + deltat;
%x(t+deltat) = x(t) + v(t).*deltat
x(n+1) = x(n) + v(n).*deltat;
%a(t) = agravity + fdrag(t)./m
atotal(n) = agravity(n) + fdrag(n)./m;
%vdrag(t) = v(t) - vwind
vdrag(n) = v(n) - vwind(n);
%Magfdrag = norm(fdrag)
magfdrag = norm(fdrag);
%Magfdrag(t) = 0.5*p*Cd*A.*(norm(vdrag)).^2
magfdrag(n) = 0.5*p*Cd*A.*(norm(vdrag)).^2;
%fdrag = norm(fdrag(t)).*(-vdrag(t)/norm(vdrag(t)))
fdrag = norm(fdrag(n)).*((-vdrag(n))/norm(vdrag(n)));
%v(t+deltat) = v(t) + a(t).*deltat
v(n+1) = v(n) + atotal(n).*deltat;

댓글을 달려면 로그인하십시오.

### 채택된 답변

Jan 2018년 4월 30일
x0 = [2,3,2];
t = zeros(1000, 1);
x = zeros(1000, 3);
v = zeros(1000, 3);
x(1, :) = x0;
n = 1;
while <Your Condition to stop the loop>
n = n + 1;
t(n+1) = t(n) + deltat;
x(n+1, :) = x(n, :) + v(n, :) .* deltat;
...
v(n+1, :) = v(n, :) + atotal(n, :) .* deltat;
end

댓글을 달려면 로그인하십시오.

### 범주

Find more on Matrix Indexing 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!

Translated by