plot scatter and line in same grid

261 ビュー (過去 30 日間)
Morley
Morley 2011 年 10 月 26 日
I am given a table of values that I am supposed to find a linear equation for then I am supposed to plot them both together.
Basically a scatter plot with a line of best fit
But through using the hold on command my graph won’t plot them both it only comes up with the scatter.
Help!!
Heres my code down to the sweet point
------------------------------------------------------
h=[0 2000 5000 7500 10000 20000 26000];
t=[212 210 203 198 194 178 168];
x=[0:1:3]
y=-.0017*x+211.88
scatter(h,t)
hold on
plot(x,y)
hold off
------------------------------------------------------
its only plotting the scatter
help appreciated

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 10 月 26 日
x=[0:1:3]*10000 will solve your problem.

その他の回答 (3 件)

Patrick Kalita
Patrick Kalita 2011 年 10 月 26 日
They're both there; they are just on vastly different scales. Note that the x-data of the line goes from 0 to 3. The x-data of the scatter goes from 0 to 26000. At that scale, the line from 0 to 3 is way too small to be seen.
Perhaps you want something more like this:
h=[0 2000 5000 7500 10000 20000 26000];
t=[212 210 203 198 194 178 168];
x= linspace(0,26000); % <--- much larger range
y=-.0017*x+211.88
scatter(h,t)
hold on
plot(x,y)
hold off

Daniel Shub
Daniel Shub 2011 年 10 月 26 日
It might even be easier to just use lsline (assuming when you say best fit you mean mmse)...
scatter(h,t)
lsline

Wayne King
Wayne King 2011 年 10 月 26 日
Hi Your h range and your x range are very different. You are not making clear what your data is.
Is h really your x measurements? Is t really your y measurements?
If so then why aren't you fitting a line to h?
h=[0 2000 5000 7500 10000 20000 26000];
t=[212 210 203 198 194 178 168];
scatter(h,t); hold on;
y=-.0017*h+211.88;
plot(h,y);

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by