Numerical Methods to solve Equation

4 次查看(过去 30 天)
I am trying to solve the equation f(x) = x^3 + x - 1 , by using Bisection method within the interval [ 0 , 1] , i have succeeded to generate a code to solve this equation but by using " while " function for looping , i need some one to help me to solve it by using " for " function , could any one help me to do that ? the code is :
% Numerical Program to find the roots of % the equation f(x) = x^3 + x - 1 % By using Bisection Method within x = [ 0 , 1 ] , 1st method clear ; clc ; close all a = 0 ; b = 1 ; error = 1e-16 ; while ( b - a )/2 > error % by using " while " function c = ( a + b )/2 fc = c^3 + c - 1 ; if fc < 0 a = c ; else b = c ; end end
Thanks in advance Razi

采纳的回答

David Goodmanson
David Goodmanson 2017-5-2
编辑:David Goodmanson 2017-5-2
Hi Razi, the usual procedure is to use the 'break' condition
start with a value (an initial value may or may not be needed)
for n = 1:large
do something, calculate a new value
if <some condition is met>
break
end
maybe do something else here
end
use the last computed value
The break procedure knocks you out of the for loop and terminates it.
  2 个评论
Razi Naji
Razi Naji 2017-5-2
Ok David , thanks , i am trying now to solve it :-) , i will inform you if solved :)
Razi Naji
Razi Naji 2017-5-2
Hello again David I did it , thanks for your hint :-) this is the code :
% Numerical Program to find the roots of % the equation f(x) = x^3 + x - 1 % By Bisection Method within x = [ 0 , 1 ] , 1st method clear ; clc ; close all a = 0 ; b = 1 ; n = 100 ; r = 0.6823278038 ; error = 0.5e-6 ; iteration = 0; for i=1:n iteration = iteration + 1 c = (a+b)/2 fc = c^3 + c - 1 ; if ( fc<0) a = c ; else b = c ; end if abs(c-r) > error continue else break end end
Best regards Razi

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by