필터 지우기
필터 지우기

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