functions for finding values

3 Ansichten (letzte 30 Tage)
harley
harley am 22 Aug. 2013
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));

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 22 Aug. 2013
Bearbeitet: Andrei Bobrov am 23 Aug. 2013
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 Kommentare
harley
harley am 22 Aug. 2013
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 am 22 Aug. 2013
corrected variable w_int

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Least Squares finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by