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

1 次查看(过去 30 天)
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

回答(2 个)

David Sanchez
David Sanchez 2013-12-17
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 个评论
Aaron
Aaron 2013-12-17
编辑:Image Analyst 2013-12-18
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2013-12-17
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 个评论
Aaron
Aaron 2013-12-17
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 2013-12-17
Notice I also switched W_2 and temp_vector, as the independent variable should go first in the plot()

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by