How to avoid error in anonymous function

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

That's the way given in the problem i.e. using 3 anonymous functions
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.

Sign in to comment.

Answers (2)

f = @(x, u) Fx(x,u)*x(t-1) + Fu(x,u)*u(t-1);

10 Comments

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) .
Basiclly, I am trying Kalman filter localization to predict the current state xt from the previous state xt-1 and the motion model ut, but getting stuck trying to implement this in Matlab using anonymous functions as depicted previously.
You cannot do that using only anonymous functions.
This is the problem statement:
Please write the anonymous function that realizes the motion model . For later use, please also implement the anonymous functions and that compute the Jacobian and of the motion model with respect to the state as well as to relative motion measurement respectively.
f = @(x, u) ????;
Fx = @(x,u) ????;
Fu = @(x,u) ????;
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
So not sure how to get f without Fx, Fu. Tried f = diff(f, x) * x(t-1) but get error.
Torsten
Torsten on 7 May 2022
Edited: Torsten on 7 May 2022
What is f in the equation xhat_k = f(x_k-1,u_k) ? How did you define it ?
is possibly some kind of function that is related to the kinematics and estimates the current position of the robot from the past position and the specified values for the input vector . @Ken to clarify.
This is a comment from someone who got it. Not sure how useful; I could not replicate symbols used correctly.
As per lecture slide, the position at time t is the position at time (t-1) plus the displacement from the motion model (using motor encoders etc).
So, X_hat = f(Xt-1,ut with f=[f1;f2] , X=[x1;x2] and U=[u1;u2]
Then Deltax.f is defined as (delf1/delx1,delf2/delx2;delf2/delx1,delf2/delx2)
Similar for Deltau.f
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 ?

Sign in to comment.

Tried as suggested, error Undefined function 'Fx' for input arguments of type 'double'.:
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);

5 Comments

As per the problem statement f is a function handle i.e. per first line of pr statement. Assignment is to replace the ????:
f = @(x, u) ????;
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.
Tried this but error:"Undefined function 'f' for input arguments of type 'double'".
f = @(x,u) jacobian(f(x,u),x).*x(t-1) + jacobian(f(x,u),u).*u(t);
Fx = @(x,u) jacobian(f(x,u),x);
Fu = @(x,u) jacobian(f(x,u),u);
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
Your anonymous function f cannot refer to f or the jacobian of f.

Sign in to comment.

Categories

Asked:

Ken
on 6 May 2022

Commented:

on 9 May 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!