How can I evaluate a function in a script in MATLAB?
1 回表示 (過去 30 日間)
古いコメントを表示
I am trying to write a function as a script file. And then put the variables in the function to get output as an array.
That's I am able to do:
function try
a = [-2 1 7.5];
ans = myfunction(a)
function y = myfunction(x)
y = 1/(x^2 + 1)
end
However, it shows solution not found. I know how to call the function on the command window but on idea how to make it done all in the script file.
Thanks in advance. Bonnie
1 件のコメント
回答 (1 件)
Image Analyst
2016 年 7 月 31 日
編集済み: Image Analyst
2016 年 7 月 31 日
Bonnie, because myfunction ended with an "end", try must also end with an "end". Ends are optional (I don't use them) but if one function in an m files uses it then all function need to use ends.
Also you need to use ./ instead of /, and .^ instead of ^ in your formula since x is an array. Corrected trythis.m is below:
function trythis
a = [-2, 1, 7.5];
answer = myfunction(a)
end
function y = myfunction(x)
y = 1 ./ (x .^ 2 + 1)
end
2 件のコメント
Walter Roberson
2016 年 7 月 31 日
Works for me. Store those two functions in trythis.m and then at the command line command
trythis
参考
カテゴリ
Help Center および File Exchange で Data Import and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!