1

I encountered a problem calling the function defined in the code below.. According to Matlab I do not have enough input arguments in the nested function Chi, when I use the function by typing in "Awesomefit(V,I,Ierr)". However I cannot really explain why, since all the input the function needs should be provided.. V, I and Ierr are matrices of 148x1 double and get initialized before calling up the function.

Does anyone have an idea what could be missing?

Matlab also shows an error message in the line with fminsearch. I do not know if this is related to the other error message or to wrongly using the "fminsearch" command. Right now I would think it is the latter possibility. But this is definitely not my main issue. I hope I can cope with that once i got the other problem solved.

Please excuse my unorthodox programming style, but I'm a physicist trying to program :/ I'm still giving my best though..

%% do stuff

function a = Awesomefit(V,I,Ierr)

% initialize starting values
A = 1;
Vbd = 25;
n = 1.2;
b = -0.01;
Var = [Vbd n b];

%do stuff
a = fminsearch(Chi,Var);


function Ifit = InotNorm(V,Vbd,n,b)
    Ifit = zeros(size(V));
    for i = 1:length(V)
        if V(i) < Vbd,
            Ifit(i) = 2*10^-12;
        else
            Ifit(i) = A * abs( (V(i)-Vbd) / (V(i)-Vbd+1/(n*b)) )^n;
        end
    end
end


function NM = NormMod(Vbd,n,b)
    NM = sum(InotNorm(V,Vbd,n,b) ./ Ierr);
end


function ND = NormDat(I,Ierr)
    ND = sum(I ./ Ierr);
end


function C = Chi(Vbd,n,b)
    C = sum(( (InotNorm(V,Vbd,n,b) .* NormDat(I,Ierr) ./ NormMod(Vbd,n,b) - I) ./ Ierr ).^2);
end


end
0

1 Answer 1

4

Two problems:

  1. The function you are trying to minimize Chi - the input of this function is required to be a single variable. The documentation does specifically say this: http://www.mathworks.com/help/matlab/ref/fminsearch.html.

    As such, if you have three variables you are trying to minimize, you must put them into a single vector... like you did with Var. As such, you need to change Chi to reflect this:

    function C = Chi(in) %// Change
        Vbd = in(1); %// Change
        n = in(2); %// Change
        b = in(3); %// Change
        C = sum(( (InotNorm(V,Vbd,n,b) .* NormDat(I,Ierr) ./ NormMod(Vbd,n,b) - I) ./ Ierr ).^2);
    end
    
  2. The first input into fminsearch is required to be a function handle if you read the documentation carefully. Therefore, you simply need to change your fminsearch call to this:

    a = fminsearch(@Chi, Var);
    

    When you did this before:

    a = fminsearch(Chi, Var);
    

    MATLAB interprets Chi as a variable, not a function.... which is why you were getting that error. You need to pass a handle to the function as the first input parameter to fminsearch. You can think of a handle as a "pointer" to the function that you are trying to minimize. Read more about function handles here: http://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html

Doing this and setting V = I = Ierr = 1, I get this:

>> Awesomefit(1,1,1)

ans =

   25.0000    1.2000   -0.0100
Sign up to request clarification or add additional context in comments.

1 Comment

@MSergio - No problem at all. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.