Using Optimizing Nonlinear Functions to find mutiple variations
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
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))
1 Commento
Lam Ha
il 9 Nov 2023
Categorie
Scopri di più su Optimization Toolbox in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

