Plotting for various values of the same parameter on the same plot

Asking here on how to plot "Y1(2,:)" for multiple "S" values on the same figure?. Consider values such as "S=0.1, 0.5, 1.1, 1.5" etc.
dYdX = @(X,Y) [Y(2); Y(3); S*Y(2).^2+Y(1).*Y(3)];
res = @(ya,yb) [ya(1)-1; ya(2); yb(2)];
SolYinit = bvpinit(linspace(0,5,50),[0; 0.2; 0.4]);
Fsol = bvp4c(dYdX, res, SolYinit);
X1 = Fsol.x;
Y1 = Fsol.y;
figure (1)
plot(X1, Y1(2,:), 'LineWidth', 2);
hold on

2 件のコメント

Ive J
Ive J 2021 年 8 月 28 日
hold on, have you tried hold on?
Ke Le
Ke Le 2021 年 8 月 28 日
@Ive J Yes, it doesn't show it there, however, I've tried it there. Even with "hold on", I am looking for help on plotting for various "S" values. Any help will be appreciated.

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

 採用された回答

Star Strider
Star Strider 2021 年 8 月 29 日
Set ‘S’ as a vector, add ‘S’ as an additional parameter to the ‘dYdX’ function, then iterate through the values of ‘S’.
Try this —
Sv = [0.1, 0.5, 1.1, 1.5];
dYdX = @(X,Y,S) [Y(2); Y(3); S*Y(2).^2+Y(1).*Y(3)];
res = @(ya,yb) [ya(1)-1; ya(2); yb(2)];
SolYinit = bvpinit(linspace(0,5,50),[0; 0.2; 0.4]);
for k = 1:numel(Sv)
S = Sv(k);
Fsol = bvp4c(@(X,Y)dYdX(X,Y,S), res, SolYinit);
X1{k} = Fsol.x;
Y1{k} = Fsol.y;
end
figure (1)
Nrsp = numel(Sv);
for k = 1:Nrsp
subplot(Nrsp,1,k)
plot(X1{k}, Y1{k}(2,:), 'LineWidth', 2)
grid
title(sprintf('S = %.1f',Sv(k)))
end
I opted for the subplots because the scales vary so significantly on the solutions that the details in the solution for ‘S(1)’ gets lost if they are all plotted on the same axes.
.

4 件のコメント

Ke Le
Ke Le 2021 年 8 月 29 日
That's really amazing sa far. Love to see it coming. Why I get the error message on my machine, though, I run in R2021a which is same as yours. Try to sort it out, kindly. Many thanks.
"Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in test (line 9)
X1{k} = Fsol.x;"
Star Strider
Star Strider 2021 年 8 月 29 日
The only reason I can think of that could have thrown that error is that you preallocated ‘X1’ and ‘Y1’ as matrices (rather than as cell arrays) prior to the loop, or have them defined as matrices (rather than as cell arrays) somewhere else earlier.
One way to avoid the error is to call them something else, such as ‘Xc’ and ‘Yc’ in the loop (and in the following plot loop), again providing that those variables have also not been previously defined as something else earlier in the code.
There could be other reasons, however that is the only situation that I have ever encountered a similar error in my experience. In any event, giving them unique names will likely solve it.
.
Ke Le
Ke Le 2021 年 8 月 30 日
Thanks
Star Strider
Star Strider 2021 年 8 月 30 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

その他の回答 (1 件)

the cyclist
the cyclist 2021 年 8 月 28 日
% Some data
x = 1:10;
% Create the figure window, use the hold command
% to prevent overwriting of axes
figure
hold on
% Plot line with different slope parameters
for s = 1:3
plot(x,s*x)
end

1 件のコメント

Ke Le
Ke Le 2021 年 8 月 28 日
@the cyclist How should we treat the above system for S values such as "-0.1, 0.5, 1.1, 1.5"?

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

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

質問済み:

2021 年 8 月 28 日

コメント済み:

2021 年 8 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by