help with using function handle
2 ビュー (過去 30 日間)
古いコメントを表示
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
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!