Reputation: 13
I am trying to pass three cell2mat arrays for I,V,H through this function and plot the result of the parameter from the nlinfit model below. But when the code is run it plots nothing and only stores one value. Any help is appreciated:)
function [Icp] = Fraunhofer_Function(I,V,H)
V1 = @(b,I)(b(1).*sign(I).*real(sqrt(I.^2 - (sign(I).*( (b(2)+b(3)/2) )).^2)) + b(4));
Vthresx = find(V<=1e-3 & V>=0);
Ithresvec = max(I(Vthresx));
Voffsetx = find(I<=0.1e-3 & I>=-.1e-3);
Voffset = max(V(Voffsetx));
Rn = (max(V)-min(V))/(max(I)-min(I));
beta1 = [Rn; Ithresvec; -Ithresvec; Voffset]; %Init values b1=Rn b2 = Icp, b3 = Icm, b4 = Voffset
opts = statset('MaxIter', 500000, 'MaxFunEvals', 100000, 'RobustWgtFun', 'andrews');
B1 = nlinfit(I, V, V1, beta1, opts ); %Fit
Icp = V1(B1,V);
end
files = dir('*.xlsx*');
for k =1:length(files)
filenames = files(k).name;
txt = 'I,V,H';
[num,txt,raw] = xlsread(filenames);
%Put data into numerical columns
Idata = num(:,1)'; Vdata = num(:,2)'; Hdata = num(:,3)';
[Hu,~,idx] = unique(Hdata);
Isplit = splitapply(@(x) {x}, [Idata(:)],idx);
Vsplit = splitapply(@(x) {x}, [Vdata(:)],idx);
Hsplit = splitapply(@(x) {x}, [Hdata(:)],idx);
for l = 1:length(Isplit)
I = (Isplit{l,1});
V = (Vsplit{l,1});
H = (Hsplit{l,1});
%fit the data to the functional form
Icp = Fraunhofer_Function(I,V,H);
end
end
Example of the I,V,H, data is below:enter image description here
Upvotes: 0
Views: 131
Reputation: 6863
Right now you are just setting Icp
to the second estimated coefficient. From the Matlab docs nlinfit
, the output beta
:
beta — Estimated regression coefficients vector
Estimated regression coefficients, returned as a vector. The number of elements in beta equals the number of elements in beta0.
So to use the estimated parameters, you should call your modelfun
, with the parameters stored in B1
:
Icp = V1(B1,V);
plot(H,Icp);
Upvotes: 0