Nested Anonymous Function in fminsearch not enough inputs

2 views (last 30 days)
I created a script as follows:
clear, clc, close all
a = 0.1;
b = 0.05;
m =0;
n = 0;
%Number of iterations
iter = 10;
%Vector Arrays
x = linspace(0,0.1,iter);
y = linspace(0,0.05,iter);
%Grid Array
[X,Y] = meshgrid(x,y);
%Nested Loops
for m = 0:iter
for n = 0:iter
P = ((sin(m*pi*X/a)).^2.*(cos(n*pi*Y/b)).^2)+((cos(m*pi*X/a)).^2.*(sin(n*pi*Y/b)).^2);
n = n+1;
end
m = m+1;
end
contour(X,Y,P)
title('Electromagnetic Power Density (W/m2)');
grid on;
Now I want to perform a fminsearch and am using the following code:
clear, clc, close all
a = 0.1;
b = 0.05;
m = 0;
n = 0;
%Number of iterations
iter = 10;
%Vector Arrays
x = linspace(0,0.1,iter);
y = linspace(0,0.05,iter);
WGPower = @(a,b,m,n,x,y)((sin(m*pi*x/a)).^2.*(cos(n*pi*y/b)).^2)+((cos(m*pi*x/a)).^2.*(sin(n*pi*y/b)).^2);
[pos,fmin]=fminsearch(WGPower,[0.01 0.01]);
fprintf('\nThe position of the min is: %2.4f\n',pos)
fprintf('The value of the min position is: %2.4f \n',fmin)
I keep getting the 'Not enough input arguments' error, however all the inputs have been accounted for.
Any assistance would be appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Feb 2014
WGPower = @(x) ((sin(m*pi*x/a)).^2.*(cos(n*pi*y/b)).^2)+((cos(m*pi*x/a)).^2.*(sin(n*pi*y/b)).^2);
However, this is going to use the linspace value of y from the scope it is defined in.
Then when you have
[pos,fmin]=fminsearch(WGPower,[0.01 0.01]);
the [0.01 0.01] tells fminsearch that it is searching over a vector of length 2. So all of the vectors that it passes in to "x" in the anonymous function will be length 2. And that is not going to work with the length 10 value of y.
My thought is that probably what you are after is
WGPower = @(xy) ((sin(m*pi*xy(1)/a)).^2.*(cos(n*pi*xy(2)/b)).^2)+((cos(m*pi*xy(1)/a)).^2.*(sin(n*pi*xy(2)/b)).^2);
Take note that the "x" and "y" linspace done just before the assignment are not going to have any effect on anything.
You should also be looking more closely at your fprintf() statement about the position of the minimum. As your x0 is length 2, the position of the minimum is going to be length 2. That fprintf() format will accept that, and it will deal with it by printing out "The position" text twice, once for pos(1) and once for pos(2) . Perhaps you want to use '%2.4f %2.4f' instead of just one %2.4f
  1 Comment
Matt J
Matt J on 14 Feb 2014
Edited: Matt J on 14 Feb 2014
Additionally, if the unknowns to be solved for in the following truly are x and y, as Walter conjectures,
f(x,y) = ((sin(m*pi*x/a)).^2.*(cos(n*pi*y/b)).^2)+((cos(m*pi*x/a)).^2.*(sin(n*pi*y/b)).^2);
then the minimum is trivially achieved at x=y=0.
So there would need to be constraints on the unknowns for the problem to make sense, and FMINSEARCH is not a constrained solver.

Sign in to comment.

More Answers (0)

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!