Code error with parameter

10 ビュー (過去 30 日間)
Ellen Brown
Ellen Brown 2020 年 1 月 4 日
コメント済み: Star Strider 2020 年 1 月 4 日
I'm trying to adapt my code below
rg = 0.01;
rl = 0.01;
s1 = [-1:0.000001:5];
x = ((s1-rg-rl)+sqrt((rg+rl-s1).^2+4.*s1.*rg))./(2.*s1);

採用された回答

Star Strider
Star Strider 2020 年 1 月 4 日
I am not certain what you want, however it most likely depends on how you define ‘t’.
Example —
rg = 0.01;
rl = 0.01;
s1 = [-1:0.000001:5];
x = ((s1-rg-rl)+sqrt((rg+rl-s1).^2+4.*s1.*rg))./(2.*s1);
t1 = 0:numel(s1)-1; % Define ‘t1’, Interval = 1 sec
t2 = (0:numel(s1)-1)*5; % Define ‘t2’, Interval = 5 sec
figure
plot(t1, x)
hold on
plot(t2, x)
hold off
grid
legend('\Delta t = 1 s', '\Delta t = 5 s', 'Location','E')
  8 件のコメント
Ellen Brown
Ellen Brown 2020 年 1 月 4 日
That's exactly what I want! Thank you so much! I'm being a bit cheeky now but how would I get a vector of those s_val values? I'd like to plot xs against the s_val values
Star Strider
Star Strider 2020 年 1 月 4 日
As always, my pleasure!
Not cheeky at all! That’s a perfectly legitimate question.
To store the ‘s_val’ values (without re-writing the rest of the loop):
xs = zeros(size(U));
s_val_vct = xs;
for k = 1: numel(U)
s_val = s1(k)*(rem(k,2)==0) + s2(k)*(rem(k,2)==1); % Even-Odd Values of ‘k’ Select ‘s’
xs(k) = x(s_val);
s_val_vct(k) = s_val;
end
Then to plot them:
figure
plot(s_val_vct, xs, 'p')
grid
xlabel('s')
ylabel('x')
It’s best to plot them with markers, unlless you want to use the sort function to sort ‘xs’ and ‘s_val_vct’ and then plot with lines, because ‘s_val’ alternates between poisitive and negative values, and that would create a confusing and disordered plot. (I like plotting with pentagrams. Choose the marker you want.)

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by