need some help with a function and calling it

1 Ansicht (letzte 30 Tage)
slowlearner
slowlearner am 8 Mai 2020
Beantwortet: slowlearner am 12 Mai 2020
I've come quite far on my script but function and calling them is still hard i was thinking of calling it for an equation, and now I'm tying it out in an if statement before putting it in the loop. The function looks like:
function z = SEKDrive(x,y) %cost for the driving distance
z = 0.17 * x*(y/1000);%nissan leaf e+ 17kwh/100km
end
and the if statment like this:
x = input('How many km is the driving distance? ')
if x > 0 % need to make an && for SOC%
o = SEKDrive(x,SEK_in);
sekleft = SEK_ut - o
else isnan(t2)
disp('number please')
end
However matlab's been telling me its to many input values, and on both the books I have it's kind similar to what i did but it works and mine doesn't.
edited abit late tho
  1 Kommentar
slowlearner
slowlearner am 8 Mai 2020
Yea sorry about that i didnt update the lower one before post

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

slowlearner
slowlearner am 12 Mai 2020
solved with SEK_in was a [10x1] array and not the correct input the script was supposed to be:
q = input('How many km is the driving distance? '); %need to include this in the loops somehow
if q > 0 %need to make an && for SOC%
o = SEKDrive(q,SEK_bat); %call the function SEKDrive
sek_left = SEK_in - o
and the function as corrected above

Weitere Antworten (2)

Ameer Hamza
Ameer Hamza am 8 Mai 2020
Bearbeitet: Ameer Hamza am 8 Mai 2020
SEKDrive have two input arguments. You should pass both.
o = SEKDrive(x, 5); % y=5 for example. You should change it according to your own requirements.
See here for basics of MATLAB functions: https://www.mathworks.com/help/matlab/ref/function.html

Steven Lord
Steven Lord am 8 Mai 2020
Your function is defined to accept and use two input arguments.
function SEK_ut = SEKDrive(x,y) %cost for the driving distance
You are only specifying one of those input arguments when you call it. [I'm assuming either that your SEKDrive function is defined in a function SEKdrive.m or you're actually calling it in your code as SEKDrive. The case of function and function file names matters in MATLAB.]
o = SEKdrive(x);
What value or values should MATLAB use when the commands inside SEKDrive try to operate on y?
It's as though you asked me "I want you to add two numbers. The first is 3. What's the total?" I don't have enough information to answer the question, and when MATLAB encounters that situation often its response is an error like:
>> plus(3)
Error using +
Not enough input arguments.
To correct the problem, specify a value or a variable for you as the second input when you call SEKDrive or modify the SEKDrive function so it only accepts and uses one input argument.
  1 Kommentar
slowlearner
slowlearner am 8 Mai 2020
Didnt rereview the post before posting it but there are 2 values in the function SEK_in is 146,42 it's a sum of two arrays devided by eachother

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by