Problem with fminsearch Error in fminsearch : (line 200) fv(:,1) = funfcn(x,varargin{:});

24 views (last 30 days)
hello every one
Here's my code :
% Variables
eta = 20 ;
beta = 100 ;
mensualites5 = 8.974646657515070*10^3 ;
k5 = 120 ;
portion_alpha = linspace(0,1,100);
k_alpha = k5.*portion_alpha ;
% Equations
reinvestissement = (1-portion_alpha)*mensualites5 ;
benefice = eta*reinvestissement.*exp(-k_alpha/beta) ;
profit = (benefice-portion_alpha.*mensualites5).*k_alpha ;
minus_profit = -(benefice-portion_alpha.*mensualites5).*k_alpha ;
% Function + fminsearch
f = @(portion_alpha) minus_profit ;
x0 = 0.35 ;
[portion_alpha, fmin] = fminsearch(f,x0);
I always have this error message :
Unable to perform assignment because the size of the left side is
1-by-1 and the size of the right side is 1-by-100.
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in Exercice1_2 (line 130)
[portion_alpha, fmin] = fminsearch(f,x0);
I don't understand why. Could somebody help me pleaaaase ? Thx

Answers (1)

Zenin Easa Panthakkalakath
Hello Amandine,
It seems that the function f is not defined in the right way. Here f is a function that takes 'portion_alpha' as an argument and returns 'minus_profit'. 'minus_profit' isn't even defined in the function and hence the value of the same is taken from the defenition before.
If you try to run the following command, it would result in a logical array of 1s. This means that the function is invariant to the input argument; or in other words, function is constant.
>> f(1) == f(2)
I think you should try to define the function in the right way and see if f(x) for different values of x are giving you the answers as expected.
Please read through the documentation for more infromation:

Community Treasure Hunt

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

Start Hunting!

Translated by