Domanda

Come indica il titolo, voglio sapere come tracciare una funzione triangolare in Matlab.ad es.

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

Così come le funzioni:

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
È stato utile?

Soluzione

Presumo che tu non stia usando variabili simboliche.Per la stampa 2D in matlab sono necessari due vettori di dimensioni uguali, uno per asse, quindi è necessario creare un vettore dell'asse x e un vettore dell'asse Y.Nel tuo esempio f (x) = 1 - | x |per|x / < 1 potresti farlo:

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

Con questo si ottiene una trama come questa:

enter image description here

Per le altre funzioni devi procedere come in questa, costruire il vettore dell'asse x e il vettore dell'asse Y.

AGGIORNARE: Nell'altro esempio: 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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top