Proper way to use exist function.

25 ビュー (過去 30 日間)
Jesse Crotts
Jesse Crotts 2018 年 12 月 9 日
編集済み: Stephen23 2018 年 12 月 9 日
I am using the ode23 function to call another function. The problem is that I need to change the time value each time the function is called in order to change another variable. Here is the part of the function that is not working to count time:
function f = wdotz(t,w,M,K,Q)
% -------------------------------------------------------
% time counter
AKA = exists(i);
if AKA == 0
i = 0
end
i = i+1;
time = (0:.001:22.5)';
treal = time(i);
% -------------------------------------------------------
when I run the code that calls this function it gives me this error:
Error: File: wdotz.m Line: 7 Column: 5
"i" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable.
A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly
using load or eval.
  2 件のコメント
Stephen23
Stephen23 2018 年 12 月 9 日
編集済み: Stephen23 2018 年 12 月 9 日
"Proper way to use exist function."
I would argue that the "proper" way to use exist on a variable is to NOT use it: introspective programming is inherently inefficient, which is why experienced users and the MATLAB documentation warn against using it: "Avoid functions that query the state of MATLAB such as inputname, which, whos, exist(var), and dbstack. Run-time introspection is computationally expensive."
In any case, it is not clear what you expect your code to do. Please explain the expected behavior.
Walter Roberson
Walter Roberson 2018 年 12 月 9 日
i will exist because it is a function that returns sqrt(-1)

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

採用された回答

Image Analyst
Image Analyst 2018 年 12 月 9 日
In your main, calling code, you do this:
f = wdotz(t,w,M,K,Q)
Note that none of the variables is called i. So there is never ever any i in your code, until you assign it.
exists() is not a function, but exist() is, and you'd need to pass in a second argument of 'var':
iExists = exist(i, 'var')
time() is a built-in function so don't use that as a variable name.
You're passing in t, which is perhaps a "time so far". If you're trying to return the elapsed time since t just do this
f = toc(t);
and get rid of everything else except the function line.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 12 月 9 日
The time value is being received into t and the ode routines will modify it as necessary to achieve integration tolerances.
  2 件のコメント
Jesse Crotts
Jesse Crotts 2018 年 12 月 9 日
I have discovered that I can use the time variable built into the ODE function. The time variable also changes a force input. Thats why I needed it. However, I am wondering why the exist function does not work the way that I am using it?
Stephen23
Stephen23 2018 年 12 月 9 日
編集済み: Stephen23 2018 年 12 月 9 日
Because i always exists:
And if that i is not what you expect to be testing for, what do you expect to test the existence of?

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by