Minimizing a multivariable anonymous function that contains vectors using fminsearch

I am trying to minimize I0 and a in the function handle below. rBes and rCutFit are vectors with the same dimensions. The variables, wave, a, C, u, and r are constants that have already been defined in the script.
R2=@(I0,a) (rBes(u)-I0*((2*rCutFit((((2*pi)/wave)*a*sin(atan((u*C)/r))))/(((2*pi)/wave)*a*sin(atan((u*C)/r))))^.2)).^2; MIN=fminsearch(R2,[1,10^-6]);
I keep getting the error below everytime I run the code. Please let me know if you have any advice for fixing this.
"Not enough input arguments.
Error in @(I0,a)(rBes(u)-I0*((2*rCutFit((((2*pi)/wave)*a*sin(atan((u*C)/r))))/(((2*pi)/wave)*a*sin(atan((u*C)/r))))^.2)).^2
Error in fminsearch (line 189) fv(:,1) = funfcn(x,varargin{:});"

Answers (1)

Make R2 accept a vector input. E.g., use x as the argument and replace I0 with x(1) and a with x(2)
R2=@(x) (rBes(u)-x(1)*((2*rCutFit((((2*pi)/wave)*x(2)*sin(atan((u*C)/r))))/(((2*pi)/wave)*x(2)*sin(atan((u*C)/r))))^.2)).^2;
MIN=fminsearch(R2,[1,10^-6]);

9 Comments

I have tried your suggestion. This is my code:
wave=473*10^-9; r=0.143; C=4.022*10^-5; u=30;
R2=@(x) (rBes(u)-x(2)*((2*rCutFit((((2*pi)/wave)*x(2)*sin(atan((u*C)/r))))/(((2*pi)/wave)*x(2)*sin(atan((u*C)/r))))^.2)).^2; MIN=fminsearch(R2,[1,10^-6]);
This is the error I received:
"Subscript indices must either be real positive integers or logicals.
Error in @(x)(rBes(u)-x(2)*((2*rCutFit((((2*pi)/wave)*x(2)*sin(atan((u*C)/r))))/(((2*pi)/wave)*x(2)*sin(atan((u*C)/r))))^.2)).^2
Error in fminsearch (line 189) fv(:,1) = funfcn(x,varargin{:});"
Please let me know if you have any further suggestions. Thanks.
You didn't replace that 2nd "a" with x(2)
Also, the error you are getting typically indicates you are shadowing a function with a variable. E.g., maybe you have a variable named "sin" or "atan" or "rCutFit" or "rBes" in your workspace that is shadowing those functions.
So sorry. I replaced the 2nd a and got the same error. rCutFit is a column vector with the same dimensions as rBes.
I do not have a variable named sin and atan, but I do have rCutFit and rBes as defined column vectors before. rBes is from experimental data and rCutFit is generated through MATLAB's besselj.
rCutFit((((2*pi)/wave)*x(2)*sin(atan((u*C)/r))))
mean that you want to calculate (((2*pi)/wave)*x(2)*sin(atan((u*C)/r))) and use that as an index into rCurFit . That is going to fail except under unlikely circumstances that the computation produces a positive integer.
Did you need a multiplication between rCurFit and the following expression?
I need to express the vector rCutFit, which was produced by the besselj function in MATLAB and reshaped by removing a section of its first values, as a function of ((2*pi)/wave)*sin(atan((u*C)/r)). rCutFit is J1 in the mathematical function attached with its input, kasin(theta)), expressed in a different form.
rBes is an experimentally derived vector and was not generated by MATLAB.
Please note that in the numerator, kasin(theta) is not a multiplicative factor, but an input into the J1 function (the bessel function of the first kind, first order).
Arrays (and vectors) have particular sizes at any one time, and are indexed with () notation and what follows in the () must be a list of positive integers, possibly together with colons.
Functions also use () notation to invoke them, and can have non-integral arguments that are function parameters.
You cannot use non-integral values (other than colon) in the () after a vector name (except in intermediate calculations provided the overall result of the calculation is positive integer).
If you need to perform a calculation on a non-integral argument and get a vector or array output, then you should make rCutFit into a function that does the appropriate bessel calculation and removes whatever and reshapes as is suitable.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

on 22 Jul 2017

Commented:

on 23 Jul 2017

Community Treasure Hunt

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

Start Hunting!