Regression line for specified x-values

2 次查看(过去 30 天)
Hi
I am searching for a way to plot regression lines, but only for given x values. I would also like to get r^2 values, change the color and linestyle to make the plot easy to understand.
My data along the x-axis is ranging from +12 to -8 and I would like to plot one line for the values from +12 to 0 and then another line from 0 to -8.
  4 个评论
John D'Errico
John D'Errico 2018-12-11
Again, you are not asking a question about MATLAB, just stating a long list of things you want to do. I'm not sure why you would expect that some very specific list of desires on your part should have a one line solution.
I told you several tools that return R^2 directly. And I gave you a link that shows how to compute it from what polyfit returns.
Again, you can plot a line. Surely you found out how to do that. Line will do it too. But, since you really want two different lines, then just plot two lines. If you look at the help docs for plot, it will show how to plot multiple lines, with different line specs for ech line. For example, this is one taken directly from doc plot.
plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')
You will see that you can set a specific color in the call to plot, although plot won't take a specific set of rgb code values as a line spec. You could do it using property/value pairs in the call to plot (but that is probably getting more complex than you can handle so far.) Or you can use handle graphics to change a color, linestyle etc.
If you are new to MATLAB, you won't learn how to use it unless you try. READ THE HELP DOCS. Try something, and see if it does what you expected. If it does not, then go back and read the help. It seems like so far, you tried a couple of random things (well, at least you tried something, better than a lot of people) but if all of that is too confusing and you are just trying random functions (refline?) then you really need to read the getting started tutorials, as you seem to be trying to run before you can walk.
So if you refuse to read the manual, then you need to go more slowly. Assuming that you understand polyfit, then it is not difficult to take what it returns and compute R^2, or use one of the alternative tools I suggested to do the regression, if you feel you really need to compute R^2.
Then learn how to use plot. You can use it to plot two lines in one call, as I show above. Or, by judicious use of the hold command, you can just call plot twice.
At some point, you may wish to learn about handle graphics. So try returning an argument from plot. See what it gies you. Then read the help for plot. For example, look at what this returns:
H = plot(rand(10,1),rand(10,1))
Now try things. Get your hands dirty. Think about what happens if you tried ths:
H.Color = 'r'
If you need to find the data points which lie below 0 in x, then what might you try? Would find help you? Try it. Better, read the help for find. Look at the examples in the doc for find.
I'm sorry that I did not tell you the exact commands to do exactly what it is that you want, but you won't learn that way. You will only learn how to dump a list of desires on Answers, and hope that someone out there feels sorry for you, thus solving whatever specific problem you have today. There is a huge amount of very well written documentation already there in place for you to use. Why not use it?
Andreas Grøvan Aspaas
Thanks for assuming the best of people. Since I do not know every possible way of solving a problem on matlab I was asking if there was a specific solution to my problem that I did not know about. It takes quite some time to read up on every way of doing something in this program and I am learing how to do things as I get stuck while analysing data as I find it more interesting than sitting watching tutorials for a week before doing something.
The problem is that T11 as defined below is values of +12 to 0. Then T12 (0 to -8) and T13 (-8 to 0) is negative values, and then T14 is positive again. I am sure there is a way of indexing it, but I do not know how yet.
I just thought there would be an easier solution than writing all this for 4 lines. Sure, it does the job, but there has to be an easier way.
T11 = Temp_mean6(1:68); A11 = AA6(1:68);
T12 = Temp_mean6(69:108); A12 = AA6(69:108);
T13 = Temp_mean6(108:124); A13 = AA6(108:124);
T14 = Temp_mean6(125:154); A14 = AA6(125:154);
p11 = polyfit(T11,A11,1);
f11 = polyval(p11,T11);
p12 = polyfit(T12,A12,1);
f12 = polyval(p12,T12);
p13 = polyfit(T13,A13,1);
f13 = polyval(p13,T13);
p14 = polyfit(T14,A14,1);
f14 = polyval(p14,T14);
figure
plot(Temp_mean6, AA6,'o','color','[1 0 0]','displayName','Array 6');
hold on
plot(T11,f11,'-','color','[0 1 0]','displayName', 'cooling');
plot(T12,f12,'--','color','[0 1 0]','displayName', 'cooling');
plot(T13,f13,'--','color','[0 0 1]','displayName', 'warming');
plot(T14,f14,'-','color','[0 0 1]','displayName', 'warming');
hold off
legend

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by