functions for finding values

3 ビュー (過去 30 日間)
harley
harley 2013 年 8 月 22 日
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 Center および File ExchangeLeast Squares についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by