Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Various errors whilst running a while loop.

조회 수: 5 (최근 30일)
Sam Potter
Sam Potter 2020년 3월 14일
마감: MATLAB Answer Bot 2021년 8월 20일
h(1)=100000; %initial Height
t(1)=0; %Initial Time
dt=0.005;
p(1)=(((h(1))/71)+1.4); %Air Density
g(1)=(40*10^7)/(6371+h(1))^2; %initial gravity
A=5; %Area
c=0.7; %Drag Coefficient
m=850; %Mass
Fd(1)=0.5*c*(p(1))*a(1)*(v(1))^2;
v(1)=59.29; %Initial velocity
a(1)=0.03535; %Initial Acceleration
i=1; %Loop Counter
while h(end)>=0
t(i+1)=t(i)+dt;
h(i+1)=100000-((v(i+1)*t(i+1))+(0.5*(a(i))*(t(i+1))^2));
p(i+1)=(((h(i+1))/71)+1.4);
Fd(i+1)=0.5*c*(p(i+1))*A*(v(i+1))^2;
g(i+1)=(40*10^7)/(6371+h(i+1))^2;
a(i+1)=g(i+1)-(Fd(i+1)/850);
v(i+1)=v(1)+(a(i+1)*t(i+1));
end
With this code I want to be able to plot graphs and get values for a and v. When I run it an error comes up either saying 'undefined function or variable a'. or 'undefined function or variable v'. One time I managed to stop these errors but I still got an error saying 'Index exceed number of array elements'. Any help would be appreciated.
  댓글 수: 6
Walter Roberson
Walter Roberson 2020년 3월 14일
h(i+1)=100000-((v(i+1)*t(i+1))+(0.5*(a(i))*(t(i+1))^2));
When i is 1, that tries to use v(1+1) and t(1+1) and a(1) . You assigned to t(1+1) in the previous line of code, so that exists, and a(1) exists . But v(2) does not exist yet, and will not exist until the end of the loop (and it relies upon the updated a(2) so you cannot just move it to earlier in the code.)
Sam Potter
Sam Potter 2020년 3월 14일
This is my current code now:
>> clear
>> h(1)=100000; %initial Height
t(1)=0; %Initial Time
dt=0.005;
p(1)=(((h(1))/71)+1.4); %Air Density
g(1)=(40*10^7)/(6371+h(1))^2; %initial gravity
A=5; %Area
c=0.7; %Drag Coefficient
m=850; %Mass
Fd(1)=0.5*c*(p(1))*a(1)*(v(1))^2;
v(1)=59.29; %Initial velocity
a(1)=0.03535; %Initial Acceleration
i=1; %Loop Counter
while h(end)>=0
t(i+1)=t(i)+dt;
h(i+1)=100000-((59.29*t(i+1))+(0.5*(a(i))*(t(i+1))^2));
p(i+1)=(((h(i+1))/71)+1.4);
Fd(i+1)=0.5*c*(p(i+1))*A*(v(i))^2;
g(i+1)=(40*10^7)/(6371+h(i+1))^2;
a(i+1)=g(i+1)-(Fd(i+1)/850);
v(i+1)=v(1)+(a(i+1)*t(i+1));
end
I have tried to make it so that everything in the loop is defined previously. However, for some reason my code doesnt save some of the initial variables (like u(1) and v(1) into the workspace?

답변 (1개)

Sriram Tadavarty
Sriram Tadavarty 2020년 3월 14일
  댓글 수: 1
Sam Potter
Sam Potter 2020년 3월 14일
Hi, because the code was different I thought it warrented a diiferent thread, apologies if not. I have replied to you in the other thread with my issue.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by