Nesting function trouble with undefined variable

2 ビュー (過去 30 日間)
aldburg
aldburg 2017 年 11 月 19 日
編集済み: Stephen23 2017 年 11 月 19 日
Trying to code a simple nesting function and I can't understand why I get an undefined function or variable warning for 'a'
function [Y] = nesty(d,e)
Y(d,e)=2*a;
nestmini
function [a] = nestmini(d,e)
a=d+e;
end
end

採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 19 日
function [Y] = nesty(d,e)
a = nestmini;
Y(d,e)=2*a;
function [a] = nestmini(d,e)
a=d+e;
end
end
Remember, the variable named as output in a function is a dummy variable that is not assigned outside the function
  5 件のコメント
Stephen23
Stephen23 2017 年 11 月 19 日
編集済み: Stephen23 2017 年 11 月 19 日
@aldburg:
  • Y=... defines a totally new variable with the name Y. It has the size of whatever the RHS has.
  • Y(d,e)=... uses indexing to allocate values into the selected elements of Y. If Y does not exist before that indexing then MATLAB creates Y and fills the unindexed elements with zero.
The introductory tutorials teach these kind of basic MATLAB concepts, such as defining variables and using indexing, and are highly recommended for all beginners:
aldburg
aldburg 2017 年 11 月 19 日
Thanks for explaining that. I will start that once I finish my courses for the quarter. I am learning Matlab to help with my composite mechanics homework.

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

その他の回答 (1 件)

KL
KL 2017 年 11 月 19 日
You're passing d,|e| to nesty function but you're using a on the right hand side of the equation. Do not expect matlab to call the nestmini function for you.
If you want to use the function return directly, try something like,
Y(d,e) = 2*nestmini(d,e);

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by