help with using function handle

조회 수: 1 (최근 30일)
Sunethra Pitawala
Sunethra Pitawala 2021년 9월 12일
댓글: Sunethra Pitawala 2021년 9월 13일
Hi,
i am using a funcion handle to fit a function which is sum of Guassian distributions to a given set of data. I have stored the details of the Guassian distributions in vector 'x'. It all works fine. I am having trouble writing the final values of gT to a file as gT is not the output to the main code. Tried to write within the function 'parameters' but did not work.
Also, this guassian distribution functions need to fit to multiple sets of data so the part of the main code is running in a for loop too.
part of my code is below.
Any suggetion to help with this is appreciated. thanks,
main code:
func =@(x)parameters(x,T2,fT2);
x_updated = fminsearch(func, x);
The function parameters is below.
function [E] = parameters(x, T2, fT2)
mu = x(1:3:end)
std = x(2:3:end)
h = x(3:3:end)
gT1 = h(1)*(exp(-(T2-mu(1)).^2/(2*std(1)^2)));
gT2 = h(2)*(exp(-(T2-mu(2)).^2/(2*std(2)^2)));
gT3 = h(3)*(exp(-(T2-mu(3)).^2/(2*std(3)^2)));
gT = gT1 +gT2 + gT3;
E = sum(gT - fT2).^2;
end
  댓글 수: 1
Sunethra Pitawala
Sunethra Pitawala 2021년 9월 13일
Thanks a lot. It works.
I am new to matlab so this helps a lot.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 12일
E = sum(gT - fT2).^2;
Remove that line from that function, and remove ft2 from its calling arguments, and have the function return gT. Now the function is a pure prediction function.
Now create a second function which is
@(x) sum((parameters(x, T2) - ft2).^2)
and fminsearch that new function.
Afterwards, take the x you got from minimization and call parameters(x, T2) and you will get back the gT implied by that x.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by