Effacer les filtres
Effacer les filtres

How can I plot multiple results from a function on the same graph?

1 vue (au cours des 30 derniers jours)
Aaron
Aaron le 17 Déc 2013
Modifié(e) : Image Analyst le 18 Déc 2013
Hello everyone,
I'm working on a Windchill conversion assignment, any guidance would be highly appreciated.
I want to display each result from the function (windchill_calc) - which takes a pre-defined temperature vector and a user inputted vector (of 5) and works out windchill (W_2)
Except the plot is only showing the result from the 5th value of the vector, I understand why this is happening and that this is quite a simple error but I can't work out a solution - any ideas?
(temp_vector is previously defined)
_______________________
windspeed_user = input('please enter a 5 windspeed vector'); %requesting input from user.
i = 1;
for i = 1:1:5;
W_2 = Windchill_calc(temp_vector, windspeed_user(i)) %calls function
end
plot (W_2, temp_vector)
xlabel ('Temperature (Fahrenheit)')
ylabel ('Windchill results')
grid on

Réponses (2)

David Sanchez
David Sanchez le 17 Déc 2013
I guess you have to work a bit with your data, but the hold on, hold off is what you need:
windspeed_user = input('please enter a 5 windspeed vector'); %requesting input from user.
i = 1;
for i = 1:5;
W_2 = Windchill_calc(temp_vector, windspeed_user(i)) %calls function
hold on
plot (W_2, temp_vector)
end
hold off
xlabel ('Temperature (Fahrenheit)')
ylabel ('Windchill results')
grid on
  1 commentaire
Aaron
Aaron le 17 Déc 2013
Modifié(e) : Image Analyst le 18 Déc 2013
Yeah your right I do - I'm now trying to embellish a little, do you know why this if statement isn't working? (I want to limit the amount of values user inputs)
This is the result after adding the 'hold' feature, thank you for your fast reply.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 17 Déc 2013
Each iteration of the loop, you are over-writing all of W_2, so at the end of the loop only the final assignment has had any effect.
It appears to me that your W_2 would be a vector. If so, then
for i = 1 : 5
W_2 = Windchill_calc(temp_vector, windspeed_user(i)) %calls function
plot(temp_vector, W_2);
hold on
end
  2 commentaires
Aaron
Aaron le 17 Déc 2013
Mm, that exactly what was happening - because as you said its the last result stored.
The hold function solved it instantly, :). Thankyou for your help and quick reply.
Walter Roberson
Walter Roberson le 17 Déc 2013
Notice I also switched W_2 and temp_vector, as the independent variable should go first in the plot()

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements 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