Using Optimizing Nonlinear Functions to find mutiple variations
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone, I'm having some problems when using Optimization to find some variations.
I have the equation as following:
xdata=125:2:145;
ydata=[0.002 0.003 0.009 0.025 0.053 0.089 0.104 0.09 0.07 0.041 0.017];
y=@(x)(-1/(dev*sqrt(2*pi))*exp(-1/2*((xdata-mean)/dev).^2);
with dev a.k.a deviation and mean is two fixed parameters I have to find.
I have referenced the Optimizing Nonlinear Functions and Solve a Constrained Nonlinear Problem, Solver-Based in Matlab.
This is my code.
%%%Function code
function sse = sseval(n,xdata,ydata)
mean=n(1);
dev=n(2);
sse=ydata-1/(dev*sqrt(2*pi))*exp(-1/2*((xdata-mean)/dev).^2);
end
%%%Main code
clc;
clear all;
close all;
xdata=[125 127 129 131 133 135 137 139 141 143 145];
ydata=[0.002 0.003 0.009 0.025 0.053 0.089 0.104 0.09 0.07 0.041 0.017];
%%%%Find mean and deviation value
rng default %for reproducibility
type sseval
fun = @(n)sseval(n,xdata,ydata);
x0=rand(2,1);
bestn=fminsearch(fun,x0)
The result showed the error and cannot run. Can anyone show me what is the problem with this? Thank you so much in advanced.
0 Kommentare
Akzeptierte Antwort
Torsten
am 8 Nov. 2023
xdata=[125 127 129 131 133 135 137 139 141 143 145];
ydata=[0.002 0.003 0.009 0.025 0.053 0.089 0.104 0.09 0.07 0.041 0.017];
%%%%Find mean and deviation value
fun = @(n)sseval(n,xdata,ydata);
x0=[135,1];
bestn=fminsearch(fun,x0)
xsim = xdata;
ysim = 1/(bestn(2)*sqrt(2*pi))*exp(-1/2*((xsim-bestn(1))/bestn(2)).^2);
hold on
plot(xsim,ysim)
plot(xdata,ydata,'o')
hold off
%%%Function code
function sse = sseval(n,xdata,ydata)
mean=n(1);
dev=n(2);
sse=ydata-1/(dev*sqrt(2*pi))*exp(-1/2*((xdata-mean)/dev).^2);
sse = sum(sse.^2);
end
Weitere Antworten (1)
Matt J
am 8 Nov. 2023
Bearbeitet: Matt J
am 8 Nov. 2023
There are also FEX downloads that can do gauss fitting for you, e.g.,
xdata=[125 127 129 131 133 135 137 139 141 143 145];
ydata=[0.002 0.003 0.009 0.025 0.053 0.089 0.104 0.09 0.07 0.041 0.017];
p=gaussfitn(xdata',ydata',[],{0,[],[]},{0,[],[]});
[~,A,mu,sig2]=deal(p{:});
mu,
dev=sqrt(sig2)
x=linspace(min(xdata),max(xdata));
yfit=@(xx) A*exp(-1/2*((xx-mu)/dev).^2);
plot(xdata,ydata,'o',x,yfit(x))
Siehe auch
Kategorien
Mehr zu Optimization Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!