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 件のコメント

Voss
Voss 2022 年 2 月 25 日
When you say "it doesn't work", what does it do? That is, what is the behavior you see vs the behavior you expect? Do you get an error message? If so, please copy and paste it here.
Also, it may be helpful if you tell us the initial values of your variables n, y, f, dt, g, B and t.
Hugo B
Hugo B 2022 年 2 月 25 日
It does work for the loop with i, and it gives me 1 plot. But I want it to be repeated 10 times so I get 10 plots in 1 (it is Brownian motion included in earlier steps).
Image Analyst
Image Analyst 2022 年 2 月 25 日
@Hugo B I think you overlooked his last sentence. If you want the inner loop repeated 10 times, then n must be 11.
If you get only one plot, not one for each k, then all your plots must be identical and they're simmply overlapping each other. If you do this do you see multiple plots
subplot(3, 4, k)
plot(t, y, '-', 'LineWidth', 2);
grid on;
Hugo B
Hugo B 2022 年 2 月 25 日
This is still not what I want. I want it in the same plot, like multiple lines. Like stock market prediction with several outcomes. It is not possible that they are overlapping since there is a stochastical (random) variable included in an earlier step.
Image Analyst
Image Analyst 2022 年 2 月 25 日
But when you did it like I showed (just humor me for a moment), were all the plots different? Or did they look the same. If they looked different, then when you did it your way, did the plot look just like the final one? If so, then somehow hold got turned off.
Hugo B
Hugo B 2022 年 2 月 25 日
They all looked the same.. so maybe that is the problem? How do I fix that?
Torsten
Torsten 2022 年 2 月 25 日
編集済み: Torsten 2022 年 2 月 25 日
First you should change B in the outer loop. And maybe a different value for y(1).
Hugo B
Hugo B 2022 年 2 月 25 日
Ok, now I have moved the outer loop to include the change in B. It still shows the same one plot with only one line (and the stared one). But it also gives the error message that t an Y is not the same length, which seems to happen when B is changed within the outer loop.
for k=1:10
for i=2:n
b = b + sqrt(dt)*randn(1);
B = [B;b];
end
Y = y_exact(B);
hold on;
y = zeros(1,n);
y(1) = exp(1);
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)
plot(t,Y,'*');
end
Torsten
Torsten 2022 年 2 月 25 日
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.
Hugo B
Hugo B 2022 年 2 月 25 日
編集済み: Stephen23 2022 年 2 月 25 日
Answer in comment from Torsten works. Thank you everyone!

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

回答 (2 件)

Torsten
Torsten 2022 年 2 月 25 日
編集済み: Torsten 2022 年 2 月 25 日

0 投票

The outer loop is superfluous - so I deleted it.
dt = ...;
B = ...;
f = @(y) ...;
g = @(y) ...;
t = ...;
y = zeros(size(t));
y(1) = ...;
for i = 2:numel(t)
y(i) = y(i-1) + f(y(i-1))*dt + g(y(i-1))*(B(i)-B(i-1));
end
plot(t,y)

1 件のコメント

Hugo B
Hugo B 2022 年 2 月 25 日
I want the outer loop to be able to repeat the inner loop 10 times. Since Brownian motion is included in earlier steps, this loop will change every time, which I want and to be able to plot it.

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

Voss
Voss 2022 年 2 月 25 日

0 投票

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

1 件のコメント

Hugo B
Hugo B 2022 年 2 月 25 日
I think this could work but now the inital values does not work. How do I add this again? initial for y I mean

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2022 年 2 月 25 日

編集済み:

2022 年 2 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by