You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to write partial derivatives in MATLAB
13 views (last 30 days)
Show older comments
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
John D'Errico
on 26 May 2022
Edited: John D'Errico
on 26 May 2022
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.
Accepted Answer
Torsten
on 26 May 2022
27 Comments
Ken
on 26 May 2022
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.
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.
Ken
on 27 May 2022
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) ????;
Ken
on 12 Jun 2022
Edited: Torsten
on 12 Jun 2022
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
Walter Roberson
on 22 Jun 2022
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.
Walter Roberson
on 22 Jun 2022
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 ?
Walter Roberson
on 23 Jun 2022
"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.
Walter Roberson
on 24 Jun 2022
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" .
Ken
on 24 Jun 2022
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."
Walter Roberson
on 25 Jun 2022
syms x [2 1]
syms u [2 1]
f = x + u
f =
jacobian(f, x)
ans =
jacobian(f, u)
ans =
More Answers (0)
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)