Function does not calculate desired variable.

1 回表示 (過去 30 日間)
Giuseppe
Giuseppe 2014 年 5 月 20 日
コメント済み: Friedrich 2014 年 5 月 21 日
Hi I have the following function;
function [EV,N,t,S] = EVjc300293(EV,N,t,S)
% If a variable is set to zero calculate its value.
% 1. Choose which variable to process.
if EV==0
EV = log2((N^2)/t)-log2(S/100);
elseif N==0
N = sqrt((2^(EV+log2(S/100)))*t);
elseif t==0
t = (N^2)/(2^(EV+log2(S/100)));
elseif S==0
S = 100/(2^(EV-log2((N^2)/t)));
end
end
I want to make it so that if a variable is set to zero in the function its value is calculated. I wish to do this using the if elseif statements listed. However when I run it it always calculates EV and not any other variable?
Thanks.
  2 件のコメント
Friedrich
Friedrich 2014 年 5 月 20 日
How do you call it? Name some examples
Giuseppe
Giuseppe 2014 年 5 月 20 日
I call it like this;
[N] = EVjc300293(10,0,3,5)
Where here for example I am trying to find N?

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

採用された回答

Friedrich
Friedrich 2014 年 5 月 20 日
N is your second output so you would need to call it like this:
>> [~,N] = EVjc300293(10,0,3,5)
  2 件のコメント
Chandrasekhar
Chandrasekhar 2014 年 5 月 21 日
In this answer only N is being calculated since the if EV==0 condition has failed.. what abt others??
Friedrich
Friedrich 2014 年 5 月 21 日
The other variables arent't calculated. Since output and input variables have the same name they are copied to the output variables. Your function always return 4 variables and you need to know/figure out which output variables are relevant for you:
[a,b,c,d] = EVjc300293(10,0,3,5)
You will see that a is 10, b got calculated and c is 3 and d is 5.

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

その他の回答 (1 件)

Chandrasekhar
Chandrasekhar 2014 年 5 月 20 日
In the function that you have written EV, N ,t,s are in separate if -else if conditions and only on of them executes at a time.
in you case if EV==0 is getting satisfied and hence only EV is being calculated and the else if and else conditions are not getting executed.
one solution is to assign a default value for all the value at the beginning of the function.
  1 件のコメント
Giuseppe
Giuseppe 2014 年 5 月 20 日
Could you further explain this idea please?

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

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by