How to plot exact user defined function?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Agniswar
il 25 Mar 2014
Modificato: Serge Boisse
il 27 Ott 2018
I have defined a function like this
function z=agni(x)
if x > 0
z=x;
else
z = x.^2;
end
But in the time of plotting plot(x,agni(x)) with x =[-2:.01:2] generate only the curve of x^2. WHY?
0 Commenti
Risposta accettata
Walter Roberson
il 25 Mar 2014
if x > 0
means
if all(x > 0)
so if you pass in a vector of x then if any x in the vector is <= 0 then the "if" will not be true.
You should research logical indexing.
Più risposte (1)
Serge Boisse
il 27 Ott 2018
Modificato: Serge Boisse
il 27 Ott 2018
Hello, You should understand that your function accepts a vector of values, not a scalar. then it must return a vector of the same length (that will be plotted) So you may rewrite your function as follows :
function z=agni(x)
z = x; % this is a vector !
ind = (x<0); % logical array : 1 if corresponding element of x is <0
z(ind) = z(ind).^2;
end
Hope this helps. If you prefer to code functions "the classical way", have a look at symbolic functions.
0 Commenti
Vedere anche
Categorie
Scopri di più su Digital Filter Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!