Issues with nlinfit - not fitting second parameter? Fitting a Blackbody curve
Mostra commenti meno recenti
Hello all,
I have code to fit a curve to a set of data (a black body curve). I used nlinfit so I could extract errors on the fitted parameters, but I can't seem to get the second parameter to be considered?
I should end up with a set of 50 plots for the fits, and then a final plot showing luminosity, temp and radius over time. However, the temp over time plot is just displaying the initial value for nlinfit.
Any ideas on what I am doing wrong here?
%errors and fitting of BB function
%constants needed for BB equation
% SI UNITS
c=2.998e8;
h=6.6261e-34;
kb=1.38e-23;
%wavelengths, x data
x = [0.000000212 0.000000231 0.000000291 3.465E-07 3.543E-07 0.000000365 0.000000445 0.000000477 0.000000551 6.231E-07 0.000000658 7.625E-07 0.000000806 9.134E-07 0.00000163];
%flux densities at one epoch (each row is an epoch).
intvaluesallSED = readmatrix("int_values_allSED_3.xlsx");
results = zeros(50,8);
for i = 1:size(intvaluesallSED,1)
flux_den_uncorrected = intvaluesallSED(i,:);
flux_den = flux_den_uncorrected - [3.044257148 2.764380323 1.849636489 1.578043671 1.551640889 1.518279132 1.295492704 1.195475706 0.975440567 0.82279158 0.761751691 0.606214695 0.552059178 0.443122732 0.168971244];
%convert flux den to fv (in SI units)
fv = 3.631e-23.*exp(-0.921034.*flux_den);
%convert fv to f_lambda
f_lambda = fv.*(c./(x.^2));
%for plotting use lambda*f_lambda - y data
y = x.*f_lambda;
%plot figure with data
figure
plot(x, y, 'o', 'MarkerFaceColor', 'b', 'MarkerSize', 5);
xlabel('Wavelength (m)');
ylabel('\lambdaf_\lambda');
%BB function and fit. Here b1 is the aplha multiplication factor and b2 is
%temp. Multiplied by wavelength(xdata) as plotted with lambda*f_lambda
BBfun = @(b,x) (b(1)).*x.*(2.*pi.*h.*(c^2)./(x.^5)).*(1./(exp((h.*c)./(x.*kb.*b(2)))-1));
beta0 = [2.2e-27 ;3e4];
[beta,R,J,CovB,MSE,ErrorModelInfo] = nlinfit(x,y,BBfun,beta0);
%Output values and errors
% b1 is alplha multiplication factor = (R/D)^2, detnoted A. b2 is temp
ci = nlparci(beta,R,"Covar",CovB);
errA = (((ci(1,2)) - (ci(1,1)))/2);
errTemp = ((ci(2,2) - ci(2,1))/2);
A = ((beta(1,1)));
Temp = beta(2,1);
Rad = sqrt(A)*1.85e+24; %convert to radius, distance to transient 1.85e+24
errRad = Rad*sqrt(((0.5*errA)/A)^2 + (4.19e+22/1.85e+24)^2); %error propagation from aplha and distance
%stephan-boltzmann constant
sb = 5.67e-08;
Lum = (4*pi*Rad^2*sb*Temp^4)*1e7; %in erg/s
errLum = Lum*sqrt((2*errRad/Rad)^2 + (4*errTemp/Temp)^2);
results(i,1) = A;
results(i,2) = errA;
results(i,3) = Rad;
results(i,4) = errRad;
results(i,5) = Temp;
results(i,6) = errTemp;
results(i,7) = Lum;
results(i,8) = errLum;
%plot fit
hold on
plot(x,BBfun(beta,x),'-r');
hold off
end
%final plots
time = [3.4421, 4.2246, 5.695, 6.6946, 7.0809, 7.2812, 8.8125, 9.6112, 10.6128, 11.5382, 11.6742, 13.3953, 14.7236, 15.6233, 16.7919, 17.0436, 18.7829, 19.1707, 20.6369, 21.7721, 22.1614, 22.1619, 23.1565, 23.1571, 23.7631, 24.2266, 25.2168, 26.1579, 26.7577, 27.4169, 29.7512, 31.5264, 33.9284, 35.2449, 37.2385, 38.8981, 39.1618, 40.3597, 41.1686, 42.1524, 44.2117, 45.8706, 46.9335, 47.2638, 48.4002, 50.1866, 52.4438, 55.7044, 60.2141, 69.4632];
figure
t=tiledlayout(3,1,'TileSpacing','none');
nexttile;
errorbar(time, results(:,7), results(:,8), '.', 'MarkerSize', 12)
xlabel('Time (MJD-58285)')
ylabel('Luminosity (erg/s)')
title('Luminosity over time')
set(gca, 'YScale', 'log')
ytickformat('%.1e')
ylim([6e41 4e45])
nexttile;
errorbar(time, results(:,3), results(:,4), '.', 'MarkerSize', 12)
xlabel('Time (MJD-58285)')
ylabel('Radius (m)')
%title('Radius over time')
ax2 = gca;
ax2.YAxis.Exponent = 0;
ytickformat('%.1e')
ylim([2.5e11 2.5e13])
nexttile;
errorbar(time, results(:,5), results(:,6), '.', 'MarkerSize', 12)
xlabel('Time (MJD-58285)')
ylabel('Temperature (K)')
%title('Temperature over time')
ax3 = gca;
ax3.YAxis.Exponent = 0;
ytickformat('%.1e')
ylim([0 4e4])
The file needed to run the code is attached as well.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Exploration and Visualization in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!