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 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