I am converting a program from c++ to matlab. I have written this code, but keep getting an error that reads "undefined function or variable x_old". How an I fix this?
This is my code so far:
num = input('Enter a positive number: ')
acc = input('Enter convergence criterion: ')
approx = input('Enter initial apporiximation: ')
x = approx;
while abs(x-x_old)>acc
x_old = x;
y = 1/num*x*x;
x = x/8*(15-y*(10-3*y));
end
disp(['Square root of' num2str(num) 'is' num2str(x)])

1 件のコメント

Adam Danz
Adam Danz 2019 年 12 月 15 日
When you get to this line of code
while abs(x-x_old)>acc
how do you expect Matlab to interpret x_old since this is the first time that variable is appearing?

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

 採用された回答

Guillaume
Guillaume 2019 年 12 月 15 日
編集済み: Guillaume 2019 年 12 月 15 日

0 投票

"How an I fix this"
By defining x_old before using it (in the while loop)?
Perhaps:
x_old = Inf;
while abs(x-x_old)>acc
%...

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeComputational Geometry についてさらに検索

タグ

質問済み:

2019 年 12 月 15 日

編集済み:

2019 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by