I want to calculate a zero of a function, can anyone help me ?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
as an example if we take x^3-8*x^2+17*x-10,i want to calculate this zero polynomial using dichotomy method but it does'nt give me the good result , can anyone help to solve this problem ?
clc
clear all
close all
syms x
f=input( 'donnez la fonction :' );
a=input( 'donnez la borne minimal de l intervalle :' );
c=input( 'donnez la borne maximal de l intervalle : ' );
b=(a+c)/2;
v=[];
n=[];
p=0;
for i=1:100
x(i)=(a(i)+b(i))/2;
if abs(f(x(i)))==0
v=[v,x(i)];
break
end
if (f(a(i))*f(x(i))<0)
a(i+1)=a(i);
b(i+1)=x(i);
else
b(i+1)=b(i);
a(i+1)=x(i);
end
end
for j=1:100
x(j)=(b(j)+c(j))/2;
if abs(f(x(j)))==0
n=[n,x(j)];
break
end
if (f(b(j))*f(x(j))<0)
b(j+1)=b(j);
c(j+1)=x(j);
else
c(j+1)=c(j);
b(j+1)=x(j);
end
end
p=[v,n]
3 Commenti
Torsten
il 26 Gen 2018
Modificato: Torsten
il 26 Gen 2018
1. Syms x is incorrect - you don't work with symbolic variables.
2. a(i) and b(i) in the first for-loop are not defined. You only have a and b which are scalars. I don't understand why you work with arrays for a, b and x anyway.
3. As Walter already mentionned, the if (abs(...)) part is questionable.
...
You should study the code I linked to - it gives you an impression how to program the bisection method in a few lines and might help you how to modify your code accordingly.
Best wishes
Torsten.
Risposta accettata
Torsten
il 26 Gen 2018
https://de.mathworks.com/matlabcentral/fileexchange/33748-bisection-method?focused=5203297&tab=function
Best wishes
Torsten.
Più risposte (1)
Walter Roberson
il 26 Gen 2018
Your line
if abs(f(x(j)))==0
is questionable. abs() of an expression can only equal 0 if the value itself is 0, in which case there would be no point in taking abs():
if f(x(j)) == 0
However, with polynomials, numeric round-off error is a common problem (which gets worse with higher degree), so it might not be possible to find an f(x(j)) that is exactly 0. Instead you might get to the point of finding two numbers that are adjacent in floating point representation such that f() of one of them is on one side of 0 and f() of the other of them is on the other side of 0 -- for example 1.834e-34 and -9.5192e-35 . Therefore instead of testing for exactly 0, you should test that the absolute value is less than a tolerance.
Vedere anche
Categorie
Scopri di più su Assumptions in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!