필터 지우기
필터 지우기

Numerical Methods to solve Equation

조회 수: 4 (최근 30일)
Razi Naji
Razi Naji 2017년 5월 2일
댓글: Razi Naji 2017년 5월 2일
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