functions for finding values

3 次查看(过去 30 天)
how and what MATLAB functions could i use to find/verify the value of 'a', like i have done using the cubic spline method. (i am pretending that i don't know what that value is)
a=5;
w=-a:a/1000:a;
F=2*sin(a*w)./w;
%
%
w_int=-5:0.01:5;
F_int=spline(w,F,w_int);
plot(w_int,F_int,'o');
plot(w,F, '--', w_int,F_int,'o')
c = max(F_int);
[~, idx] = min(abs(w_int - 0.005));
d = w_int(idx);
a1 = (c*d)/(2*sin(d));

采纳的回答

Andrei Bobrov
Andrei Bobrov 2013-8-22
编辑:Andrei Bobrov 2013-8-23
Use Curve Fitting Toolbox
a=5;
w=-a:a/1000:a;
F=2*sin(a*w)./w;
%
w_int=-5:0.01:5;
w_int(abs(w_int) < eps(1e3)) = [];
F_int=spline(w,F,w_int);
solution:
f = fittype('a2*sin(a1*x)./x');
ff = fit(w_int(:),F_int(:),f,'StartPoint',[1 1]);
out = ff.a1
OR use Statistics Toolbox
a=5;
w=-a:a/1000:a;
F=2*sin(a*w)./w;
%
w_int=-5:0.01:5;
F_int=spline(w,F,w_int);
mf = @(b,x)b(2)*sin(b(1)*x)./x;
b_out = nlinfit(w_int,F_int,mf,[1; 1]);
out = b_out(1);
AND variant with Optimization Toolbox
xdata=(-5:0.01:5)';
ydata=spline(w,F,xdata);
t = abs(xdata) < eps(1e4);
xdata(t) = [];
ydata(t) = [];
f = @(a,xdata)a(2)*sin(a(1)*xdata)./xdata;
x = lsqcurvefit(f,[1; 1],xdata,ydata);
out = x(1);
  2 个评论
harley
harley 2013-8-22
thanks, i get the error
??? NaN computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients.
Error in ==> fit at 445
errstr = handleerr( errid, errmsg, suppresserr );
Error in ==> Untitled at 8
ff = fit(w_int(:),F_int(:),f,'StartPoint',[1 1]);
Andrei Bobrov
Andrei Bobrov 2013-8-22
corrected variable w_int

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Least Squares 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by