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

6 次查看(过去 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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by