Clear Filters
Clear Filters

Numerical Methods to solve Equation

4 views (last 30 days)
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

Accepted Answer

David Goodmanson
David Goodmanson on 2 May 2017
Edited: David Goodmanson on 2 May 2017
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 Comments
Razi Naji
Razi Naji on 2 May 2017
Ok David , thanks , i am trying now to solve it :-) , i will inform you if solved :)
Razi Naji
Razi Naji on 2 May 2017
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

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!