How do I make this graph in just one expression?

Hello! I'm fairly new to matlab and I wonder how to properly plot a graph with its line of regression in just one expression as when I tried to convert it to a .tex file it only showed the inital data points and not the regression. Since these two are two seperate graphs layered on top of another i wonder if there is a way to merge them. Thanks in advance!
Here is the code:
ir=[0.03485,0.0294,0.0198,0.0074]; t_ir=[0.372,0.233,0.108,0.037]
t_ir = 1×4
0.3720 0.2330 0.1080 0.0370
p=polyfit(log(ir),log(t_ir),1);
k=p(1);
m=p(2);
f=@(ir)k.*ir+m;
xMin=min(log(ir));xMax=max(log(ir));
hold on
plot(log(ir),log(t_ir),'o'); xlabel('ln(innerradius/m)'); ylabel('ln(time/s)');
fplot(f,[xMin,xMax])

 Accepted Answer

Each of the following two methods does the plotting with one line of code.
ir = [0.03485,0.0294,0.0198,0.0074];
t_ir=[0.372,0.233,0.108,0.037];
log_ir = log(ir.');
log_t_ir = log(t_ir.');
Method 1: Use polyfit, and plot both lines (data and fit) at once:
p=polyfit(log_ir,log_t_ir,1);
k=p(1);
m=p(2);
figure
plot(log_ir,k.*log_ir+m,'r-',log_ir,log_t_ir,'bo');
xlabel('ln(innerradius/m)');
ylabel('ln(time/s)');
Method 2: Use fit, and plot the returned cfit object along with the data:
cf = fit(log_ir,log_t_ir,'poly1');
figure
plot(cf,log_ir,log_t_ir,'o')
xlabel('ln(innerradius/m)');
ylabel('ln(time/s)');

More Answers (0)

Categories

Asked:

on 18 Oct 2023

Edited:

on 18 Oct 2023

Community Treasure Hunt

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

Start Hunting!