Undefined function for input arguments of type 'double': how do I make them vectors and not doubles?
Show older comments
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.
. 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.
(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 Comments
Adam Danz
on 8 Sep 2019
jacobian() expects symbolic inputs
OvercomerGrit
on 8 Sep 2019
Adam Danz
on 8 Sep 2019
or use lsqnonlin() with 0 max iterations
OvercomerGrit
on 8 Sep 2019
Adam Danz
on 9 Sep 2019
That's what functions are for! :)
The function could be a local or nested function (defined within the same m file as your main code).
OvercomerGrit
on 9 Sep 2019
Adam Danz
on 9 Sep 2019
You can pass inputs from function1 to function2 whether function2 is in an independent m-file or is nested within the main file.
OvercomerGrit
on 9 Sep 2019
Adam Danz
on 9 Sep 2019
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.
Answers (2)
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 Comments
OvercomerGrit
on 14 Sep 2019
Adam Danz
on 14 Sep 2019
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
on 8 Sep 2019
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 Comments
OvercomerGrit
on 8 Sep 2019
David Hill
on 8 Sep 2019
If you type:
sym a
and get an error, then I would think you don't have the symbolic toolbox.
I didn't know exactly what you were doing...if nargout ==1 is a possiblity, then you should put the other if statement back in with the a,b,c assignments at the end.
David Hill
on 8 Sep 2019
If the symbolic expressions for val, H, and g will not be changing, you could fix those and use your function to produce the evaluations of those expressions. No need to keep calculating the jacobian and gradient symbolic expressions over and over.
OvercomerGrit
on 8 Sep 2019
OvercomerGrit
on 8 Sep 2019
Adam Danz
on 9 Sep 2019
"nargout>2" is a better habbit than "nargout==3".
Suppose 6 months from now you add an additional output to the funciton and now it has 4 outputs. When the user executes this line below, the funciton will not compute the 3rd output becuase nargout=4, not =3, and that will result in an error.
[val,g,H,z]=givenfGNM(x)
Of course you could go through your code and look for any nargout==foo and change it but instead, you could make it flexible to begin with.
About checking whether you have symbolic toolbox, type ver() in the command window and view the list of toolboxes you have. Look for "Symbolic Math Toolbox ".
OvercomerGrit
on 9 Sep 2019
OvercomerGrit
on 14 Sep 2019
Edited: OvercomerGrit
on 14 Sep 2019
OvercomerGrit
on 14 Sep 2019
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!