How to use log/exponential forms for line of best fit?
1 view (last 30 days)
Show older comments
I created a graph that contains a line of best fit using the linear form y = ax+b. It will tell me the values of a and b in the command window. However, I also want to add a line of best fit using the log/exponential form y = Ce^dx and display the values of C and d in the command window. How can I do so?
rideData; % contains the data for speed and distance
figure(1);
plot(speed, distance, "o");
p = polyfit(speed, distance, 1);
f = polyval(p, speed);
hold on
plot(climb, f, 'r');
xlabel('speed');
ylabel('distance');
slope = p(1);
fprintf('a = %f\n', slope);
intercept = p(2);
fprintf('b = %f\n', intercept);
0 Comments
Answers (1)
Star Strider
on 10 Dec 2018
You could do a log transformation, however that distorts the errors and (unless the errors are negligable), a less than optimum fit.
I prefer something like this:
eqn = @(b,x) b(1).*exp(b(2).*x);
x = speed;
y = distance;
B = fminsearch(@(b) norm(y - eqn(b,x)), [1; -1]);
fexp = eqn(B,x);
figure
plot(x, fexp)
xlabel('speed');
ylabel('distance');
0 Comments
See Also
Categories
Find more on Directed Graphs 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!