This is code for Modified Regula Falsi method for finding roots. Something in here is preventing the if statements from executing properly.

조회 수: 7 (최근 30일)
function ModRegFal = ModRegFal(a, b, n)
format long;
a = input('Enter a value for lower boundary a: ');
b = input('Enter a value for upper boundary b: ');
n = input('How small should should the error be (to what -power)? ');
if (f(a)*f(b) > 0 )
disp ('Invalid values of a and b. Program Closing')
return;
end;
F = f(a);
G = f(b);
w0 = a;
while (1)
wn = (G*a-F*b)/(G-F);
disp([a b wn w0]) %%just checking where the values are, and it they look correct
if f(a)*f(wn) > 0
disp('ranif 1')%%just checking where the values are, and it they look correct
b = wn;
G = f(wn);
if f(w0)*f(wn) > 0
F = F/ 2; end;
disp('ranif 2')%%just checking where the values are, and it they look correct
disp([a b wn w0])%%just checking where the values are, and it they look correct
else
a = wn;
F = f(wn);
if f(w0)*f(wn) > 0
disp('ranif 3')%%just checking where the values are, and it they look correct
disp([a b wn w0])%%just checking where the values are, and it they look correct
G = G/ 2; end;
end
disp([a b wn w0])
if (abs((wn - w0)/wn) < 0.5*10^-n)
disp ('The root is: ')
disp (wn)
break;
else
w0 = wn;
end
end
I wrote a separate scrip with the function y = f(x) y = x^2 - 2;
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2016년 9월 28일
Jay - why do you pass in the input parameters a, b and n only to overwrite them with user input? How or where is f defined because the above code would fail with an error for this undefined function?
Please provide a working sample of code and include your steps on calling this function.
Jay Gersten
Jay Gersten 2016년 11월 8일
I worked it out. a b n should be taken out. f is any function defined in a separate script.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by