Anonymous Function Error Question

3 visualizaciones (últimos 30 días)
Brittany Isbister el 10 de Mzo. de 2021
Comentada: Stephen23 el 11 de Mzo. de 2021
Hi All,
I am trying to write an anonymous function that will take the equation and then it will find the parameters for x(1) and x(2). It's telling me that my it can't do it because the size of the left side is 1-by-1 and the size of the right side is 41-by-1. The drug1Dose is a 41x1 structure. Attached is my code. Thank you for your help in advance!
f= @(X,c) c.^X(2)./(X(1)^X(2)+ c.^X(2));
c=drug1Dose;
fun=@(X)f(X,c);
x1=[2,5];
param1=fminsearch(fun,x1)
1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Stephen23 el 11 de Mzo. de 2021
"The drug1Dose is a 41x1 structure. Attached is my code."
So lets try it and see what happens:
drug1Dose = struct('whatever',num2cell(rand(41,1)))
drug1Dose = 41x1 struct array with fields:
whatever
f= @(X,c) c.^X(2)./(X(1)^X(2)+ c.^X(2));
c=drug1Dose;
fun=@(X)f(X,c);
x1=[2,5];
param1=fminsearch(fun,x1)
Operator '.^' is not supported for operands of type 'struct'.

Error in solution (line 2)
f= @(X,c) c.^X(2)./(X(1)^X(2)+ c.^X(2));

Error in solution (line 4)
fun=@(X)f(X,c);

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

Iniciar sesión para comentar.

Respuestas (1)

Star Strider el 10 de Mzo. de 2021
The problem you want to solve is not at all clear.
It would appear that you are doing curve-fitting (parameter estimation), with independent and dependnet variables. If so, ‘fun’ needs to be:
fun = @(x) norm(c - f(x,t));
assuming that you are estimating the pharmacokinetic parameters of your model sa a function of time.
In any event, ‘fun’ needs to return a scalar, not a vector, for fminsearch (or any other optimisation function) to work correctly.
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Categorías

Más información sobre Nonlinear Optimization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by