help with using function handle

1 view (last 30 days)
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 Comment
Sunethra Pitawala
Sunethra Pitawala on 13 Sep 2021
Thanks a lot. It works.
I am new to matlab so this helps a lot.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Sep 2021
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!