Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
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 Kommentare
Geoff Hayes
Geoff Hayes am 28 Sep. 2016
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 am 8 Nov. 2016
I worked it out. a b n should be taken out. f is any function defined in a separate script.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by