Piecewise function graph help

2 ビュー (過去 30 日間)
Alyssa
Alyssa 2023 年 11 月 14 日
編集済み: Les Beckham 2023 年 11 月 14 日
I'm trying to graph a piecewise function and I already sketched know what the graph should look like. The last segment of the graph should be horizontal and linear from r=120.4 until r=200. Any help would be appreciated!
L=200; d=10; s=30; r1_max=22.36; r2_max=120.4;
r=0:200;
for i=1:length(r)
if r(i) < d
F==0
elseif (r(i) >=d) & (r(i) <=r1_max)
F=(s+2.*sqrt(r.^2-d^2))./(L-s);
elseif (r(i) >= r1_max) & (r(i) <=r2_max)
F=(sqrt(r.^2-d^2)+(L/4))./(L-s);
else
F==1;
end
end
Unrecognized function or variable 'F'.
plot(r,F)

回答 (4 件)

Torsten
Torsten 2023 年 11 月 14 日
編集済み: Torsten 2023 年 11 月 14 日
L=200; d=10; s=30; r1_max=22.36; r2_max=120.4;
r=0:200;
for i=1:length(r)
if r(i) < d
F(i)=0;
elseif (r(i) >=d) & (r(i) <r1_max)
F(i)=(s+2.*sqrt(r(i).^2-d^2))./(L-s);
elseif (r(i) >= r1_max) & (r(i) <r2_max)
F(i)=(sqrt(r(i).^2-d^2)+(L/4))./(L-s);
else
F(i)=1;
end
end
plot(r,F)

Voss
Voss 2023 年 11 月 14 日
編集済み: Voss 2023 年 11 月 14 日
L=200; d=10; s=30; r1_max=22.36; r2_max=120.4;
r=0:200;
F = zeros(size(r));
for i = 1:numel(r)
if r(i) < d
F(i)=0;
elseif r(i) <= r1_max
F(i)=(s+2.*sqrt(r(i).^2-d^2))./(L-s);
elseif r(i) <= r2_max
F(i)=(sqrt(r(i).^2-d^2)+(L/4))./(L-s);
else
F(i)=1;
end
end
plot(r,F)
axis padded

madhan ravi
madhan ravi 2023 年 11 月 14 日
F = (r < d) * 0 + ((r >= d) & (r <= r1_max)) .* ((s+2.*sqrt(r.^2-d^2))./(L-s)) + ((r >= r1_max) & (r <= r2_max)) .* ((sqrt(r.^2-d^2)+(L/4))./(L-s)) + (r > r2_max);
plot(r,F)

Les Beckham
Les Beckham 2023 年 11 月 14 日
編集済み: Les Beckham 2023 年 11 月 14 日
L = 200;
d = 10;
s = 30;
r1_max = 22.36;
r2_max = 120.4;
r = 0:200;
F = zeros(size(r));
idx = r >= d & r < r1_max; % find indices for the first "piece"
% Note: idx will be true where the condition is met
F(idx) = (s + 2.*sqrt(r(idx).^2 -d^2)) ./ (L-s);
idx = r >= r1_max & r < r2_max; % find indices for second "piece"
F(idx) = (sqrt(r(idx) .^2 - d^2) + (L/4)) ./ (L-s);
idx = r >= r2_max; % find indices for third "piece"
F(idx) = 1;
plot(r, F, '.-')
grid on
xlabel 'r'
ylabel 'F(r)'

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by