![temperature.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/197638/temperature.png)
How can I fit a line to values and plot it on the same plot as those values?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Samy Ben Thabet
am 29 Nov. 2018
Kommentiert: Samy Ben Thabet
am 30 Nov. 2018
I have 70 different temperatures that I need to plot over the course of 70 years. I was able to do that but I also have to fit a line to those values and plot it in the same plot. I tried using polyfit but when I tried to plot that it would remove my intitial plot.
Here is a picture of what I need to do ![Screen Shot 2018-11-29 at 18.01.21.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/197632/Screen%20Shot%202018-11-29%20at%2018.01.21.png)
![Screen Shot 2018-11-29 at 18.01.21.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/197632/Screen%20Shot%202018-11-29%20at%2018.01.21.png)
0 Kommentare
Akzeptierte Antwort
Akira Agata
am 30 Nov. 2018
Maybe I guess the usase of polyfit, polyval or hold function in your code is incorrect. How about the following?
% Sample data
Time = 1948:2017;
Temp = (20:0.01:20.69) + rand(1,70);
% Fit with 1st order polynomial
p = polyfit(Time,Temperature,1);
predTemp = polyval(p,Time);
% Plot the result
figure
plot(Time,Temp)
hold on
plot(Time,predTemp)
![temperature.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/197638/temperature.png)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!