Polyfit on semilogy. Straight line equations and intercept point
4 views (last 30 days)
Show older comments
Emanuele Sortino
on 17 Jan 2017
Commented: Emanuele Sortino
on 18 Jan 2017
Hi all. I'm trying to fit this two lines on matlab. I take the last 30 second of my experiment, the sampling is 2 data per second so i take the last 60 points. 40 before the peak and 20 after it.

I'd like to do a linear regression on the first 20 datas and then make a straight line. (Blue line) But using polyfit I've a curve on it. How can I make a straight line in a semilogy? I can't understand the equation to use in this case. I don't know where to put the 10 or the e. Also I'd like to consider the tangent from the peak to the second before. But if i'm able to do it for two point i can't elongate the red line to the blue line. Then i'd like to find the intercept of this two line. How can I do it mathematically? Anyone can help? this is the code i'm using
time=all_data(:,1);
voltage=all_data(:,3);
current=all_data(:,4);
power=current.*voltage/(0.7*2*4);
logpower=log(power);
maxt=find(power==max(power));
timebef=time(maxt-40:maxt+20);
powerbef=power(maxt-40:maxt+20);
close all
fig=figure(j);
set(fig,'units','normalized','outerposition',[0 0 1 1])
semilogy(time,power,'LineWidth',2,'Color',[0 0 0]);
xlim([min(timebef),max(timebef)]);
xlabel('time (s)');
ylabel('Log Power density mW/mm^3');
set(gca,'FontSize',16);
ylim([10,10000]);
ylabel('Pow Den mW/mm^3');
xlabel('time (s)');
title(lista(j,:));
hold on
slope=polyfit(time((maxt-40):(maxt-20)),power((maxt-40):(maxt-20)),1);
Y1=(slope(1).*time((maxt-40):(maxt+20)))+slope(2);
semilogy(time((maxt-40):(maxt+20)),Y1,'LineWidth',2,'Color',[0 0 1])
X2=[time(maxt-2) time(maxt)];
Y2=[power(maxt-2) power(maxt)];
semilogy(X2,Y2,'LineWidth',2,'Color',[1 0 0])
0 Comments
Accepted Answer
David Goodmanson
on 17 Jan 2017
Hello Emanuele, when you are looking at a semilogy plot you are interested in log(y) vs. x. So you need to go into log-land and do all the fits on the logpower variable you created. The fit
polyfit(t,logpower(t),1)
gives you a linear relationship of the form log(power) = at + b (for a and b you have slope(1) and slope(2) respectively). The actual fit is on the log, but the inverse of this relation is power = exp(at +b), so if for the blue line you do
slope = polyfit(time((maxt-40):(maxt-20)),logpower((maxt-40):(maxt-20)),1);
Y1 = exp( (slope(1).*time((maxt-40):(maxt+20)))+slope(2) );
semilogy(time((maxt-40):(maxt+20)),Y1,'LineWidth',2,'Color',[0 0 1])
then the blue line represents an exponential for the actual power and a straight line on a log plot. Same for the red line, you can do a fit on logpower to find a and b, then exponentiate and expand your x array to make a line that crosses the blue one.
3 Comments
David Goodmanson
on 17 Jan 2017
Edited: David Goodmanson
on 17 Jan 2017
The idea is to create the line in log-land first, and exponentiate it afterwards. This parallels the blue line code so it should work:
Y1 = exp( PEAK(2) + PEAK(1).*X2 );
More Answers (0)
See Also
Categories
Find more on Log Plots 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!