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 CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by