Why do I get an erro when using the built-in function gradient() ?

5 次查看(过去 30 天)
Hi.
I can't get an ideal why the following codes using gradient() turn out to be erroneous.
01.F=@(X)(4*X(1)^2+X(2)^2-X(1)^2*X(2))
02.F =
03. @(X)(4*X(1)^2+X(2)^2-X(1)^2*X(2))
04.>> gradient(F)
05.??? Error using ==> zeros
06.Trailing string input must be a valid numeric class name.
07.Error in ==> gradient at 64
08. g = zeros(size(f),class(f)); % case of singleton dimension
please point out why it's an erro using !
Best regards.
David

采纳的回答

dpb
dpb 2014-6-4
Because you call F w/o any argument list.
Try
x=1:3;
gradient(F(x))
instead. However, I suspect this isn't doing what you are thinking it might (altho I'm not positive what that might be, I'm guessing a little on that point).
  3 个评论
dpb
dpb 2014-6-4
I don't understand what
x=[ x(1), x(2) ]
is supposed to represent. What it is is a 2-vector. But by definition gradient is just the first difference of the input vector so while you can call it using F(x) as the argument it's only going to evaluate the difference evaluated from the function evaluated over the inputs.
Now you could write the function definition to handle a vector input perhaps; but you would need to use different notation--like F(x,y) if the intent of x is to be a 2-column array of two different x-vectors. Again, that's just a guess as to what you may be intending...
David W.
David W. 2014-6-7
I have thought out another method to do my programming which need not follow this confusing way. Tanks any way!

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by