# How to evaluate parameters in sum() of handle functions with fminsearch

4 views (last 30 days)
3Nz0 on 24 Oct 2021
Commented: 3Nz0 on 24 Oct 2021
I have some problems with the evaluation of parameters in a sum of handle functions
x_exp and y_exp are known vectors.
I want to evaluate the parameters in the following function:
The idea is to use fminsearch to evaluate g(i) and t(i): ObjFunc=@(g,t)sum((y_exp-y_eq(x))^2/(y_exp^2))
Looking in the community, I tried to use:
x_exp=rand(1,10); %just to define the known vectors
y_exp=rand(1,10);
n=3;
g=rand(1,n);
t=rand(1,n);
syms x i
y_eq = matlabFunction( symsum(g.*exp(-x_eq./t), i, 1, n));
My question is: "How can I evaluate g(i) and t(i)?
##### 2 CommentsShow 1 older commentHide 1 older comment
3Nz0 on 24 Oct 2021
Sorry, I know it is not necessary to use the symbolic parameters.
I solved the problem but, actually, I really don't know how to make this in an automatic way.
My mfile (posted in the other comment) works fine, but an adaptive mfile depending on "n" should be great.
Thank you.

Sargondjani on 24 Oct 2021
Take a look at this. I hope it helps.
x_exp=rand(1,10); %just to define the known vectors
y_exp=rand(1,10);
n=3;
g=rand(1,n);
t=rand(1,n);
%Create function handle:
fun_eq = @(g,t,x_eq)sum(g*-exp(-x_eq/t));
% Evalute y_eq for g(2) and t(2):
ii = 2;
y_eq = fun_eq(g(ii),t(ii),x_exp);
% Use fminsearch as follows:
%Define the objective
ObjFunc = @(g,t,x_,y_)sum((y_-fun_eq(g,t,x_)).^2./(y_.^2));
% Use x_exp and y_exp as input x_ and y_, and g and t are inputs x(1) and (x2)
[sol] = fminsearch(@(x)ObjFunc(x(1),x(2),x_exp,y_exp),[1,1])
3Nz0 on 24 Oct 2021
I don't know, in the old script B was a matrix in fminsearch().
Again, many thanks for your help

### Categories

Find more on Calculus in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!