Triangle fitting using fminsearch
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
How can I fit a dataset with Gaussian plus Triangle function? Here is my fitting function attached.
function [FW,slope] = gaussplustriangle(x,x0,y,isPlot) %x0 is fixed center
% b1 Gaussian amplitude
% b2 sigma gaussian
% b3 amplitude triangle
% b4 triangle slope left side
% b5 triangle slope right side
%%
initialGuess = [0.5, 0.8, 0.5 ,0.05, 0.05];
fG = @(b,x) b(1).*exp(-0.5.*(x-x0).^2./(b(2)).^2); %Gaussian fn
for xi=1:length(x)
if x(xi) < x0
fT(xi)= @(b,x) b(3) - b(4)*x0 + b(4).*x(xi);
else
fT(xi)= @(b,x) b(3) + b(5)*x0 - b(5).*x(xi);
end
end
F=@(b,x) b(1).*exp(-0.5.*(x-x0).^2./(b(2)).^2)+ fT; % Objective function
%%
OLS = @(b) sum((F(b,x) - y).^2); % Ordinary Least Squares cost function
%%
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
%%
B = fminsearch(OLS, initialGuess, opts); % Use ‘fminsearch’ to minimise the ‘OLS’ function
%%
if isPlot
subplot(5,1,1:3);
plot(x, y, '*b') % plotting all fitted functions, resids (not included)
hold on
end
end
1 Commento
Walter Roberson
il 28 Mag 2020
It is confusing how you sometimes use x as a parameter of your anonymous functions, and in other places uses it as the variable that is passed in as a parameter. @(b) sum((F(b,x) - y).^2); is using the parameter, but it is passing it to an anonymous function... why bother to use it as a parameter for F when you know it is going to resolve to the value passed in to gaussplustriangle ?
Risposte (1)
Walter Roberson
il 28 Mag 2020
Guessing about what you were trying to do with that fT stuff:
function FW = gaussplustriangle(x,x0,y,isPlot) %x0 is fixed center
% b1 Gaussian amplitude
% b2 sigma gaussian
% b3 amplitude triangle
% b4 triangle slope left side
% b5 triangle slope right side
%%
initialGuess = [0.5, 0.8, 0.5 ,0.05, 0.05];
fG = @(b,x) b(1).*exp(-0.5.*(x-x0).^2./(b(2)).^2); %Gaussian fn
fT = cell(length(x),1);
for xi=1:length(x)
if x(xi) < x0
fT{xi} = @(b,x) b(3) - b(4)*x0 + b(4).*x(xi);
else
fT{xi} = @(b,x) b(3) + b(5)*x0 - b(5).*x(xi);
end
end
F=@(b,x) b(1).*exp(-0.5.*(x-x0).^2./(b(2)).^2)+ sum(cell2mat(cellfun(@(f) f(b,x), fT, 'uniform', 0))); % Objective function
%%
OLS = @(b) sum((F(b,x) - y).^2); % Ordinary Least Squares cost function
%%
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
%%
FW = fminsearch(OLS, initialGuess, opts); % Use ‘fminsearch’ to minimise the ‘OLS’ function
%%
if isPlot
subplot(5,1,1:3);
plot(x, y, '*b') % plotting all fitted functions, resids (not included)
hold on
end
end
I had to guess about what FW was intended to be. And you did not calculate slope anywhere.
I suspect strongly that you are processing the triangle quite incorrectly. I think you should be doing something more like:
ft = @(b,x) b(3) + b(4) .* (x - x0) .* (x <= x0) + b(5) .* (x0 - x) .* (x0 > x)
but please re-check that the signs for b(4) and b(5) are the way that you want them to be.
0 Commenti
Vedere anche
Categorie
Scopri di più su Surrogate Optimization in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!