Hi @Caelan,
Thank you for sharing your code and detailed description of the issues. I have reviewed your approach and specific concerns. Here is a structured response:
Comment #1: I have code to fit a curve to a set of data (a blackbody 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."
Analysis:The second parameter, temperature, may not appear to update because the blackbody function is highly nonlinear. When the wavelengths are very small (e.g., in meters) or flux values are very low, the exponential term in the blackbody function can cause numerical underflow. As a result, `nlinfit` treats the temperature parameter as insensitive—small changes in temperature do not noticeably affect the model output.
My recommendations for comment#1
- Use dynamic initial guesses for temperature for each epoch. For example, estimate temperature from the approximate peak wavelength of your data (using Wien’s displacement approximation).
- Scale your wavelength and flux data to avoid extremely small or large numbers. For instance, convert wavelengths from meters to microns and normalize flux values to a reasonable range.
- Avoid toolbox-dependent options like robust weighting if you want to rely solely on base MATLAB.
Comment #2: 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`.
Analysis:If temperature remains constant across epochs, the model is insensitive to that parameter for certain rows. This usually occurs when the flux is very low, nearly flat, or the exponential term is extremely small.
My recommendations for comment#2
- Implement epoch-specific initial guesses for temperature to ensure `nlinfit` starts near a realistic value.
- Ensure that flux correction and scaling preserve enough variation for parameter adjustment.
- Plot each fitted curve immediately after fitting to verify that temperature updates as expected.
- Using scaled data (wavelengths in microns, flux normalized appropriately) improves convergence and prevents underflow.
References (Math-Focused Sources)
1. Nonlinear Curve Fitting with `nlinfit`: https://www.mathworks.com/help/stats/nlinfit.html
2. Example: Fit Blackbody Radiation Data: https://www.mathworks.com/help/matlab/ref/nlinfit.html#example-blackbody
3. Confidence Intervals (`nlparci`): https://www.mathworks.com/help/stats/nlparci.html
In summary, the temperature parameter issue and flat temperature plot are primarily due to numerical sensitivity and initial guesses. Using dynamic initial guesses, scaling data, and verifying fits after each epoch should resolve these problems.
Hope this helps!