How do I solve this looping problem?

3 ビュー (過去 30 日間)
Deen Halis
Deen Halis 2016 年 4 月 3 日
コメント済み: jgg 2016 年 4 月 6 日
Hello all! Please help me solve this. This is a dumbed down problem to what I’m having. I have a time interval of ‘t = 0:ts:N’. For each time step, I need to go through these formulas ‘qt1, F1, FF1, F2 FF2’. Values of FF1 and FF2 are cumulative sums of F1 and F2 respectively. For each time step, I need to do iteration until the condition ‘er’ is met, then I output the result. For each iteration where the condition is not met, I need to take qt1 = FF2 to start calculation of F1, FF1, F2 and FF2 again until the condition is met.
Thanks is advance
tic
clc
clear
w = 20;
p1 = 15;
T1 = 2*pi/w;
ts = T1/25;
N = 0.1;
m = 10;
q0 = 10;
G = 0.05;
n1 = G*p1^2/w;
v1 = n1/p1^2;
pp1 = p1*(1-(v1*p1)^2)^0.5;
t = 0:ts:N;
while 1
for i = 1:length(t)
qt1 = q0*sin(w*t(i));
F1 = ts*qt1.*sin(pp1*t(i)).*exp(n1*t(i))%%(a)
FF1 = cumsum(F1);% not working
F2 = ts*qt1.*sin(pp1*t(i+1)).*exp(n1*t(i+1))% (b)%Attempted to access t(9); index out of bounds because numel(t)=8.
FF2 = cumsum(F2);% not working
er = (FF2 - FF1)/FF2
end
if er <= 0.05
break
else
qt1 = FF2 % need to take this to calculate F1 and F2, at (a) and (b) above
end
FF2
end
toc
[EDITED, Jan, Code formatted]
  3 件のコメント
Deen Halis
Deen Halis 2016 年 4 月 6 日
Thanks Jan, so how do i correct this error?
jgg
jgg 2016 年 4 月 6 日
The question is, what should this line do:
F2 = ts*qt1.*sin(pp1*t(i+1)).*exp(n1*t(i+1))%
When i = length(t)? If you can tell that, you can correct this code. The issue is arising because i iterates up to the end of t then tries to look one step farther, which is impossible.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by