Octave: testare le funzioni in un intervallo in elenchi o come con For-loop?
Domanda
voglio vedere i valori della funzione y (x) con diversi valori di x, dove y (x) <5:
y = abs (x) + abs (x + 2) -5
Come posso fare?
Soluzione
fplot(@(x) abs(x)+abs(x+2)-5, [-10 10])
hold all
fplot(@(x) 5, [-10 10])
legend({'y=abs(x)+abs(x+2)-5' 'y=5'})
Altri suggerimenti
Si può solo creare un vettore di valori x
, poi guardare il y
vettore, o tracciare esso:
% Create a vector with 20 equally spaced entries in the range [-1,1].
x = linspace(-1,1,20);
% Calculate y, and see the result
y = abs(x) + abs(x + 2) - 5
% Plot.
plot(x, y);
% View the minimum and maximum.
min(y)
max(y)
Se si limita x
alla gamma [-6 4]
, farà in modo che y
sarà limitata a minore o uguale a 5. In MATLAB, è possibile tracciare la funzione utilizzando fplot (come Amro ha suggerito ) o linspace e PLOT (come Peter suggerito ):
y = @(x) abs(x)+abs(x+2)-5; % Create a function handle for y
fplot(y,[-6 4]); % FPLOT chooses the points at which to evaluate y
% OR
x = linspace(-6,4,100); % Create 100 equally-spaced points from -6 to 4
plot(x,y(x)); % PLOT will plot the points you give it
% create a vector x with values between 1 and 5
x = 1:0.1:5;
% create a vector y with your function
y = abs(x) + abs(x+2) - 5;
% find all elements in y that are under 5
y(find(y<5))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow