How to plot multiple curves on the same screen for different values of "b" with bvp4c solver?

2 ビュー (過去 30 日間)
I am trying to draw multiple plots for multiple "b" values on a the same screen. The code is here. Any kind of help would be great.
function solution
b=1;
R=2;
pp=[1; 2; 3];
for i=1:numel(pp)
b=pp(i);
end
dYdX = @(X,Y) [Y(2); Y(3); (R/3*Y(2)).*(-2*Y(1).*Y(3)+Y(2)^.2)];
res = @(ya,yb) [ya(1)-b; ya(2); yb(2)];
SolYinit = bvpinit(linspace(0,100,1000),[0; 0; 0;]);
Fsol = bvp4c(dYdX, res, SolYinit);
X1 = Fsol.x;
Y1 = Fsol.y;
figure (1)
plot(X1, Y1(2,:), 'LineWidth', 2);
hold on

採用された回答

Star Strider
Star Strider 2022 年 9 月 12 日
The problem is that the initial conditions are uniformly zero, so any change in ‘b’ will not be evident since the entire result will be zero otherwise. Also, the entire code needs to be inside the loop.
The ‘res’ function will inherit the value of ‘b from the workspace, so it does not have to be passed as an extra parameter, however it could be. It would then be:
res = @(ya,yb,b) [ya(1)-b; ya(2); yb(2)];
and the call too it in the bvp4c call would then be:
Fsol = bvp4c(dYdX, @(ya,yb)res(ya,yb,b), SolYinit);
That also works.
Try this —
b=1;
R=2;
pp=[1; 2; 3];
for i=1:numel(pp)
b=pp(i);
dYdX = @(X,Y) [Y(2); Y(3); (R/3*Y(2)).*(-2*Y(1).*Y(3)+Y(2)^.2)];
res = @(ya,yb) [ya(1)-b; ya(2); yb(2)];
SolYinit = bvpinit(linspace(0,100,1000),[0; 0; 0]+eps);
Fsol = bvp4c(dYdX, res, SolYinit);
X1{i} = Fsol.x;
Y1{i} = Fsol.y;
end
figure (1)
hold on
for k = 1:numel(pp)
plot(X1{k}, Y1{k}(2,:), 'LineWidth', 2, 'DisplayName',sprintf('p = %d',pp(k)))
end
hold off
legend('Location','best')
The initial conditions need to be something other than zero in order that the output not be uniformly zero, so I added eps to nudge them away from zero.
.
  4 件のコメント
Ke Le
Ke Le 2022 年 9 月 12 日
Agree. Thank you again.
Star Strider
Star Strider 2022 年 9 月 13 日
As always, my pleasure!

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

その他の回答 (1 件)

Torsten
Torsten 2022 年 9 月 12 日
The solution will be
Y1 = b, Y2 = Y3 = 0
Is it that what you want ?

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by