这是我的代码,我想在其顶部添加线性拟合。 我还想展示他拟合的方程。
start=5;
ending=50
fprintf(' ---- First definition ------ \n');
for jN=start:ending
nP_in=nnz(rmsd(:,jN));
fprintf('for timestep %d fraction of particles remaining in the
box is %2.2f \n',jN,nP_in/nP);
rd1(jN)=sum(rmsd(:,jN))/nP_in;
end
figure(1)
loglog((tM(start:ending)),(rd1(start:ending)))
xlabel('log(time (s))')
ylabel('log(<(\Delta r)>^2)')
str = sprintf(' DEFINITION 1 \nInitial number of particles = %d
Particles remaining = %d\nMonitors between tinitial and
tfinal=%d \ntinitial=%.2f tfinal=%.2f',
nP,nP_in,length(tM(start:ending)),start*tM(1,1),ending*tM(1,1));
title(str, 'FontSize', 10);
这就是我要得到的
如果我也希望拟合在图表的特定部分上,我该怎么做?enter image description here
答案 0 :(得分:0)
尝试以下
clear
close all
clc
x = (1:10)';
y1 = ((2+0.1*rand(10,1)).*x+1);
y2 = ((3+0.1*rand(10,1)).*x+2);
loglog(x,y1,'kx',x,y2,'ro') % make a log log plot
hold on
grid on
X = table(x,y1,y2);
f1 = fitglm(X,'y1~x'); %fit linear model
f2 = fitglm(X,'y2~x');
plot(X.x,predict(f1,X),':k', X.x,predict(f2,X),'--r')