0

You can minimize a function using fminunc like this:

x = linspace(-2,2,1000);
f0      = -x.^2;
ftest1  = @(a) a(1)*exp(-a(2)*x.^2);
fmin1   = @(a) trapz(x,(ftest1(a) - f0).^2);

[a1,fval] = fminunc(fmin1,rand(1,2));
plot(x,f0,x,ftest1(a1));

Is there a similar way to do this if you'd like to minimize with respect to two different (vector) variables?:

x = linspace(-2,2,1000);
f0      = -x.^2;
ftest2  = @(a,b) a(1)*exp(-b(1)*x.^2) + a(2)*exp(-b(2)*x.^2);
fmin2   = @(a,b) trapz(x,(ftest2(a,b) - f0).^2);
%[a2,b2,fval] = fminunc(...
anon01
  • 10,618
  • 8
  • 35
  • 58
  • You need to know the length of `a` and `b`... call it `n`... after that it's pretty simple: – transversality condition Jan 14 '16 at 22:15
  • @transversalitycondition if you have a solution then post it. – anon01 Jan 14 '16 at 22:42
  • According to documentation "A scalar objective function file accepts one input, say x, and returns one scalar output, say f". Why not pack both variables in a 1x4 vector? – Anton Jan 15 '16 at 13:03
  • Good find in the doc... that clears it up anyway. I was hoping to avoid this since its not quite as clean in the more complicated example. – anon01 Jan 15 '16 at 13:47

0 Answers0