how to write partial derivatives in MATLAB
13 次查看(过去 30 天)
显示 更早的评论
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 个评论
John D'Errico
2022-5-26
编辑:John D'Errico
2022-5-26
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.
Ken
2022-5-26
Maybe I didn't explain clearly:
delf1delx1=????;
Not sure what to put on the RHS of the eqn
采纳的回答
Torsten
2022-5-26
27 个评论
Ken
2022-5-26
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.
Ken
2022-5-26
Tried, got:
Unrecognized function or variable 'df1dx1'.
Error in solution>@(x,u)[df1dx1,df1dx2;df2dx1,df2dx2] (line 2)
Ken
2022-5-26
I used this:
f = @(x, u) x+u;
Fx = @(x,u) [df1dx1,df1dx2;df2dx1,df2dx2];
Fu = @(x,u) [df1du1,df1du2;df2du1,df2du2];
Ken
2022-5-27
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
2022-5-27
编辑:Torsten
2022-5-27
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
2022-5-27
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) ????;
Torsten
2022-5-27
And did you write the anonymous function f that realizes the motion model ?
Without f, no Fx and no Fu.
Torsten
2022-5-28
Then Fx = 1, Fu = t.
But what is your question ?
If you want me to deduce f: I can't.
Ken
2022-6-12
编辑:Torsten
2022-6-12
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
Ken
2022-6-22
Still no luck - wrong. Got this comment: The Jacobian of a multivariate function (the output of f is two-dimensional) is a matrix.
Walter Roberson
2022-6-22
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.
Ken
2022-6-22
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 )];
Torsten
2022-6-22
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.
Ken
2022-6-22
This is the problem statement that cannot be changed:
f = @(x, u) ????;
Fx = @(x,u) ????;
Fu = @(x,u) ????;
Walter Roberson
2022-6-22
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 ?
Ken
2022-6-22
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."
Ken
2022-6-22
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.]
Walter Roberson
2022-6-23
"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
2022-6-24
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
2022-6-24
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
2022-6-25
syms x [2 1]
syms u [2 1]
f = x + u
f =

jacobian(f, x)
ans =

jacobian(f, u)
ans =

更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)