Jump Discontinuity Functions Open and Close Circle

24 visualizaciones (últimos 30 días)
Wal MathEngineering
Wal MathEngineering el 8 de Feb. de 2022
Editada: Walter Roberson el 9 de Feb. de 2022
Hello,
Attached below is the code.
I am trying to plot jump discontinuity functions. But I have diffuclties to plot the functions. Also, in my code I tried to show open and close circles. The reason why I need this is to use it in my research paper to introduce some limit defintions. Your help is greatly appreciated.
clc
clear
%%
x = linspace(-pi, pi);
% a piecewise discontinuous function can be plotted using a for loop
for i = 1:length(x)
X = x(i);
if x(i) <= 1
y = x.^2;
elseif x <= 2;
y = x.^3 - 5;
else
y = 5 - 2*x;
end
end
hold on
axis ([-10 10 -10 20])
plot([-8 8],[0 0], 'k-','markersize',2)
plot([0 0], [-10 15],'k-','markersize',2)
plot(X,y,'markersize',12)
title(' Graph of Piecewise Discontinuous f(x)')
legend('y = x^2', 'y = x^3-5','y = 5-2x')
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
grid on

Respuestas (1)

Turlough Hughes
Turlough Hughes el 8 de Feb. de 2022
Editada: Turlough Hughes el 8 de Feb. de 2022
My preferred approach is to use fplot as follows:
clc, clear
figure(), hold on
axis ([-10 10 -10 20])
fplot(@(x) x.^2, [-pi 1],'.-','MarkerSize',12)
hold on, fplot(@(x) x.^3 - 5, [1 2],'.-','MarkerSize',12)
fplot(@(x) 5 - 2*x, [2 pi],'.-','MarkerSize',12)
% Graph formatting
title('Graph of Piecewise Discontinuous f(x)')
legend('y = x^2', 'y = x^3-5','y = 5-2x')
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
grid on
If you need to you can also return the x and y values from fplot using [x,y] = fplot(___)
Another way you could do it:
clc, clear
x = linspace(-pi, pi);
% index for intervals
idxA = x<=1;
idxB = x>1 & x<=2;
idxC = x>2;
% solve equations
y = [x(idxA).^2, x(idxB).^3-5, 5-2*x(idxC)];
figure(), hold on
axis ([-10 10 -10 20])
plot(x(idxA), y(idxA),'.-','MarkerSize',12)
plot(x(idxB), y(idxB),'.-','MarkerSize',12)
plot(x(idxC), y(idxC), '.-','MarkerSize',12)
title(' Graph of Piecewise Discontinuous f(x)')
legend('y = x^2', 'y = x^3-5','y = 5-2x')
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
grid on
% Alternatively
figure(), plot(x,y,'-','LineWidth',2)
axis ([-10 10 -10 20])
set(gca,'XAxisLocation','origin','YAxisLocation','origin')
for reference you can do the loop as follows. You were almost there, but the mistake you made was not to index the current element and also not indexing where to store the output values within y. Compare the code you have to this and you should get the idea.
for ii = 1:numel(x)
if x(ii) <= 1
y(ii) = x(ii)^2;
elseif x(ii) <= 2
disp(1)
y(ii) = x(ii).^3 - 5;
else
y(ii) = 5 - 2*x(ii);
end
end
  2 comentarios
Wal MathEngineering
Wal MathEngineering el 8 de Feb. de 2022
Editada: Walter Roberson el 9 de Feb. de 2022
Thank you for your help.
Can we do in MATLAB open circle and close circle only on the domain of the graph? like in this link:
Turlough Hughes
Turlough Hughes el 8 de Feb. de 2022
I suppose you can do the following (I don't see any dedicated functions for this).
clc, clear
figure(), hold on
axis ([-10 10 -10 20])
fcnA = @(x) x.^2;
fcnB = @(x) x.^3 - 5;
fcnC = @(x) 5 - 2*x;
hp(1) = fplot(fcnA, [-pi 1],'-k','LineWidth',2);
plot(1,fcnA(1), 'ok','MarkerFaceColor','k','LineWidth',2)
hp(2) = fplot(fcnB, [1 2],'-r','LineWidth',2);
plot(1, fcnB(1), 'or','MarkerFaceColor','w','LineWidth',2)
plot(2, fcnB(2), 'or', 'MarkerFaceColor','r','LineWidth',2)
hp(3) = fplot(fcnC, [2 pi],'-b','LineWidth',2);
plot(2, fcnC(2), 'ob','MarkerFaceColor','w','LineWidth',2)
title('Graph of Piecewise Discontinuous f(x)')
legend(hp, 'y = x^2', 'y = x^3-5','y = 5-2x')
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
grid on
scatter might be worth considering also, which allows you to modify the inidividual marker appearances, but it's probably just as easy to use plot as above.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by