How to combine two trendline from two figures and find the angle?

1 view (last 30 days)
Halo, i have asked about the same thing before and also i have searched for the solution on Google but still havent found it yet. I have some 117*1 data. Time, x, and y. I make plot for time-x and time-y. So i get two plots and also two trendlines which i get from polyfit. Now im so confused about how to combine these trendlines until i get the intersection and calculate the angle between them. Let's say time=z, x=x, and y=y. So then the plots are z-x and z-y. My teacher told me to look for a solution if z-x and z-y become x-y. Do i need to do some rotation and tranformation? Please help me. Thankyouu.
aa=polyfit(time,x,1); bb=polyval(aa,time); figure plot(time,x) hold on plot(time,bb) cc=polyfit(time,y,1); dd=polyval(cc,time); figure plot(time,y) hold on plot(time,dd)

Accepted Answer

Roger Stafford
Roger Stafford on 7 Feb 2018
Edited: Roger Stafford on 7 Feb 2018
polyfit(x,y,1) will give you [p,q] which represents the line y = p*x+q, so ‘p’ will be the slope of such a fitted line. Therefore, to obtain the angle between two such lines, do this
pol1 = polyfit(x1,y1,1); % Where x1,y1 are data for the first line
pol2 = polyfit(x2,y2,1); % and x2,y2 are data for the second line
p1 = pol1(1); % First slope
p2 = pol2(1); % Second slope
ang = atan((p2-p1)/(1+p2*p1));% Angle counterclockwise from line 1 to line 2
This will give an angle lying between pi/2 and -pi/2. If you want it to lie between 0 and pi, replace the last line above by
ang = mod(atan((p2-p1)/(1+p2*p1)),pi);
[Note: You do not need to find the intersection of the lines to determine this angle.]

More Answers (0)

Community Treasure Hunt

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

Start Hunting!