Line of best fit issues

1 view (last 30 days)
CAM
CAM on 12 May 2016
Commented: CAM on 12 May 2016
I'm trying to do a line of best fit for an inverse power law, but all that's happening is I get various straight lines, no matter how I modify the initial conditions. What am I doing wrong here?
The data set is:
xdata1 = [0.04693787, 0.04950828, 0.055079095, 0.05966484, 0.06726917, 0.07483848,...
0.08550887, 0.09262812, 0.10443364, 0.11312852, 0.12925825, 0.14189875,...
0.15786561, 0.17330371, 0.19025153, 0.21165918, 0.23235789, 0.2654872,...
0.2993237, 0.34658992, 0.39076304, 0.423297, 0.4524684, 0.4967165, ...
0.5380719, 0.5675377, 0.6398708, 0.69314486, 0.7508544, 0.79197264, ...
0.8810876, 0.9802301, 1.1350182, 1.229517, 1.2968477, 1.4817507, ...
1.693017, 1.9087932, 2.123576, 2.426353, 2.628365, 2.847196, 3.1256309, ...
3.3410332, 3.8686156, 4.1907067, 4.5396147, 4.9175715, 5.3984737, 5.7705083,...
6.2509465,6.593261, 7.335153, 7.6344023, 8.38099, 8.958565, 9.834646, 10.796403, ...
11.387635, 12.501262, 13.542086, 14.669565, 15.268034, 16.320227, 17.444931, ...
18.156626, 19.932209, 19.932209, 22.175037, 23.079704, 24.670237, 27.082804, ...
29.731304, 32.206657, 33.520576, 34.888103, 35.830643, 39.334614, 42.045353,...
45.54594];
ydata1 = [469.75888,399.54205,358.66376,289.0264,220.6734,177.82794,143.30125,...
109.411385,93.057205,74.98942,67.31704,48.69675,46.138397,37.180267,29.961428,...
22.875732,19.4564,14.855081,12.634629,9.139817,7.77365,6.61169,5.327979,4.7828584,...
3.854229,3.4598916,2.7881267,2.2467902,1.9109529,1.5399265,1.3097472,0.9474635,...
0.80584216,0.64938164,0.55231583,0.4216965,0.33982083,0.24582441,0.17782794,...
0.14330126,0.103663296,0.08816831,0.07104974,0.048696753,0.0392419,0.026895989,...
0.022875732,0.015678788,0.011970851,0.008659643,0.005048066,0.003854229,0.002788127,...
0.00224679,0.001382372,9.47E-04,7.23E-04,4.70E-04,3.22E-04,2.21E-04,1.88E-04,...
1.51E-04,1.09E-04,8.35E-05,6.04E-05,5.14E-05,3.92E-05,3.52E-05,2.84E-05,...
2.17E-05,1.33E-05,9.65E-06,6.98E-06,5.33E-06,4.29E-06,3.85E-06,2.50E-06,...
1.91E-06,1.63E-06,1.18E-06];
And the main code is: clc clear all %All three below simply call the data files which we are to use. ElectronData; % here we define the function f1 = @(b,x) 1./(b(1).*(x).^b(2)+b(3).*(x).^b(4)+b(5).*(x).^b(6));
xdc1 = linspace(min(xdata1), max(xdata1), 150);% Continuous ‘xdata’ for each
b0 = [1; 1; 1; 1; 1; 1];%initial estimate vectors
B1 = nlinfit(xdata1, ydata1, f1, b0);%non-linear regression
%below, we're simply graphing the thing
figure(1)
loglog(xdata1, ydata1, 'bp') %ElectronData
hold on
loglog(xdc1, f1(B1,xdc1))%Best fit for Electrons
hold off
grid
%and now for the titles and lables
title('Low and High Energy Cosmic Ray Spectra')
xlabel('KE')
ylabel('Flux')
legend('Helium Data', 'Electron Data', 'Proton Data', 'Location','Southwest')
  1 Comment
CAM
CAM on 12 May 2016
I should also note that loglog is necessary to properly view the data. If you do a normal plot, given the range of the data, it doesn't display correctly!

Sign in to comment.

Answers (0)

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!