How to evaluate parameters in sum() of handle functions with fminsearch
조회 수: 1 (최근 30일)
이전 댓글 표시
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/776888/image.png)
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)?
Thank you in advance
댓글 수: 2
John D'Errico
2021년 10월 24일
편집: John D'Errico
2021년 10월 24일
This is a question I see often, where somene feels they need to introduce symbolic parameters. When there is no reason to use a symbolic expression, DON'T! That you don't know the value of something does not mean it must be symbolic. And, as soon as you make things symbolic, you make MATLAB run MUCH more slowly.
As you can see in the answer you got, no symbolic parameters needed to be created or involved.
채택된 답변
Sargondjani
2021년 10월 24일
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])
댓글 수: 5
Sargondjani
2021년 10월 24일
편집: Sargondjani
2021년 10월 24일
There is one thing. I think the argument in fminsearch needs to be a vector. So you would need a transformation. So:
ObjFunc=@(B) sum(((y_exp-fun_y_sum(x_exp,B(1:n),B(n+1:2*n))).^2)./y_exp.^2);
B0=rand(2*n,1);
[B_est, SSE] = fminsearch(ObjFunc, B0)
B_est = [B_est(1:n),B(n+1:2*n)]
with
function y_sum = fun_y_sum(x,B1,B2)
y_sum = 0;
for ii = 1:size(B1,1)
y_sum = y_sum + B1(ii,1)*exp(-x/B2(ii,1));
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!