0
votes

I want to minimize this function:

function [GCV2]=GCV(y,x,k)

[n, p]=size(x);

A=(x'*x+k*eye(p));
A=A\x';
A=x*A;

I_mat=eye(n);

num2=(I_mat-A);
num2=num2*y;
num2=norm(num2);
num2=num2^2;
num2=num2/n;

%(norm((I_mat-A)*y)^2)/n;
den2=(I_mat-A);
den2=trace(den2);
den2=den2/n;
den2=den2^2;

GCV2=num2/den2;

end

The x and y values are 13-by-4) and 13-by-1 arrays, respectively, and these values are already defined in the Matlab workspace. I want to optimize on the k value so that the function value GCV is minimized.

The parameter being optimized as well as the output are scalar so fminsearch should be appropriate.

But I can't get it to run?

I've tried several methods, the latest being:

k_min = fminsearch(@GCV,(x;y;0));
??? k_min = fminsearch(@GCV,(x;y;0));
                              |
Error: Unbalanced or unexpected parenthesis or bracket.

What am I doing wrong?

2

2 Answers

1
votes

Looks like you're learning about anonymous functions. fminsearch minimizes a single variable (which may be a vector). Your objective function must therefore have only one input. You have a function, GCV, that takes three inputs. Two are static and are defined in the workspace outside of the minimization, while k is the one to be minimized. To create a function with one input from GCV, you can use any anonymous function, taking care to specify which variables are parameters:

x = ...
y = ...
k0 = 0;
k_min = fminsearch(@(k)GCV(y,x,k),k0);
1
votes

The second input to fminsearch is the starting parameter (i.e. k0), so specify a starting value of k. Then you can define an anonymous helper function and optimize on that:

>> % define x,y
>> GCVk = @(k) GCV(y,x,k);
>> k0 = 0;
>> k_min = fminsearch(GCVk,k0)

There is probably another way to do this, but this is one of the listed ways of passing additional parameters for the optimizer.

And since there are no bonus points for being first, how about humor. Let's have an example:

>> x=1; y=1;
>> GCVk = @(k) x+y+k; k0=0;
>> k_min = fminsearch(GCVk,k0)

Exiting: Maximum number of function evaluations has been exceeded
         - increase MaxFunEvals option.
         Current function value: -316912650057057490000000000.000000 

k_min =
  -3.1691e+26

Found it - the lowest number (minus 2) in the world! Profit?