Comment tracer une fonction triangulaire et d'autres fonctions étape dans Matlab [fermé]

StackOverflow https://stackoverflow.com//questions/20033809

  •  21-12-2019
  •  | 
  •  

Question

Comme le titre l'indique, je veux savoir comment tracer une fonction triangulaire dans Matlab.par exemple.

f(x) = 1-|x| for |x| < 1 and f(x) = 0 otherwise

Ainsi que les fonctions :

Af(x) = A for x >= 0 and Af(x) = 0 for x < 0; -f(x) = -1 for x >= 0 and -f(x) = 0 for x < 0
Était-ce utile?

La solution

Je suppose que vous n'utilisez pas de variables symboliques.Pour le traçage 2D dans Matlab, vous avez besoin de deux vecteurs de taille égale, un par axe, vous devez donc créer un vecteur d'axe x et un vecteur d'axe y.Dans votre exemple f (x) = 1- | x | pour | x | <1 vous pouvez faire ceci:

x = linspace(-5,5,500); %x-axis vector from -5 to 5 with 500 points
y = zeros(1,500);  %y-axis vector initialized to 0, also 500 points like the x-axis vector
y(abs(x) < 1) = 1- abs(x(abs(x)<1)); %the points corresponding to |x|< 1 are set to |x|

figure() %new figure
plot(x,y) %plot
box off  %removing box
grid on  %adding grid
xlabel('x axis', 'FontSize', 15) %label of x axis
ylabel('y axis', 'FontSize', 15) %label of y axis
axis([x(1), x(end), -0.5, 1.5])  %axis limits

Avec cela, vous obtenez un tracé comme celui-ci :

enter image description here

Pour les autres fonctions il faut procéder comme dans celle-ci, construire le vecteur axe x et le vecteur axe y.

MISE À JOUR: Dans l'autre exemple : f(x) = A for x >= 0 and f(x) = 0 for x < 0:

A = 3;
x = linspace(-5,5,500); %x-axis vector from -5 to 5 with 500 points
y = zeros(1,500);  %y-axis vector initialized to 0, also 500 points like the x-axis vector
y(x >= 0) = A; %the points corresponding to x >= 0 are set to A

figure() %new figure
plot(x,y) %plot
box off  %removing box
grid on  %adding grid
xlabel('x axis', 'FontSize', 15) %label of x axis
ylabel('y axis', 'FontSize', 15) %label of y axis
axis([x(1), x(end), -0.5, 3.5])  %axis limits

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top