need some help with a function and calling it

1 回表示 (過去 30 日間)
slowlearner
slowlearner 2020 年 5 月 8 日
回答済み: slowlearner 2020 年 5 月 12 日
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 件のコメント
slowlearner
slowlearner 2020 年 5 月 8 日
Yea sorry about that i didnt update the lower one before post

サインインしてコメントする。

採用された回答

slowlearner
slowlearner 2020 年 5 月 12 日
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

その他の回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 8 日
編集済み: Ameer Hamza 2020 年 5 月 8 日
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 2020 年 5 月 8 日
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 件のコメント
slowlearner
slowlearner 2020 年 5 月 8 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by