Effacer les filtres
Effacer les filtres

Numerical Methods to solve Equation

4 vues (au cours des 30 derniers jours)
Razi Naji
Razi Naji le 2 Mai 2017
Commenté : Razi Naji le 2 Mai 2017
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

Réponse acceptée

David Goodmanson
David Goodmanson le 2 Mai 2017
Modifié(e) : David Goodmanson le 2 Mai 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 commentaires
Razi Naji
Razi Naji le 2 Mai 2017
Ok David , thanks , i am trying now to solve it :-) , i will inform you if solved :)
Razi Naji
Razi Naji le 2 Mai 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by