MATLAB Answers

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

1 view (last 30 days)
Neonmd
Neonmd on 23 Jul 2021
Answered: Walter Roberson on 24 Jul 2021
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 Comment
dpb
dpb on 23 Jul 2021
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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 24 Jul 2021
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.

Community Treasure Hunt

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

Start Hunting!