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