linear least square method to fit the log data

This is what I have so far. Im trying to fit the line (yp) to the data points plotted. Any suggestions of what Im doing wrong? Thanks!
function LogModel()
t=[1:1:48]'; %% data for time : column vector
y=[1.7039;5.9098;10.6923;15.6497;19.0261;23.1650;25.8011;26.2864;28.0410; 28.0070;28.8502;27.0665;27.4650;
25.9110; 23.3441;21.9954;20.7284;19.1973;17.3139;15.5076;13.7446;12.5922;11.4729;
10.4418;9.1933;7.6495;7.2157; 6.0907; 5.3670;4.6935;4.3370;3.6142; 3.1584;2.8272;
2.4244;2.0813; 1.8584; 1.5881; 1.4892;1.2528;1.1232; 1.0128;0.7994;0.7104;0.6527;0.4646;
0.4801;0.4537]; %% data for auto supply
n = length(t);
yln=log(y);
A=[ones(n,2),t, yln]; %% matrix formed by basis function 1, t at all time data
coef=inv(A'*A).*(A'*yln); %% least square solution
c1=exp(coef(1)); c2=exp(coef(2)); c3=coef(3);
tp=[0:0.01:50]; %% for plotting the model functiona f(t)
yp=c1.*tp.^(c2).*exp(c3.*tp);
m=1:n;
RMSE=norm((1./m).*sum(abs(y-yln).^2)).^(0.5);
fprintf('RMSE for log model = %12.5e\n', RMSE);
plot(t,y,'o', tp, yp, '-');

댓글 수: 2

Jon
Jon 2021년 3월 18일
Please use the code button in the MATLAB answers toolbar to copy and paste your code rather than using a screen shot. The screen shot is not clear enough to read, and also it can't be copied and pasted to try running it
AJM
AJM 2021년 3월 18일
Thanks for the suggestion!

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2021년 3월 18일

0 개 추천

Are you required to use a log fit for those data?
If not, try this:
objfcn = @(b,x) b(1).*x.*exp(b(2).*x) + b(3).*x.*exp(b(4).*x);
[B,resnrm] = fminsearch(@(b) norm(y - objfcn(b,t)), rand(4,1))
figure
plot(t, y, '.')
hold on
plot(t, objfcn(B,t), '-r')
hold off
grid
This parameter set provided an acceptable fit:
B =
-22.028821282813610
-0.268737534521761
21.509885470995002
-0.159842817422680
.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

AJM
2021년 3월 18일

답변:

2021년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by