lsqcurvefit help: Error using asset. FUN must have two input arguments.

10 Ansichten (letzte 30 Tage)
I'm trying to work through a curve fitting problem and I haven't been able to resolve the following coding error. Unfortunately, I haven't done much curve fitting as of yet, so any help is greatly appreciated!
PV = readtable('clean_data.csv');
NV = readtable('noisy_data.csv');
% PV is clean
lambda_p = table2array(PV(:,1));
I_p = table2array(PV(:,2));
% NV is noisy
lambda_n = table2array(NV(:,1));
k_n = lambda_n./(2*pi);
I_n = table2array(NV(:,2));
w_e = 2.8e-8;
% Curve fitting with v_hat in Fourier space
v_hat_p = abs(fft(I_p));
k_p = lambda_p./(2*pi);
v_hat = @(A_v,w_g,w_l) A_v.*exp(-(w_g.^2*k_p.^2)/(16*log(2)) - (w_l.*abs(k_p))./2);
A_v = 100; w_g = 0.01; w_l = 0.01; % Initial guesses
x0 = [A_v, w_g, w_l]; % initial guesses
[x_fit,y_fit] = lsqcurvefit(v_hat,x0,k_p,v_hat_p);
Error using assert
FUN must have two input arguments.
Error in lsqcurvefit (line 229)
assert(numberInput == 2, message('optimlib:lsqcurvefit:NotEnoughInputArg'));
Error in myfilename (line 29)
[x_fit,y_fit] = lsqcurvefit(v_hat,x0,k_p,v_hat_p);

Akzeptierte Antwort

Torsten
Torsten am 1 Mär. 2023
Replace the line
v_hat = @(A_v,w_g,w_l) A_v.*exp(-(w_g.^2*k_p.^2)/(16*log(2)) - (w_l.*abs(k_p))./2);
by
V_hat = @(A_v,w_g,w_l,k_p) A_v.*exp(-(w_g.^2*k_p.^2)/(16*log(2)) - (w_l.*abs(k_p))./2);
v_hat = @(x,xdata) V_hat(x(1),x(2),x(3),xdata)
And the division by 16*log(2) is superfluous - this can be compensated by fitting w_g accordingly.
  1 Kommentar
Richard Schure
Richard Schure am 1 Mär. 2023
Thanks so much! I did try to add the k_p variable to the original and that didn't work, but now I see how separating the equations fixes the problem.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by