Finding the root of an equation using iteration

3 ビュー (過去 30 日間)
Sara Jones
Sara Jones 2018 年 10 月 27 日
編集済み: Stephan 2018 年 10 月 27 日
I am attempting to find the root of the equation 3x^3+x^2-10 using iteration. Here is the code that I have so far, however I don't think I have implemented the while loop correctly since it the equation is currently only evaluated once. Essentially, the code ought to evaluate whether the absolute value of xVal-cVal is less than 0.0001 and if so, it should print out the value of cVal and stop. Otherwise, the value of xVal should be set to equal cVal and loop back, however the code does not do this.
xVal = input('Enter a value for x:');
cVal =((10-xVal^2)/3)^(1/3);
while (abs(xVal-cVal)>0.0001)
xVal = cVal;
end
disp(cVal);

回答 (1 件)

Stephan
Stephan 2018 年 10 月 27 日
編集済み: Stephan 2018 年 10 月 27 日
Hi,
you miss to the actualize the value for cval in your while loop - Try:
xVal = input('Enter a value for x:');
cVal =((10-xVal^2)/3)^(1/3);
while (abs(xVal-cVal)>0.0001)
xVal = cVal;
cVal =((10-xVal^2)/3)^(1/3);
end
disp(cVal)
This will work.
Best regards
Stephan

カテゴリ

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