How do I solve this looping problem?

3 views (last 30 days)
Deen Halis
Deen Halis on 3 Apr 2016
Commented: jgg on 6 Apr 2016
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 Comments
Deen Halis
Deen Halis on 6 Apr 2016
Thanks Jan, so how do i correct this error?
jgg
jgg on 6 Apr 2016
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.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements 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!