错误的Y截距为loglog图的最佳拟合线

时间:2018-01-21 05:21:34

标签: matlab plot matlab-figure linear-regression loglog

我已经尝试过许多指南中的解决方案,为我的loglog情节绘制最佳拟合线,但我不断得到一条似乎具有正确斜率的线,但y-intercept也是如此大。该图表绘制在最后一块代码中。

剧情的截图示例:

Example screenshot of plot

N=2; % Starting/current matrix size
M=1; % Number of trials per matrix
incr=2; %matrix size increment
reps=20; %number of increments
all_errs=zeros(reps,1); %vector of mean erros for each N
used_N=zeros(reps,1); %values of N used for plot

%find M such that max margin of error <0.01 for confidence interval
desired_margin=0.01; %desired margin of error
conf=0.98; %value for confidence interval
curr_margin=conf/sqrt(M); %current margin of error

while curr_margin>=desired_margin
    M=M+1;
    curr_margin=conf/sqrt(M);
end

for h=1:reps
    used_N(h)=N; %add current value of N to list of used N values
    errs=zeros(M,1); % Vector of errors
    x=ones(N,1); % exact solution vector

    for i=1:M
        A=spdiags(rand(N,3), -1:1, N,N); %constructs tridiagonal matrx with random entries
        b=A*x; % Compute the right-hand side vector
        z=A\b; % Solve the linear system
        errs(i)=max(abs(z-x)); % Compute the error
    end

    mean_err=mean(errs);
    all_errs(h)=mean(errs);
    N=N+incr; %increments N

end
%disp(all_errs)

p=polyfit(log10(used_N), log10(all_errs), 1); %fits line to data
fit=exp(polyval(p,log10(used_N))); %y-coordinates of best fit line
loglog(used_N, all_errs, 'o',used_N,fit,'-'); %plots the error versus N in a loglog plot w/best fit line
title(['Log-Log Plot for Matrix Size vs. Mean Error'],'fontsize',14)
xlabel('log_{10}(Size of Matrix)','fontsize',12)
ylabel('log_{10}(Mean Error)','fontsize',12)

1 个答案:

答案 0 :(得分:4)

您在polyfit之前获取日志库10,但之后使用e转换回来。相反,你应该使用10。

fit=10.^(polyval(p,log10(used_N)));