help with using function handle
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Sunethra Pitawala
el 12 de Sept. de 2021
Comentada: Sunethra Pitawala
el 13 de Sept. de 2021
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
Respuesta aceptada
Walter Roberson
el 12 de Sept. de 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.
0 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!