Display equation of exponential fitted line using fit function

4 views (last 30 days)
Here's my code, I want to find the equation of the fitted line on either of the figures.
clc
clear
T = readtable('ViscosityResults.xlsx','Range','L2:M14',... #read spreadsheet and select range
'ReadVariableNames',true,'VariableNamingRule','preserve')... #take headings from file and keep them as they are
figure; %experimental results
x = T{:,1}
y = T{:,2}
f = fit(x,y,'exp1'); %create variables
plot(f,x,y)
xlim([0,40])
ylim([0,40])
title('Viscosity of ED1242-9001 primer by % volume water','(experimental results)')
xlabel('% Volume water')
ylabel('Viscosity (cP)') %plot and format graph
grid on
figure; %forecast graph
xx=linspace(0,80,500);
plot(xx,f(xx))
xlim([0,80])
ylim([0,40])
title('Viscosity of ED1242-9001 primer by % volume water')
xlabel('% Volume water')
ylabel('Viscosity (cP)') %plot and format graph
grid on

Accepted Answer

Davide Masiello
Davide Masiello on 8 Mar 2022
See if this works
clc
clear
T = readtable('ViscosityResults.xlsx','Range','L2:M14',... #read spreadsheet and select range
'ReadVariableNames',true,'VariableNamingRule','preserve')... #take headings from file and keep them as they are
figure; %experimental results
x = T{:,1}
y = T{:,2}
f = fit(x,y,'exp1'); %create variables
txt = sprintf('f(x)=%.2f*exp(%.2f*x)\n',[f.a,f.b]);
plot(f,x,y)
xlim([0,40])
ylim([0,40])
title('Viscosity of ED1242-9001 primer by % volume water','(experimental results)')
xlabel('% Volume water')
ylabel('Viscosity (cP)') %plot and format graph
text(min(x),max(y),txt)
grid on
figure; %forecast graph
xx=linspace(0,80,500);
plot(xx,f(xx))
xlim([0,80])
ylim([0,40])
title('Viscosity of ED1242-9001 primer by % volume water')
xlabel('% Volume water')
ylabel('Viscosity (cP)') %plot and format graph
text(min(x),max(y),txt)
grid on
  3 Comments
Walter Roberson
Walter Roberson on 10 Mar 2022
The equation is currently being displayed according to
text(min(x),max(y),txt)
so change the min(x) and max(y) to whatever values are appropriate for your purpose.
You might also want to consider instead using legend() to display the equation.
Davide Masiello
Davide Masiello on 10 Mar 2022
Yes. The first two entries of the command text() are the coordinates where the top-left corner of the bounding box of the displayed text are placed.
For more info, I suggest you check this link

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Curve Fitting Toolbox 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!