Piecewise function graph help
2 views (last 30 days)
Show older comments
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
plot(r,F)
0 Comments
Answers (4)
Torsten
on 14 Nov 2023
Edited: Torsten
on 14 Nov 2023
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)
0 Comments
Voss
on 14 Nov 2023
Edited: Voss
on 14 Nov 2023
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
0 Comments
madhan ravi
on 14 Nov 2023
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)
0 Comments
Les Beckham
on 14 Nov 2023
Edited: Les Beckham
on 14 Nov 2023
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)'
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!