Program doesn't calculate with input
이전 댓글 표시
My program is supposed to take input and use this to calculate the corresponding hydrogen. My code is this:
function Q = hydrogenusage(P)
P = 0:1:4000;
Q = 79.987*P+42.942;
plot(P,Q)
xlabel('Power (W)')
ylabel('Usage (l/minute)')
title('Hydrogen usage (no leak)')
axis([0 60 0 4000])
grid on
if nargin == 0
Q = input('Enter the power to get the hydrogen usage: ');
%This needs a reference to the power of the program by Erik Rijckaert.
end
Entering 'hydrogenusage' gives me:
Enter the power to get the hydrogen usage
I enter for an example 40
If I fill that in my formula it gives me Q = 3242.422.
But when I enter 40 it gives me this:
ans =
40
It looks wonderful and it very accurately repeats what I just entered but I'd like it to give me the answer of the function.
How can I make this happen?
답변 (2개)
Walter Roberson
2014년 1월 16일
The first few lines of your code ignore any parameter passed in and assign a vector to P and assign a vector of values to Q based upon that vector. Then the bottom portion tests whether any parameters were passed (even though they were otherwise ignored) and if not then uses input() to overwrite the Q (or P?) that had been assigned before, but does not calculate any Q value.
I suggest,
function Q1 = hydrogenusage
P = 0:1:4000;
Q = 79.987*P+42.942;
plot(P,Q)
xlabel('Power (W)')
ylabel('Usage (l/minute)')
title('Hydrogen usage (no leak)')
axis([0 60 0 4000])
grid on
Pt = input('Enter the power to get the hydrogen usage: ');
Q1 = interp1(P, Q, Pt);
Amit
2014년 1월 16일
I am not sure if why you defining P inside the function if you are taking it as a input. Moreover, I think you need to ask for input before doing any calculations. Something like this:
function Q = hydrogenusage(P)
%P = 0:1:4000;
if nargin == 0
P = input('Enter the power to get the hydrogen usage: ');
%This needs a reference to the power of the program by Erik Rijckaert.
end
Q = 79.987*P+42.942;
plot(P,Q)
xlabel('Power (W)')
ylabel('Usage (l/minute)')
title('Hydrogen usage (no leak)')
axis([0 60 0 4000])
grid on
카테고리
도움말 센터 및 File Exchange에서 Quantum Mechanics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!