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

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 Comments

@AdamDanz can I use symbolic inputs in an m function? If not, then what is the way around this? What function can I use instead that does not expect symbolic inputs?

Thing is, I am not supplying what the point where I want the Jacobson evaluated at yet. The function that is calling this function is supplying that. So how do I get around this?

Also, how do I get the gradient taken care of this way too?

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).
It could be, but it’s not supposed to be. It’s supposed to be the input for my main function (I did not write my main function; I was supplied it by my professor and I am not proficient in Matlab enough to tweak it like that). So if I wanted to not set the value for my initial point here, how would I do that?
You can pass inputs from function1 to function2 whether function2 is in an independent m-file or is nested within the main file.
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.
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.

Sign in to comment.

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

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.

Sign in to comment.

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

I don’t know if I have symbolic toolbox. How do I find out if I do?
Also, why did you get rid of the nargout>2 and replace it with nargout==3?
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.
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.
They will. I need to run the same code over with 4 different initial points. And the function I have that does Newton’s method (here, Gauss-Newton because of the Jacobians) requires the points to be inputted directly into it.
Sorry, didn’t elaborate, my Newton’s method code requires this function as input.
"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 ".
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2017a

Asked:

on 8 Sep 2019

Commented:

on 14 Sep 2019

Community Treasure Hunt

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

Start Hunting!