Azzera filtri
Azzera filtri

Numerical Methods to solve Equation

4 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

David Goodmanson
David Goodmanson il 2 Mag 2017
Modificato: David Goodmanson il 2 Mag 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 Commenti
Razi Naji
Razi Naji il 2 Mag 2017
Ok David , thanks , i am trying now to solve it :-) , i will inform you if solved :)
Razi Naji
Razi Naji il 2 Mag 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Large Files and Big Data in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by