How to fit a particular analytical function while optimising its parameters to a histogram for different data sets in a loop in Matlab?
7 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to fit a particular analytical function to histogram of a dataset. The parameters of the function is being optimised using lsqcurvefit command of Matlab. When I am trying for a single data set, its giving right curve. But when I am trying to put it in a loop to fit several datasets such that each time it will take one dataset and fit the function with optimized parameters to its histogram and then go to the next dataset and do the same job, it is giving wrong curve.
I am suspecting that somehow it is taking garbage values while plotting the function.
please help me out with this. I am attaching input 3 datasets .
thank you.
d=dir('latlong*.csv');
n=length(d);
data=cell(n,1);
for i = 0:2
for j=0:0
data{j*70 + i+1}=csvread(d(j*70 + i+1).name,1,0,[1,3,8830,3]);
a = data{j*70 + i+1};
b(:,1) = a(:,2);
k3=0;
for l=1:8831
if b(l,1)>=0.001
k3=k3+1;
end
end
z=zeros(k3,1);
k3=0;
for l=1:8831
if b(l,1)>=0.001
k3=k3+1;
z(k3,1)=b(l,1);
end
end
figure(i+1);
hold on
h=histogram(z,1000,'Normalization','pdf');
no=h.NumBins;
counts=zeros(no,1);
counts(:,1)=h.Values;
edge=zeros(no+1,1);
edge(:,1)=h.BinEdges;
edge=edge(2:end,1);
Fun3=@(q,edge) ((1./(edge.*q(2).*sqrt(2*pi))).*exp(-((log(edge)-q(1)).^2)./(2*q(2)^2)));
q0=[2,2];
q(i+1,:) = lsqcurvefit(Fun3,q0,edge,counts);
plot(edge,Fun3(q,edge));
hold off
xlabel('Observation')
ylabel('Probability Density')
legend('histogram','log')
end
end
0 个评论
回答(1 个)
Brian
2022-11-18
Hello Joya,
I refactored your code to get a better idea of what it was doing, and there seem to be two issues. The main issue is that on line 39 you call "Fun3" with "q" instead of "q(i+1,:)". The second issue is that "csvread" is prepending a zero to your data. The "csvread" function is not recommended and it is recommended to use "readmatrix" or "readtable" instead.
d=dir('latlong*.csv');
n=length(d);
data=cell(n,1);
q=zeros(n,2);
for i = 1:n
data{i}=readtable(d(i).name);
z=data{i}.precipitation(data{i}.precipitation >= 0.001);
figure(i);
hold on
h=histogram(z,1000,'Normalization','pdf');
no=h.NumBins;
counts=h.Values';
edge=h.BinEdges(2:end)';
Fun3=@(q,edge) ((1./(edge.*q(2).*sqrt(2*pi))).*exp(-((log(edge)-q(1)).^2)./(2*q(2)^2)));
q0=[2,2];
q(i,:)=lsqcurvefit(Fun3,q0,edge,counts);
plot(edge,Fun3(q(i,:),edge));
hold off
xlabel('Observation')
ylabel('Probability Density')
legend('histogram','log')
end
Hope this helps, let me know if I misunderstood anything.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!