Reputation: 1
I'm attempting to create a Lorentzian distribution for my data using an anonymous function. The Gaussian distribution is created with no issues, but the Lorentzian distribution encounters this error.
The error:
Error using /
Matrix dimensions must agree.
Error in @(p,x)1/(pi*p(2)*(1+((x-p(1))/(p(2))).^2))
Code:
% Variables necessary for histograms
num_bins = 100;
low_range = linspace(min(low_data), max(low_data), num_bins);
low_counts = histc(low_data, low_range);
low_err = sqrt(low_counts);
% Creating and testing fits
gauss_fun_form = @(p, x) 1/(p(2)*sqrt(2*pi))*exp(-(x-p(1)).^2/(2*p(2).^2));
loren_fun_form = @(p, x) 1/(pi*p(2)*(1+((x-p(1))/(p(2))).^2));
low_avg = mean(low_data);
low_std = std(low_data);
low_gauss_param = [low_avg low_std];
low_gauss = gauss_fun_form(low_gauss_param, low_range);
low_gauss_scale = max(low_counts)/max(low_gauss);
low_loren_param = [low_avg 2*log(2)*low_std];
low_loren = loren_fun_form(low_loren_param, low_range);
low_loren_scale = max(low_counts)/max(low_loren);
Upvotes: 0
Views: 167
Reputation: 2898
(pi*p(2)*(1+((x-p(1))/(p(2))).^2)) generate vector of 100 dimensions
while 1 is vector of 1 dimension, so matrix division is not valid , ./ should be applied. change the definition of loren_fun_form as below should work
loren_fun_form = @(p, x) 1./(pi*p(2)*(1+((x-p(1))/(p(2))).^2));
Upvotes: 1