Effacer les filtres
Effacer les filtres

how to write a bisection loop?

3 vues (au cours des 30 derniers jours)
wenchong chen
wenchong chen le 28 Fév 2021
Modifié(e) : Matt J le 1 Mar 2021
y = (-0.50598*10^-10)*x.^3 + (0.38292*10^-7)*x.^2 + (0.74363*10^-4)*x + 0.88318*10^-2
and I am trying to use bisection to find three point for this function, here is what I did.
For bisection one: xl = -1000 and xu = -500, xm = (-1000+(-500))/2 = -750
If xl*xu < 0
Xu=xm
Xm=(xl+xu)/2
error = ((xm-xu)/xm)*100
end
If xl*xu >0
xl = xm
Xm=(xl+xu)/2
error = ((xm-xu)/xm)*100
end
If error < 5
end
this is what I plan to do in word I dont know how to make a funtion to start it.
  7 commentaires
wenchong chen
wenchong chen le 28 Fév 2021
while error >5
if xl*xu<0
xu = xm
xm=(xl+xu)/2
error = ((xm-xu)/xm)*100
end
if xl*xu >0
xl = xm
Xm=(xl+xu)/2
error = ((xm-xu)/xm)*100
end
end
here is my new code it is runing but not working as I think
Jan
Jan le 1 Mar 2021
Please use the tools for formatting code. This improves the readability.
It is still not clear, what you want to achieve. "bisection between -1000 to -500, -500 to 0 and 1500 – 2000" does not explain this, because you can divide anything into anything. I guess, you want to find a minimum or zero value?
Your code contains an "Xm" adn "xm" with different uppercase X.
If you check for "if xl*xu<0" you need an "else" to handle the opposite case. Changing the value of xu and checking again "if xl*xu>0" is not suffcient.
The case that "xl*xu==0" is not considered.
Most likely I assume you want to create a function, which is evaluated at the points xl and xu. Comparing the x values is not meaningful. You can find many bisection codes in Matlab, if you ask an internet search engine.

Connectez-vous pour commenter.

Réponses (1)

Matt J
Matt J le 1 Mar 2021
Modifié(e) : Matt J le 1 Mar 2021
Because y is a polynomial, it is much more expedient to use roots(),
y = [(-0.50598*10^-10) , (0.38292*10^-7) , (0.74363*10^-4), 0.88318*10^-2];
r=roots(y)
r = 3×1
1.6884 -0.8029 -0.1288
fplot(@(x)polyval(y,x)); hold on
plot(r,0*r,'*'); hold off
xlim([min(r), max(r)])

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by