Finding Best Fit for Log Scaled Graphs
2 views (last 30 days)
Show older comments
Hi, I am attempting to find the best fit for a log scaled graph but have no idea how to do it. My code is as below,
x = [0, 0.00027036151034654, 0.00115093883458806, 0.00223432233808507, 0.00539981900723518, 0.00809536518833309, 0.0111270389729589, 0.014777933095254, 0.0189175724121603, 0.0235573556054278, 0.0287025567374752, 0.0343600870656987, 0.0582919612991634, 0.0682154843903124, 0.0790320917901928, 0.0914334718128318, 0.104973874288574, 0.119722291281348, 0.135699592328357, 0.240771355163788, 0.278078798463323, 0.314454995935481, 0.561768676836072]
y = [0.30371074055921, 0.290537098717707, 0.324374702814966, 0.337252335934006, 0.38897792790656, 0.403745877906912, 0.415277428739248, 0.427209812186528, 0.438112837361668, 0.448214463712894, 0.457622869575205, 0.466422006319539, 0.51823177314633, 0.526699880477541, 0.534668807004341, 0.543031975970824, 0.55093440322382, 0.558408580520752, 0.565435121097348, 0.622450007115967, 0.631025115825886, 0.637361264649512, 0.685778962718182]
set(gca,'YScale','log')
set(gca,'XScale','log')
plot(x,y)
I tried using polyfit but can't seem to find the right approach to get a best fit still. I still want to use the log-scaled graph, so I think simply logging the x and y values doesn't work too.
Any help will be much appreciated!!!
0 Comments
Answers (1)
Star Strider
on 27 Oct 2020
It is best to fit the power function using a nonlinear approach:
x = [0, 0.00027036151034654, 0.00115093883458806, 0.00223432233808507, 0.00539981900723518, 0.00809536518833309, 0.0111270389729589, 0.014777933095254, 0.0189175724121603, 0.0235573556054278, 0.0287025567374752, 0.0343600870656987, 0.0582919612991634, 0.0682154843903124, 0.0790320917901928, 0.0914334718128318, 0.104973874288574, 0.119722291281348, 0.135699592328357, 0.240771355163788, 0.278078798463323, 0.314454995935481, 0.561768676836072]
y = [0.30371074055921, 0.290537098717707, 0.324374702814966, 0.337252335934006, 0.38897792790656, 0.403745877906912, 0.415277428739248, 0.427209812186528, 0.438112837361668, 0.448214463712894, 0.457622869575205, 0.466422006319539, 0.51823177314633, 0.526699880477541, 0.534668807004341, 0.543031975970824, 0.55093440322382, 0.558408580520752, 0.565435121097348, 0.622450007115967, 0.631025115825886, 0.637361264649512, 0.685778962718182]
objfcn = @(b,x) b(1).*x.^b(2);
[B,resnorm] = fminsearch(@(b) norm(y - objfcn(b,x)), rand(2,1))
figure
plot(x, y, 'p')
hold on
plot(x, objfcn(B,x), '-r')
hold off
grid
hold off
text(0.25, 0.45, sprintf(' y = %.3f\\cdotx^{%.3f}', B))
figure
loglog(x, y, 'p')
hold on
plot(x, objfcn(B,x), '-r')
hold off
grid
hold off
text(5E-4, 0.45, sprintf(' y = %.3f\\cdotx^{%.3f}', B))
producing (for the loglog plot):
.
0 Comments
See Also
Categories
Find more on Title 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!