Simple MATLAB code, trying to make a line of best fit but it won\'t show up. The
ID: 3673086 • Letter: S
Question
Simple MATLAB code, trying to make a line of best fit but it won't show up. The scatter plot shows up just fine, and I put the polyfit function in, but I can't get the line of best fit to show up in the graph. Can you let me know what's wrong in the plotting part of the code? Thanks.
format long
f = 10e9:1e6:11e9; % Investigate frequencies between 10 GHz and 11 GHz
fb = 10.5e9; % Set the Brillouin frequency at 10.5 GHz
delf = 35e6; % The gain profile has a 35 MHz width
amp = 1; % Set the initial amplitude to 1
n = 1; % Loop counter
avgmax = 100; % Amount of calculations performed before the average is taken
totf = zeros(1,avgmax); % Store the calculated frequencies before average is taken
maxerr = 2e6; % Maximum allowed error, an increase of 2 MHz
bg = amp*(1+4*((f-fb)./delf).^2).^-1; % Calculate the Brillouin gain over the frequency range
while (1)
for m=1:1:avgmax % Find the frequency, averaging out the noise
lnoise = randn( size(f) ); % Calculate the normally distributed noise
measured = 0.01*n * lnoise + bg; % Add noise of standard deviation 0.1n to the gain profile
% Set up an anonymous function and search for a minimum
% x(1) = amplitude of the Brillouin gain
% x(2) = frequency, scaled by 10 GHz
% x(3) width of the profile, scaled by 10 MHz
brillouin2 = @(x) sum((measured-x(1)*(1+4*((f-x(2)*1e10)./(x(3)*1e7)).^2).^-1).^2);
[x,fval,exitflag,output] = fminsearch(brillouin2,[1,1.05,3.5]);
totf(m) = x(2);
end
std(n) = 0.01*n; % Record error and standard deviation
temperr(n) = mean( abs(totf-fb*1e-10 )) * 1e10; % Find the frequency error
if(temperr(n) > maxerr) % Check if temperature variance exceeds 2 degrees C
break % Exit loop if true
end
n = n + 1 % Increase counter
end
p = polyfit(std,temperr,1)
hold on
plot(std,temperr, 'o')
hold off
xlabel('Standard Deviation (-)')
ylabel('Frequency Offset (Hz)')
Explanation / Answer
Hi use polyval to get the points on the line over the entire range that you're interested in.You cannot interpolate to find the intercept. You must extropolate
Eample Demo,