functions for finding values

3 vues (au cours des 30 derniers jours)
harley
harley le 22 Août 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));

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 22 Août 2013
Modifié(e) : Andrei Bobrov le 23 Août 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 commentaires
harley
harley le 22 Août 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 le 22 Août 2013
corrected variable w_int

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Least Squares dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by