Trying to write a function using @
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
ssmith
am 17 Nov. 2021
Kommentiert: John D'Errico
am 17 Nov. 2021
I am trying to write this function in my code but I am getting an invalid MATLAB syntax error. The equation is g(x) = sin(x)/x
Here is what I have:
g = @x sin(x)/x
g(1)
g(pi)
The g(1) and g(pi) are to be used calling the function to solve.
0 Kommentare
Akzeptierte Antwort
Stephen23
am 17 Nov. 2021
Bearbeitet: Stephen23
am 17 Nov. 2021
g = @(x) sin(x)/x;
% ^ ^ you forgot these
g(1)
g(pi)
1 Kommentar
John D'Errico
am 17 Nov. 2021
A good addendum is that IF you want this function to be vectorized, thus able to work for an entire vector or array of inputs elements, then use the ./ operator. Thus:
g = @(x) sin(x)./x;
(Without that dot in there, you will get strange results when you try to use this function on a vector.) But now we can use g on entire vectors.
g([1 2 3 4 5])
The singularity at x==0 is unresolved of course, since 0/0 is undefined..
g(0)
We can even plot it nicely without any arguments from MATLAB, thus
fplot(g,[-10*pi,10*pi])
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Elementary Math finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!