In MATLAB R2020b I have the following code:
f=@(x) x.^2;
y=2;
g=@(x) f(x).*y
and the output is
g = function_handle with value: @(x)f(x).*y
But y = 2
, so I would expect the output to be @(x)f.*2
. Is this how its supposed to work or a bug? And can I display it as f.*2
rather than f.*y
?
disp()
method for function handle classes, i.e. you'd need to modify the MATLAB source code for this class directly. It might be impossible, e.g. if the code is pre-compiled and/or obfuscated, and even if possible, I'd recommend against modifying MATLAB source files. You might be able to write a wrapper function that's called instead ondisp()
calls for function handles. – Lillithy
: what happens if you doy = 3;h=@(x) f.*y
and then compare the output with that ofg
? and of course: what happens then forg(2)
andh(2)
? Are the outputs the same, i.e. did the value ofy
insideg
change, or are they different? – Lillithg(2)
gives me 8 andh(2)
gives 12. So it works when I put in the argument. But I need to create an array of Legendre polynomials (by recursion),P_=cell(1,N+1); P_{1}=@(u_) 1; P_{2}=@(u_) u_; for n=1:N, P_{n+2}=@(u_)((n+1)*@(u_)P_{n+1}(u_)-n*@(u_)P_{n}(u_))/(2*n+1); end
and it never progresses because each cell has n but not its value in the loop. Any ideas on this? – CallousP_{3}(4)
gives `` 2.3333. But I still find it a problem that
P_{3}`` gives `` function_handle with value: @(u_)((n+1)*P_{n+1}(u_)-n*P_{n}(u_))/(2*n+1)``, because n=4. This make the code difficult to check analytically. – Callousintegral
andquadgk
which apply some fancy quadrature. Is this possible with the vector polynomials? – Callous@Cris
to notify me of your comment. Otherwise I only see it by chance... You can create an anonymous function that callspolyval
. The approach would allow computing the polynomial factors once, when you construct it, rather than every time you evaluate the polynomial. – Passkeyy
could have been anything. And Matlab does not have a generalized method of formatting arbitrary values as literal expressions. (mat2str
only works on basic primitive types.) Plus what ify
occurred several times in the function's code? Easier to understand if it's a variable. Finally, ify
is ahandle
object, its value is actually mutable, and could change between subsequent calls on your anonymous function. – Brochette