double forloop and plotting

1 回表示 (過去 30 日間)
Junhwi Mun
Junhwi Mun 2020 年 6 月 9 日
編集済み: madhan ravi 2020 年 6 月 12 日
Hi, I’m stuck on using ‘double forloop’
I got each ’all_K_db{i}’ corresponding to ‘e_1value’
and I want to get solutions and plot them in a (time, state x) graph.
As there are 5 values for ’all_K_db{i}’ and 2 values for ‘x=(x1,x2)’, 10 lines should exist on a plot. How can I show all the lines in one graph?
even if the code doesn't make any error, I can't check all(10lines) values of x=(x1,x2), I only see 2lines
%Figure2-2,data-based
e_1value=0.01:0.5:2.01;
numb_t = length(e_1value);%5
x1=[1; 1];
tspan = (1:2:15);
for tid=1:2:15
for i=1:numb_t
A= [0.2, 1.3; 0.1, 1.2];
B= [1; 2];
D= [0.45 0.45; 0.3 -0.3];
dxdt =@(t,x) A*x+ B*(all_K_db{i})*x+ D*x*(all_K_db{i})*x;
end
[t,x] = ode45(dxdt, tspan, x1);
end
  2 件のコメント
Junhwi Mun
Junhwi Mun 2020 年 6 月 9 日
I also used 'all_x{tid}=x;' right after [t,x] = ode45(dxdt, tspan, x1);, but every 'all_x{tid}=x' has the same values for (x1,x2)
Rik
Rik 2020 年 6 月 9 日
You are overwriting all your results in each loop.

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

採用された回答

Ayush Gupta
Ayush Gupta 2020 年 6 月 12 日
編集済み: madhan ravi 2020 年 6 月 12 日
The problem is here that you are overwriting values when you use
[t,x] = ode45(dxdt, tspan, x1);
As this line is outside of the second loop, only one dxdt value is returned and since the first/outside loop runs one time, it stores only 2 values and therefore you can see only 2 lines instead of 10. Correct version of this will be the following:
%Figure2-2, data-based
e_1value=0.01:0.5:2.01;
numb_t = length(e_1value);%5
x1=[1; 1];
tspan = (1:2:15);
for tid=1:2:15
for i=1:numb_t
A= [0.2, 1.3; 0.1, 1.2];
B= [1; 2];
D= [0.45 0.45; 0.3 -0.3];
dxdt =@(t,x) A*x+ B*(all_K_db{i})*x+ D*x*(all_K_db{i})*x;
[t,x] = ode45(dxdt, tspan, x1);
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by