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 个评论

请先登录,再进行评论。

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by