How to avoid error in anonymous function
Show older comments
I am trying to predict the robot position using the 3 steps below but get error:
Fx = @(x,u) jacobian(f(x,u),x);
Fu = @(x,u) jacobian(f(x,u),y);
f = @(x, u) Fx*x(t-1) +Fu*u(t-1);
2 Comments
Ken
on 7 May 2022
Walter Roberson
on 7 May 2022
The jacobian of a function with respect to a single variable is the same as the derivative with respect to that variable. So Fx is diff(f, x) and Fu is diff(f, y). But f is not a function of y, so diff(f, y) is 0. So your f simplifies to
f = diff(f, x) * x(t-1)
Except that x is obviously a function of t, so you are taking the derivative with respect to a function.
By examination it becomes clear that the simplest function that can satisfy this is f(x, u) = 0, and there is no real reason to try for anything more complicated.
Answers (2)
Walter Roberson
on 6 May 2022
f = @(x, u) Fx(x,u)*x(t-1) + Fu(x,u)*u(t-1);
10 Comments
Walter Roberson
on 6 May 2022
However, please reconsider what you are doing. You define the function Fx and Fu in terms of some function f and then having done that, you define an anonymous function named f . The f that is used in Fx will never be the same as the f that you define after Fx and Fu .
When you define an anonymous function, it looks through the expression you give, and it locates all of the parameters from the @() expression and marks them. Then it looks through all the remaining names (that are not the parameters in the @() list), including for things like jacobian and f, and sees whether those names correspond to variables in the current workspace, or which occur as named parameters to the current function, and it takes copies of those variables and stores them in the anonynous function it is building. Then it looks through to see if any names correspond to global variables or variables that are being imported into the current function as references to variables defined in a function that nests the current one, and it puts references to those. (Technically this is really the same phase as the previous one, with a slightly different understanding of what it means to "copy" in those cases.) Then after that it starts looking at the remaining names and looks to see if they can be resolved as function names, and if so then it locks in references to the exact place that function was defined. Any name that remains, that is not a parameter or variable or accessible function, is marked as undefined.
So at the time you first do
Fx = @(x,u) jacobian(f(x,u),x);
the anonymous function definition is going to go looking for an existing f . If it finds a variable by that name, whether numeric or anonynous function, then it will copy that into the @ structure it is creating -- locking in the definition of hypothetical anonymous function f that exists then . If it doesn't find a variable f then it will look in the usual places, and if it finds some f in the path it will lock in that f . If not found, it locks in "undefined"
Any changes to variable f after that point cannot affect what happens when Fx is executed. Even if you had had an f.m file that was being picked up at the time you defined Fx then it is permitted for MATLAB to have parsed f.m at the time that Fx was built, and record it in memory, and changes to F.m after that point might not affect anonymous function Fx
Your code has the look of trying to define a Legendre function or something like that, a recursive construction. It is not possible to define mutual recursion between pure anonymous functions in MATLAB. And if it were possible, your code would bomb when it eventually tried to access u(0) .
Ken
on 7 May 2022
Walter Roberson
on 7 May 2022
You cannot do that using only anonymous functions.
Ken
on 7 May 2022
Walter Roberson
on 7 May 2022
It says "for later use". So not something needed to implement f. It is not clear that the definition of f depends on the jacobians.
Note too that Δuf is asked for, not Δyf
Ken
on 7 May 2022
Ken
on 7 May 2022
Torsten
on 7 May 2022
You still did not answer the main question: Is f a function, a function handle ? Is f difficult such that derivatives are only available by numerical differencing or easy such that it's possible to get them analytically ?
Ken
on 7 May 2022
5 Comments
Ken
on 7 May 2022
If you refer to f as f(x,u), f need not be a function handle, it can also be a complicated function.
If f is so simple that you can use a handle, then do
syms x u
f = x^2+u^2;
fx = matlabFunction(diff(f,x),'Vars',{x,u})
fu = matlabFunction(diff(f,u),'Vars',{x,u})
f = matlabFunction(f,'Vars',{x,u})
F = @(x,u) fx(x,u).*x + fu(x,u).*u;
By the way:
In your lines of code
f = @(x, u) Fx(x,u)*x(t-1) + Fu(x,u)*u(t-1);
Fx = @(x,u) jacobian(f(x,u),x);
Fu = @(x,u) jacobian(f(x,u),y);
you use Fx and Fu before you define them. This cannot work.
Ken
on 9 May 2022
Walter Roberson
on 9 May 2022
Edited: Walter Roberson
on 9 May 2022
What you want to do cannot be programmed in MATLAB.
This is a different statement than saying that the assignment cannot be completed in MATLAB.
In MATLAB it is impossible for an anonymous function to refer to itself. I explained that in detail earlier. To refresh your memory:
At the time you build the anonymous function that will be assigned to f, MATLAB will look at all mentioned functions and variables, and will take copies of the variables that exist (except for the named parameters), and will mark the other ones as undefined. f does not exist at the time the right hand side of the @ is evaluated, so it gets marked as undefined in the anonymous function. The f that the anonymous function gets assigned to has no connection to the f inside the anonymous function. When you execute the anonymous function, at the point that it needs to call f, it sees that inside of the anonymous function that f is marked as undefined and it will error. Under no circumstances will it look to say "Oh, but f is defined now, we will use that!". NEVER
Walter Roberson
on 9 May 2022
Your anonymous function f cannot refer to f or the jacobian of f.
Categories
Find more on Function Handles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


