No curve in plot when executing function with multiple data sets

1 vue (au cours des 30 derniers jours)
Max Kennedy Till
Max Kennedy Till le 30 Sep 2019
So I have a function to output a value of heat capacity for a given species and temperature.
function[c]=HeatCapacity(Species,T)
if Species == 1
a = 38.91; b = 3.903e-2; c = -3.105e-5; d = 8.606e-9;
Cp = [d c b a];
HeatCapacity = polyval(Cp,T)
end
if Species == 2
a = 48.50; b = 9.188e-2; c = -8.540e-5; d = 32.40e-9;
Cp = [d c b a];
HeatCapacity = polyval(Cp,T)
end
if Species == 3
a = 29.10; b = 1.158e-2; c = -0.607e-5; d = 1.311e-9;
Cp = [d c b a];
HeatCapacity = polyval(Cp,T)
end
if Species == 4
a = 29.00; b = 0.2199e-2; c = -0.5723e-5; d = -2.871e-9;
Cp = [d c b a];
HeatCapacity = polyval(Cp,T)
end
% Cp = a + b*T + c*T^2 + d*T^3
And so I want to write a script that plots dependencies of heat capacity for all four species versus temperature in the temperature range ‐100ºC <= T <= 1000ºC in a single plot
for Species = 1:4
T = -100:1:1000
HeatCapacity(Species,T);
plot(T,HeatCapacity(Species,T))
xlim([-100,1000])
ylim([0,100])
end
However this returns a blank plot with no curves. How can I fix this in order to display the four curves?

Réponses (1)

Jyotsna Talluri
Jyotsna Talluri le 3 Oct 2019
The function output variable should be initialized same as the passing variable . For plotting multiple plots on the same curve use hold on and hold off
function Capacity = HeatCapacity(Species,T)
if Species == 1
a = 38.91; b = 3.903e-2; c = -3.105e-5; d = 8.606e-9;
Cp = [d c b a];
Capacity = polyval(Cp,T)
end
if Species == 2
a = 48.50; b = 9.188e-2; c = -8.540e-5; d = 32.40e-9;
Cp = [d c b a];
Capacity = polyval(Cp,T)
end
if Species == 3
a = 29.10; b = 1.158e-2; c = -0.607e-5; d = 1.311e-9;
Cp = [d c b a];
Capacity = polyval(Cp,T)
end
if Species == 4
a = 29.00; b = 0.2199e-2; c = -0.5723e-5; d = -2.871e-9;
Cp = [d c b a];
Capacity = polyval(Cp,T)
end
end
figure();
hold on;
for Species = 1:4
T = -100:1:1000
c=HeatCapacity(Species,T);
plot(T,c)
xlim([-100,1000])
ylim([0,100])
end
hold off

Catégories

En savoir plus sur Graphics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by