Undefined function for input arguments of type 'double': how do I make them vectors and not doubles?

48 ビュー (過去 30 日間)
For an assignment I'm working on, I was asked to write an input function that would calculate the Hessian of a particular function using the Jacobian approximation - i.e., . Then, have the code I was given for Newton's method call this input function, and the result would essentially be the same as running Gauss-Newton's method on it instead.
The problem is, every time I attempt to run it, I get error messages telling me "Undefined function 'jacobian' for input arguments of type 'double'." How do I modify my code so that it doesn't think that the function "val" which is supposed to be a function of three variables:
is of type double, and that it knows it is supposed to take both the gradient and the Jacobians with respect to the vector (but it needs to be coded as x(1), x(2), x(3) in order for my Newton's Method function to accept it.
Here is the code of my input function (thank you for your time and patience):
function[val,g,H]=givenfGNM(x)
%givenf() modified to output the Hessian approximated by the Jacobian, as
%required by the Gauss-Newton Method
val=(1/2)*((2*x(1)-(x(2)*x(3))-1)^2+(1-x(1)+x(2)-exp(x(1)-x(3)))^2+(-x(1)-2*x(2)+3*x(3))^2);
if(nargout>1)
g=gradient(val, x);
end
if(nargout>2)
J=jacobian(val, x);
K=transpose(J);
H=mtimes(K,J);
end
end
  9 件のコメント
OvercomerGrit
OvercomerGrit 2019 年 9 月 9 日
I suppose I could also set the values in the function m file each time and just make sure whatever inputs I gave it were the same as what the file said. But that would be incredibly contrived.
I don’t know where even in the Newton’s method file I would nest this code. And I don’t want to do that. I like your solution, I just want to modify it to allow it to take a new inputted initial x value every time and I don’t know how to do that.
Adam Danz
Adam Danz 2019 年 9 月 9 日
Your hesitation is healthy! :) Hesitation in the face of doubt is much better than the very common mistake we often see when people who have no idea what they are doing start making messy changes to working code.
Nested functions usually go at the end of the main function. Here is some introductory info on nested functions :
Save a backup of your working code so 1) you can revert to it if needed and 2) so you can compare the outputs from your updated code to the older version as a means of a sanity check.
"I suppose I could also set the values in the function m file each time... ...But that would be incredibly contrived"
Ineeded it would be contrived so don't do that. If you need additional inputs, add them to the nested funtion. For example, in Matt J's answer, you could even add the objective function handle as an input if you think the objective function itself might change.

サインインしてコメントする。

回答 (2 件)

Matt J
Matt J 2019 年 9 月 9 日
編集済み: Matt J 2019 年 9 月 9 日
Here is the non-symbolic approach mentioned by Adam.
function[val,g,H]=givenfGNM(x)
%givenf() modified to output the Hessian approximated by the Jacobian, as
%required by the Gauss-Newton Method
givenf=@(z) (1/2)*((2*z(1)-(z(2)*z(3))-1)^2+(1-z(1)+z(2)-ezp(z(1)-z(3)))^2+(-z(1)-2*z(2)+3*z(3))^2);
val=givenf(x);
opts=optimoptions('lsqnonlin','MaxIter',1,'Display','none',...
'Algorithm','levenberg-marquardt');
[~,~,~,~,~,~,J]=lsqnonlin(givenf,x,[],[],opts);
g=J.';
H=J.'*J;
end
  2 件のコメント
OvercomerGrit
OvercomerGrit 2019 年 9 月 14 日
What do the ~'s and the []'s mean here?
Adam Danz
Adam Danz 2019 年 9 月 14 日
Check out the documentation for lsqnonlin.
You'll see that the 7th output is the jacobian. You don't need outputs 1:6 so to get the 7th output without assigning outputs 1:6 to variables, you can use tildes (~) as place holders.
Similarly, "opts" is the 5th input but you don't necessarily need to specify the lower and upper bounds (3rd and 4th inputs) (or maybe you do need to specify them - that's up to you). With many functions an empty input results in using the default values.
To summarize, use tildes for output place holders and square brackets for input place holders in most functions.

サインインしてコメントする。


David Hill
David Hill 2019 年 9 月 8 日
function[val,g,H]=givenfGNM(x)
syms a b c
val=(1/2)*((2*a-(b*c)-1)^2+(1-a+b-exp(a-c))^2+(-a-2*b+3*c)^2);
g=gradient(val, [a b c]);
if(nargout==3)
J=jacobian(val, [a b c]);
K=transpose(J);
H=mtimes(K,J);
a=x(1);
b=x(2);
c=x(3);
H=double(subs(H));
end
a=x(1);
b=x(2);
c=x(3);
g=double(subs(g));
val=double(subs(val));
end
If you do have symbolic toolbox, you should be able to produce the gradient and jacobian of your function and evaluate them whenever you want using the subs function.
  9 件のコメント
OvercomerGrit
OvercomerGrit 2019 年 9 月 14 日
編集済み: OvercomerGrit 2019 年 9 月 14 日
If I’m still not getting the convergence I’m supposed to get (namely the convergence that somebody else got with theirs), should I post my Newton’s Method function that I’m feeding this into, along with screenshots of my output and their code and outputs as a separate question? They’re getting convergence in 14 iterations and I’m not even getting it in 2000 iterations.
OvercomerGrit
OvercomerGrit 2019 年 9 月 14 日
I posted it as a separate question on here

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by