Getting only one output from a function with several ones

6 ビュー (過去 30 日間)
Michael Bettar
Michael Bettar 2017 年 10 月 22 日
コメント済み: Star Strider 2017 年 10 月 22 日
Hey! I am trying to create a function that approximates the root(first output) with help of newton's method and also save every loop value(second output) in a vector called resultVec. Here's the code:
function [x1, resultVec] = newt(x0)
x1 = x0 - ((exp(-x0)-2.*(x0.^3)+(5.*x0))/(-exp(-x0)-6.*(x0.^2)+5));
resultVec = [];
while abs(x1-x0)>=1e-10
x0 = x1;
x1 = x0 - ((exp(-x0)-2.*(x0.^3)+(5.*x0))/(-exp(-x0)-6.*(x0.^2)+5));
resultVec = [resultVec x1];
end
end
The problem is that I am only getting the output value for x1, what am I doing wrong?

採用された回答

Star Strider
Star Strider 2017 年 10 月 22 日
You must not be asking for both outputs.
Try something like this in your calling script:
[x1, resultVec] = newt(x0);
That should return both outputs.
  3 件のコメント
Michael Bettar
Michael Bettar 2017 年 10 月 22 日
Oh I got it now thanks for the answer
Star Strider
Star Strider 2017 年 10 月 22 日
As always, my pleasure.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by