Solve an equation through iteration in Matlab

76 ビュー (過去 30 日間)
kasim
kasim 2019 年 11 月 13 日
コメント済み: David Hill 2019 年 11 月 13 日
Hi, I am trying to solve the following 750 = x^4 + x in matlab using iteration
I want Matlab to display the values it obtains for x, I have tried to use a for loop but with no luck.
Would anyone be able to help out with a basic code I would need.
Kind regards

回答 (1 件)

David Hill
David Hill 2019 年 11 月 13 日
function b = Solution(a,b)%a and b must bound the solution (a=5.2, b=5.3)
f=@(x)x^4+x-750;
while f(b)>.000001%whatever accuracy you want here
if f((a+b)/2)<0%this is a simple half slitting technique
a=(a+b)/2;
else
b=(a+b)/2;
end
end
  2 件のコメント
kasim
kasim 2019 年 11 月 13 日
Hi David,
When running the code, I am getting the following error:
Error in untitled2 (line 5)
while f(b)>.000001%whatever accuracy you want here
David Hill
David Hill 2019 年 11 月 13 日
Works perfectly for me. Did you set it up as a function? If not, you need to assign values to a and b.
a=5.2;
b=5.3;
f=@(x)x^4+x-750;
while f(b)>.000001%whatever accuracy you want here
if f((a+b)/2)<0%this is a simple half slitting technique
a=(a+b)/2;
else
b=(a+b)/2;
end
end

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

カテゴリ

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