Azzera filtri
Azzera filtri

bisection method, iteration does not stop although hand calculation were diffrent

1 visualizzazione (ultimi 30 giorni)
while running this scrept it keeps loopin on the same iteration for some reason
while doing the bisection by hand error reached below zero after 5 iterations
end
% Setting x as symbolic variable
syms q w e r t y u i o p l k j h g f d s a z x c v b n m ;
% Input Section
y = input('Enter the equation equations: ');
a = input('Enter A value of the interval: ');
b = input('Enter B value of the interval: ');
e = input('Entre the minimum tolerable error: ');
% Finding Functional Value
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = (a+b)/2;
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
else
a =c;
end
c = (a+b)/2;
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end
my inputs are
>> bisection2
Enter the equation equations: (sqrt(19.62.*x))*tanh((sqrt(x*19.62)/8)*2.5)
Enter A value of the interval: 0
Enter B value of the interval: 2
Entre the minimum tolerable error: 0.00005
the thing afterwards goes on forever unless i stop the script
this the function i am trying to the find the root of
  3 Commenti
ali altaif
ali altaif il 2 Dic 2022
thanks for the answer but the values are way off from what i have calculated by hand do you have an idea on why is that the case. figure attached is the desired values
Torsten
Torsten il 2 Dic 2022
Modificato: Torsten il 2 Dic 2022
Then you must have used a different function in your hand calculation.
f(0), e.g. is obviously 0 for the function given, but in your list, f(0) = -5.
Maybe your function is
y = @(x)(sqrt(19.62.*x)).*tanh((sqrt(x*19.62)/8)*2.5) - 5;
?

Accedi per commentare.

Risposte (1)

Steven Lord
Steven Lord il 2 Dic 2022
Let's look at your function.
syms x
f = sqrt(19.62*x);
fun = f*tanh(2.5*f/8);
fplot(fun)
yline(0)
It looks like you have a zero crossing at x = 0 but you never check for that in your code. You only check that the value of the function at the midpoint of your interval is 0. Let's check numerically what we observed graphically.
value = subs(fun, x, 0)
value = 
0
By the way, stop using eval on symbolic expressions. If you need to convert the result of the subs call into a double precision value, call double on it.
d = double(value)
d = 0

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by