loop in a loop
7 views (last 30 days)
Show older comments
Hello!
I want to repeat the inner loop 10 times but it doesn't work. How do I solve that?
(Part of a bigger project)
hold on;
for k=1:10
for i=1:n-1
y(i+1)=y(i)+f(y(i))*dt+g(y(i)).*(B(i+1)-B(i));
end
plot(t,y)
end
10 Comments
Torsten
on 25 Feb 2022
B = sqrt(dt)*randn(n,1)
instead of
for i=2:n
b = b + sqrt(dt)*randn(1);
B = [B;b];
end
Otherwise, the B in the inner loop will still be unchanged.
Answers (2)
Voss
on 25 Feb 2022
figure();
n = 100;
t = linspace(0,1,n);
dt = t(2);
f = @some_function;
g = @some_function;
B = randn(1,100);
hold on;
for k=1:10
y = zeros(1,n); % (re-)initialize y to be all zeros for each plot
for i=1:n-1
y(i+1)=y(i)+f(y(i))*dt+g(y(i)).*(B(i+1)-B(i));
end
plot(t,y);
end
function out = some_function(in);
out = in+randn(size(in));
end
See Also
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!