How to plot multiple curves on the same screen for different values of "b" with bvp4c solver?
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Star Strider
on 12 Sep 2022
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 Comments
More Answers (1)
See Also
Categories
Find more on Downloads in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!