3

In matlab, we use meshgrid instead of a double for-loop to increase the speed, especially when the number of iterations is large.

In my application, I am using meshgrid to find the critical point of a matrix.

syms x
a=0.1:5
b=0.1:5
[A,B]=meshgrid(a,b)
y=A*x^2+B*x+B
y_deriv=diff(y,x)
solution=solve(y_deriv==0,x)

However, it gives me

Warning: 25 equations in 1 variables. 
> In C:\Program Files\MATLAB\R2013b\toolbox\symbolic\symbolic\symengine.p>symengine at 56
  In mupadengine.mupadengine>mupadengine.evalin at 97
  In mupadengine.mupadengine>mupadengine.feval at 150
  In solve at 170 
Warning: Explicit solution could not be found. 
> In solve at 179 

solution =

[ empty sym ]

What I meant to do is:

solve(y_deriv(1)==0,x)

and

solve(y_deriv(2)==0,x)

... and so on.

I could do it in a loop, but I don't want to. Is there any element-by-element wise solving operation in matlab?


Update:

I think y_deriv gives me:

[ x/5 + 1/10,  (11*x)/5 + 1/10,  (21*x)/5 + 1/10,  (31*x)/5 + 1/10,  (41*x)/5 + 1/10]
[ x/5 + 11/10, (11*x)/5 + 11/10, (21*x)/5 + 11/10, (31*x)/5 + 11/10, (41*x)/5 + 11/10]
[ x/5 + 21/10, (11*x)/5 + 21/10, (21*x)/5 + 21/10, (31*x)/5 + 21/10, (41*x)/5 + 21/10]
[ x/5 + 31/10, (11*x)/5 + 31/10, (21*x)/5 + 31/10, (31*x)/5 + 31/10, (41*x)/5 + 31/10]
[ x/5 + 41/10, (11*x)/5 + 41/10, (21*x)/5 + 41/10, (31*x)/5 + 41/10, (41*x)/5 + 41/10]

What I want is to solve x/5+1/10==0, x/5+11/10==0, x/5+21/10==0, ... and so on for all elements in the matrix.

jurgispods
  • 745
  • 8
  • 19
andy_tse
  • 189
  • 1
  • 5
  • 13
  • 1
    I think that [diff](http://se.mathworks.com/help/symbolic/differentiation.html) does not work exactly as you hope it does. The function does an elementwise differentiation for matrix input. `x` is treated as a scalar here and I assume that this is not what you want. However, I found a similar topic on [maths stackexchange](http://math.stackexchange.com/questions/518760/critical-points-of-a-matrix-equation). – patrik Dec 22 '14 at 08:17
  • Actually @patrik, `diff` is the correct function here, see http://uk.mathworks.com/help/symbolic/diff.html. I think the issue is because of `meshgrid`. @andy_tse, I think the coefficients `A` and `B` in the equation to solve need to be scalars, you can't use matrices. – am304 Dec 22 '14 at 09:48
  • 1
    @am304 I think that op wants to solve [this](http://math.stackexchange.com/questions/518760/critical-points-of-a-matrix-equation). However, OP assumes that by creating a `sym` named `x` and using it in a matrix equation, `diff` will assume `x` to be a matrix. This is however, not the case (it is assumed scalar here). Also, for matrices `diff` works elementwise (since x is scalar and diff is only able to differentiate scalar variables). This may not be what OP want, why I said that `diff` may not work as he think it does. – patrik Dec 22 '14 at 11:48
  • 1
    @andy_tse I think that it would be better to rename the question to **"How to find critical points of a matrix equation in matlab"** – patrik Dec 22 '14 at 11:59
  • 2
    @andy_tse So what you want to do is not to find the critical point of a matrix then? You rather want to find the critical points of a set of equations that is collected in a matrix? These are not the same and not what you wrote in your question. However, it seems that for matrices only one output per variable is permitted. note that a matrix equation `Ax+b=0` where size of A is 3x3 actually contains 3 equations an 3 variables (`[a11*x1+a12*x2+a13*x3+b1=0;...]`). Anyway, it does not seem to matter if you use cell either, so I suggest using cells instead and `cellfun` with argument `'un',0`. – patrik Dec 23 '14 at 06:33

0 Answers0