i need help writing code in matlab that will plot data of 4 different variables onto one graph

조회 수: 6 (최근 30일)
Help plotting data. When I do for loop nothing shows up on my plot figure.
I am given for example data like this: A b and c are just constants depending on the planets: T is the temp in celcius.
A B C
earth: 14.3145 2756.22 228.060
venus: 15.0717 3580.80 224.65
mars: 13.7819 2726.81 217.572
jupiter: 13.6608 2154.70 238.789
plot the 4 different planets datas on the same graph, plot the pressures from T=25C to T=125C with intervals of 1C
  댓글 수: 1
dpb
dpb 2021년 7월 23일
편집: dpb 2021년 7월 23일
See the examples for plot() in the documentation.
HINT: Do NOT use a loop; use an array (or at least vectors), "the MATLAB way"...if use the latter and not the former, then you'll need to use hold on, too.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2021년 7월 24일
format long g
data = {
'earth', 14.3145, 2756.22, 228.060
'venus', 15.0717, 3580.80, 224.65
'mars', 13.7819, 2726.81, 217.572
'jupiter', 13.6608, 2154.70, 238.789
}
data = 4×4 cell array
{'earth' } {[14.3145]} {[2756.22]} {[ 228.06]} {'venus' } {[15.0717]} {[ 3580.8]} {[ 224.65]} {'mars' } {[13.7819]} {[2726.81]} {[217.572]} {'jupiter'} {[13.6608]} {[ 2154.7]} {[238.789]}
temperature = vertcat(data{:,4}) - 273.15;
variable2 = vertcat(data{:,2});
[temperature, idx] = sort(temperature);
variable2 = variable2(idx);
T = 25:125;
p = polyfit(temperature, variable2, length(temperature)-1);
interpolated2 = polyval(p, T);
plot(temperature, variable2, '*', T, interpolated2, 'b-')
hold on
for K = 1 : size(data,1)
text(temperature(K), variable2(K), data{K,1});
end
hold off
ylim([min(temperature)-50, max(interpolated2)+50])
This is an excellent case of "Garbage In, Garbage Out". You are asked to predict behaviour at temperatures that are 75C to 200C higher than you have any data for, and you have very limited amounts of data to predict from. The output probably has no meaningful relationship to the inputs.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by