Save data from while loop

조회 수: 6 (최근 30일)
Anonymous User
Anonymous User 2019년 7월 5일
편집: madhan ravi 2019년 7월 5일
How I can save x,y,z data from each irritation and build a matrix ? So I can use them for future work ...
t=0;
while t<100
v=pi/10;
r=6;
dt=0.1;
w=v/r;
t=t+dt;
theta=pi/2;
x=r*sin(theta-w*t)
y=r*cos(theta-w*t)
z=sqrt(10^2-x^2-y^2)
end

채택된 답변

per isakson
per isakson 2019년 7월 5일
편집: per isakson 2019년 7월 5일
Your script overwrites x,y,z for each iteration. You need to assign vectors of appropriate length to x,y,z , (preallocate) before running the loop. Try
t=0;
ix=0;
v=pi/10;
r=6;
dt=0.1;
w=v/r;
x = nan(round(100/dt),1);
y = x;
z = x;
while t<100
ix = ix + 1;
t=t+dt;
x(ix)=r*sin(theta-w*t);
y(ix)=r*cos(theta-w*t);
z(ix)=sqrt(10^2-x^2-y^2);
end
Just to learn how to use a loop, despite it is not neccessary in your example.

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 7월 5일
편집: madhan ravi 2019년 7월 5일
You don’t need a loop , simply define t as
t = 0:dt:100;
doc while % to know more about the loop
doc for
and use element wise operation ( https://in.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html ) meaning * to .* and ^ to .^

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by