フィルターのクリア

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.
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 Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by