Matlab taking a very long time to give output when my error limit is low
Afficher commentaires plus anciens
I am finding roots by the false position method. User inputs are upper and lower boundary (yu,yl) and error limit (err)
It works, except when you give it too large of numbers to work with. Whenever I put in the error as 1 and yu,yl as say (-5,7) it will work. But when I try that with a error limit as (.00001) or so, then matlab gets stuck on run and won't ever give the output.
clc
clear all
close all
%, press run, and then enter values you'd like
f=@(x)(x.^3+7*x.^2-33*x-135);
yl = input('type approximated lower boundary:');
yu = input('type approximated upper boundary:');
err=input('Type desired approximate error limit:');
if (yl*yu) > 0
disp(' the appriximation boundaries cannot have the same sign, final warning.');
%if values do not produce a negative number give better boundaries
end
while (abs(yu-yl)>err) %basically how close do you want the boundaries to go
ynew = yu -(f(yu)*(yl - yu))/(f(yl) - f(yu));
if (f(yl)*f(ynew) < 0)
yu=ynew;
else
yl=ynew;
end
end
fprintf('The root of this equation given your error limit is=%f', ynew);
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!