How to produce a graph with 2 sets of data on matlab?
Afficher commentaires plus anciens
Hi everyone, so i've created a code that solves an iteration for me that gives me the correct Temperature for a composition.
This leads to me needing to code for a graph Bubble Point Temperature v Composition and Dew Point Temperature v Composition.
Both in 1 graph so that I have 2 curves in the same graph.
How would I go about doing this?
Also is there a way for the graph to be produced automatically right after the code i have already coded of for provides me with the temperature?
If this doesn't make sense i'm more than happy to re-explain, i'm currently pretty sleep deprived haha.
Any help is appreciated!
Réponses (2)
Star Strider
le 9 Nov 2015
There are several ways. The easiest way is to use the hold function.
Example:
figure(1)
plot(x1, y1)
hold on
plot(x2, y2)
hold off
grid
7 commentaires
Alai Yosief
le 9 Nov 2015
Modifié(e) : Alai Yosief
le 9 Nov 2015
Star Strider
le 9 Nov 2015
My pleasure.
In my Answer, ‘x1’, ‘y1’ and the others represent vectors of your independent and dependent variables respectively. In each plot call, both vectors have to have the same number of elements.
Alai Yosief
le 10 Nov 2015
Modifié(e) : Alai Yosief
le 10 Nov 2015
Star Strider
le 10 Nov 2015
Try this:
x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
Temperature = [35, 34, 34, 34, 33, 32.9, 32.8, 32.5, 32.7, 31, 30];
figure(1)
plot(x, Temperature)
grid
xlabel('x')
ylabel('Temperature °C')
Alai Yosief
le 10 Nov 2015
Thorsten
le 10 Nov 2015
You have 12 elements in Temperature1, but only 11 in x and Temperature. Get rid of one element in Temperature1 and you are done.
Star Strider
le 10 Nov 2015
Modifié(e) : Star Strider
le 10 Nov 2015
You have to have the same number of elements in x and Temperature in the first plot, and x and Temperature1 in the second.
One way to do that is to create x1 and x2:
x1 = linspace(0, 1, length(Temperature));
x2 = linspace(0, 1, length(Temperature1));
figure(1)
plot(x1, Temperature)
hold on
plot(x2, Temperature1)
hold off
grid
legend('Temperature', 'Temperature1')
xlabel('x')
ylabel('Temperature (°C)')

I deleted the final element of Temperature1, such that x, Temperature, and Temperature1 have the same number of elements:
x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
Temperature = [272.78, 262.92, 259.74, 258.66, 258.30, 258.19, 258.18, 258.27, 258.68, 260.31, 266.70];
Temperature1 = [272.8, 270.4, 267.9, 265.2, 262.3, 259.3, 258.3, 258.5, 260.6, 262.7, 264.8];
To plot, use:
plot(x, Temperature)
hold on
plot(x, Temperature1)
or
plot(x, Temperature, x, Temperature1)
or
plot(x, [Temperature; Temperature1]')
Catégories
En savoir plus sur Spline Postprocessing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!