How can I plot my function?

조회 수: 1 (최근 30일)
N/A
N/A 2014년 1월 14일
댓글: Youssef Khmou 2014년 1월 15일
My program is supposed to plot a function, but every time I try this I either end up with an empty plot or with an error. The code is this:
function Q = hydrogenusage(P)
Q = (120*P)-(P^2);
plot(P,Q)
xlabel('Power')
ylabel('Usage')
title('Hydrogen usage')
grid on
end
The error I get is 'Not enough input arguments'. Okay, I thought, it needs some kind of input. So I entered:
hydrogenusage(0)
But if I try that it gives me an empty plot. I tried varying the 0 value I added as input, but it didn't work. How can I make this work?

채택된 답변

Youssef  Khmou
Youssef Khmou 2014년 1월 14일
You need to provide a vector as the input to get a result of the equation Q , but you need to use element wise operations , alter your function :
function Q = hydrogenusage(P)
Q = (120*P)-(P.^2);
plot(P,Q)
xlabel('Power')
ylabel('Usage')
title('Hydrogen usage')
grid on
end
  댓글 수: 2
N/A
N/A 2014년 1월 15일
Thank you! It worked. But can I make it so that I just have to type in 'hydrogenusage' (so not hydrogen (0:120) ? I tried using:
axis([xmin xmax ymin ymax])
But alas, that only sets the limits of the axis itself. I'd like to preset the number of data points it should calculate before plotting. Is that possible?
Youssef  Khmou
Youssef Khmou 2014년 1월 15일
the question is not clear yet, you mean a function without input? or only axis adjustment ? inside the function put this line :
axis([P(1) P(end) Q(1) Q(end)])

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2014년 1월 14일
It is plotting, but it is only plotting a single point because you only asked it to plot a single point. You need to pass in a vector of points such as
hydrogenusage(0:10)
You will find you also need to change P^2 to P.^2

카테고리

Help CenterFile Exchange에서 Surfaces, Volumes, and Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by