how to write partial derivatives in MATLAB

I am trying to form a 2X2 matrix using partial derivatives i.e. [delf1/delx1, delf1/delx2; delf2/delx1, delf2/delx2]. Not sure how to write it.

2 Comments

This is strange. @Torsten gave you a simple solution. Your response was that the excercise is so simple, that it should be doable using direct computations. In fact, it is quite simple. So do it.
In fact, you know how to form a matrix, since you wrote that part in your question. So just compute the derivatives. Where is the problem? Do you not know how to use diff? Are you not allowed to use diff? If you can use diff, then do so. If not, then you must understand what the definition of a derivative is, as a limit, and surely you have learned how to approximate a derivative? So what are you not saying? Don't just say it is easy, as you would not be asking the question if it was really that easy for you.
IF the entire thing is too complex for you to do, then take one piece at a time. You have a function (actually, two of them), of two variables. Differentiate each piece with respect to each variable, using whatever means you wish. Then do that 4 times, and put it together.
Maybe I didn't explain clearly:
delf1delx1=????;
Not sure what to put on the RHS of the eqn

Sign in to comment.

 Accepted Answer

27 Comments

Thanks. Comment:
This exercise is simple enough, such that you don't need to use any kind of function call like jacobian(). All the exercise is asking for, is to implement the function for the state transition, f = x+u and its derivatives Fx, Fu, which can easily be calculated by hand.
Then do as suggested: Calculate it by hand.
df1dx1 = ...;
df1dx2 = ...;
df2dx1 = ...;
df2dx2 = ...;
Df = [df1dx1,df1dx2;df2dx1,df2dx2];
Tried, got:
Unrecognized function or variable 'df1dx1'.
Error in solution>@(x,u)[df1dx1,df1dx2;df2dx1,df2dx2] (line 2)
I used this:
f = @(x, u) x+u;
Fx = @(x,u) [df1dx1,df1dx2;df2dx1,df2dx2];
Fu = @(x,u) [df1du1,df1du2;df2du1,df2du2];
f = @(x, u) x+u;
Df = @(x,u)[1,1];
or simply (since dfdx and dfdu are constant)
Df = [1,1]
Thanks, still no dice. I guess the problem is to get the estimated position from previous position and motion estimate -
and put this in MATLAB i.e. x(t) =x(t-1) + u(t)*t
Torsten
Torsten on 27 May 2022
Edited: Torsten on 27 May 2022
I guess the problem is to get the estimated position from previous position and motion estimate -
and put this in MATLAB i.e. x(t) =x(t-1) + u(t)*t
I don't see any relation to your first question.
Maybe you should copy your assignment in here in order not to waste our time.
Thanks here is the assignment:
In this simplified example, the state is solely comprised of the position of the robot. We assume that the robot provides some inputs to its locomotion system and employs sensors to determine the resulting change in position, which we will denote as .
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) ????;
And did you write the anonymous function f that realizes the motion model ?
Without f, no Fx and no Fu.
Tried:
f = @(x,u) x+u*t;
Then Fx = 1, Fu = t.
But what is your question ?
If you want me to deduce f: I can't.
This is the problem statement and my solution. Still get error Error "in solution: Line: 7 Column: 3
Unsupported use of the '=' operator. To compare values for equality, use '=='. To specify name-value arguments, check that name is a valid identifier with no surrounding quotes."
f = @(x, u) x+u;
Fx = @(x,u) [1,0];
Fu = @(x,u) [0,1];
%%% SECTION FOR SELF-EVALUATION. PLEASE DO NOT EDIT BELOW THIS LINE %%%
x = [1;2]
x = 2×1
1 2
u = [3;4]
u = 2×1
3 4
f_eval = f(x,u)
f_eval = 2×1
4 6
Fx_eval = Fx(x,u)
Fx_eval = 1×2
1 0
Fu_eval = Fu(x,u)
Fu_eval = 1×2
0 1
Still no luck - wrong. Got this comment: The Jacobian of a multivariate function (the output of f is two-dimensional) is a matrix.
You posted that you have
f = @(x, u) x+u;
Fx = @(x,u) [df1dx1,df1dx2;df2dx1,df2dx2];
Fu = @(x,u) [df1du1,df1du2;df2du1,df2du2];
You should be using
f = @(x, u) x+u;
Fx = @(x,u) [df1dx1(x, u), df1dx2(x,u); df2dx1(x, u), df2dx2(x, u)];
Fu = @(x,u) [df1du1(x,u), df1du2(x,u); df2du1(x, u), df2du2(x, u)];
after having defined df1du1 and so on as function handles.
The result will be function handles that will compute the matrices given inputs.
I suspect however that what is expected is that you create a function that takes inputs and returns the jacobian matrix, rather than returning a handle to create the matrix.
Thanks, tried it and got:
Undefined function 'df1dx1' for input arguments of type 'double'.
Error in solution>@(x,u)[df1dx1(x,u),df1dx2(x,u);df2dx1(x,u),df2dx2(x,u)] (line 2)
Fx = @(x,u) [df1dx1(x, u), df1dx2(x,u); df2dx1(x, u), df2dx2(x, u )];
I repeat what I already wrote:
You should be using
f = @(x, u) x+u;
Fx = @(x,u) [df1dx1(x,u), df1dx2(x,u); df2dx1(x, u), df2dx2(x, u)];
Fu = @(x,u) [df1du1(x,u), df1du2(x,u); df2du1(x, u), df2du2(x, u)];
after having defined df1dx1 and so on as function handles.
This is the problem statement that cannot be changed:
f = @(x, u) ????;
Fx = @(x,u) ????;
Fu = @(x,u) ????;
Is that problem statement incompatible with the forms we show like
Fx = @(x,u) [df1dx1(x,u), df1dx2(x,u); df2dx1(x, u), df2dx2(x, u)];
after having defined df1dx1 and so on as function handles ?
I believe that Fx = @(x,u) [1,0]is correct. I guess the problem lies in the first line i.e. f= @(x, u) x+u; This is because the instructor comment was "The Jacobian of a multivariate function (the output of f is two-dimensional) is a matrix."
Torsten
Torsten on 22 Jun 2022
Edited: Torsten on 22 Jun 2022
fx = 1, not [1,0] if f = x+u.
I was given the foll. hint to solve this:
X= f[Xt-1, Ut] , and f=[f1,f2]', X = [x1,x2]', U=[u1,u2]'
Then grad xF is defined as [delf1/delx1 etc....]
similarly grad uF is defined as [del f1/delu1 etc.]
"The Jacobian of a multivariate function (the output of f is two-dimensional) is a matrix."
That is correct.
I guess the problem lies in the first line i.e. f= @(x, u) x+u;
That is not the Jacobian of any function, so it does not matter if it is not a matrix. On the other hand in the quote above, we are told that the output of f is two-dimensional (so, not a vector), so apparently f should be returning a 2d array.
X= f[Xt-1, Ut] , and f=[f1,f2]', X = [x1,x2]', U=[u1,u2]
X seems to be a 2 x 1 column vector, and U seems to be a 1 x 2 row vector, if I read that correctly. In that case, using implicit expansion. x+u woud be (2 x 1) plus (1 x 2) which should give a 2 x 2 result. However, it is not common for formulas to add arrays of different sizes (unless one of the operands is a scalar), so it is not clear that x+u is correct -- either that or else 2 x 1 and 1 x 2 might not be correct.
Looks like a typo crept in i.e. U=[u1,u2] is wrong, should be U=[u1,u2]'
If x is 2 x 1 and u is 2 x 1, then u+x is 2 x 1, and that contradicts the information we are given that "the output of f is two-dimensional" .
Thanks. just wonder about the foll. hint I rec'd and if it can be used to solve this problem:
"First, we're dealing with a discrete system here, so there's no dependency on time t anymore. Further i want to note that the execise gives you x=x[k-1] and u=u[k]. Given the state transition x[k] = x[k-1] + u[k] the solution is as simple as f = @(x, u) x + u. Differentiating this equation by x and u will then give you two very simple, constant matrices as the solution."
syms x [2 1]
syms u [2 1]
f = x + u
f = 
jacobian(f, x)
ans = 
jacobian(f, u)
ans = 

Sign in to comment.

More Answers (0)

Tags

Asked:

Ken
on 26 May 2022

Commented:

on 25 Jun 2022

Community Treasure Hunt

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

Start Hunting!