Piecewise function graph help

2 Ansichten (letzte 30 Tage)
Alyssa
Alyssa am 14 Nov. 2023
Bearbeitet: Les Beckham am 14 Nov. 2023
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)

Antworten (4)

Torsten
Torsten am 14 Nov. 2023
Bearbeitet: Torsten am 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)

Voss
Voss am 14 Nov. 2023
Bearbeitet: Voss am 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

madhan ravi
madhan ravi am 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)

Les Beckham
Les Beckham am 14 Nov. 2023
Bearbeitet: Les Beckham am 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)'

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by