the derivative of determinant

5 views (last 30 days)
san -ji
san -ji on 6 May 2014
Commented: san -ji on 10 May 2014
Hi! I have a problem about differentiating determinant.I don't know how to make it. I got these message:"Matrix dimensions must agree." ,"Inputs must be a scalar and a square,and "Inner matrix dimensions must agree." when I added "." somewhere. Thanks for helping!
h = 0.001;
x = 0:h:100;
m = 5;
h = 2*pi.*x;
q= besseli(5,h);
k = [m*q.*(h-3)*5 5*5*h ; q.*6-h.^2 q.*h-2 ];
c = diff(det(k));
----------------------------------------------------------------------------
I revised my code here:
f = 0.001;
x = 1:f:5;
w = 2*pi*x;
k = [cos(w), 0; 0, cos(w) ];
detk = k(1,1)*k(2,2)-k(2,1)*k(1,2);
c = diff(detk)/f;
plot(x,detk,x,[0 c],'r:')
I want to differentiate detk to f.But after I run it,my matrix c is empty. Can anyone tell me what is wrong? Thanks.
  4 Comments
Andrew Newell
Andrew Newell on 6 May 2014
I think you'll need to describe the problem you're trying to solve. Without more information I can only guess how to make it square.
san -ji
san -ji on 6 May 2014
Oops! I posted wrong program. I edited it again. My question is: How to differentiate determinant k(2*2 matrix).

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 6 May 2014
Edited: John D'Errico on 6 May 2014
You need to explain what you are doing. I think that your problem is you are not thinking that through yourself, so you don't really appreciate what is happening in these statements.
You cannot simply throw diff or det at something and expect it to magically know what you want it to do, especially when that something is not the size or shape input that det expects to see.
Lets look at what you have done so far.
h = 0.001;
x = 0:h:100;
m = 5;
h = 2*pi.*x;
q= besseli(5,h);
By the way, defining h to be a scalar, then re-using h immediately afterwards to be a vector is TERRIBLY poor programming style. If you continue to do things like this in your code, expect to find MANY bugs cropping up, that will be tough to understand what happened. Your question then to this forum will be, "What is wrong with my code?????"
If we step past that flaw...
Clearly x is a moderately long row vector, so will be q. Both are of size 1x100001.
But now, what do you think k is?
k = [m*q.*(h-3)*5 5*5*h ; q.*6-h.^2 q.*h-2 ];
What shape is this thing?
size(k)
ans =
2 200002
So k is a mess. Maybe you wanted to create a 2x2xn array. Maybe not. Did you expect this to create a sequence of 2x2 arrays? Look more carefully at your code. Do the computations at the command line as I have done, and often you will understand more about your problem. THINK about what you are doing at each step, taking the time to see what has happened. If each step is as you expect it to be, then the final result will often work easily.
So, what determinants are you looking to create? What is it that you are trying to differentiate? With respect to WHAT variable?
Once you start thinking clearly about what you are trying to do, enough so that you can explain the problem to others, it will often be far more clear how to solve the problem, both to you and to those who might try to help you.
Edit: You comments above suggest that what you WANTED to do was to create a sequence of matrices, all 2x2, then to differentiate WRT the sequence parameter.
Answer to that: a 2x2 determinant is TRIVIAL to compute. You don't need to use det. So if A is a 2x2 matrix, then det(A) would be...
A(1,1)*A(2,2) - A(2,1)*A(1,2)
If A is actually a sequence of matrices, then simply compute the above value for each member of the sequence. The result will be another vector, of length 1x100001. Use diff on that vector. Make sure you know what you are differentiating with respect to. Is it x? h?
  1 Comment
san -ji
san -ji on 10 May 2014
I am too edgy I post my bad codes.I will modify my codes carefully. Thank you very much for responding and teaching me with a long letter.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!