roots finding using muller method
33 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
only one root is showing suppose f(x)=x^2+1 only -i root is showing not +i
here this is program of muller method
function shik3
% t2=1e-6;
p0 = 10;
p1 = 20;
p2 = 30;
TOL = 10^-9;
N0 = 100;
format long;
h1 = p1 - p0;
h2 = p2 - p1;
DELTA1 = (f(p1) - f(p0))/h1;
DELTA2 = (f(p2) - f(p1))/h2;
d = (DELTA2 - DELTA1)/(h2 + h1);
i=3;
while i <= N0
b = DELTA2 + h2*d;
D = (b.^2 - 4*f(p2).*d).^(1/2);
if abs(b-D) < abs(b+D)
E = b + D;
else
E = b - D;
end
h = -2*f(p2)/E;
p = p2 + h;
if abs(h) < TOL
p
disp(p)
break
end
p0 = p1;
p1 = p2;
p2 = p;
h1 = p1 - p0;
h2 = p2 - p1;
DELTA1 = (f(p1) - f(p0))/h1;
DELTA2 = (f(p2) - f(p1))/h2;
d = (DELTA2 - DELTA1)/(h2 + h1);
i = i + 1;
disp(p)
%disp([imag(p) t2])
%plot(t2,real(p))
end
if i > N0
formatSpec = string('The method failed after N0 iterations,N0= %d \n');
fprintf(formatSpec,N0);
end
function y=f(x)
y=x^2+1 ;
end
end
2 comentarios
Jan
el 25 de Jul. de 2021
Please edit your question. Do you see the pile of blank lines? Deleteing them and using the code block improves the readability. Thanks.
Respuestas (2)
Chunru
el 25 de Jul. de 2021
The Muller's method is supposed for finding the real solution. Your code is attempting to find the complex solution. I am wondering if it has any other side-effect.
The Muller's method is based on secant method (or in a similar way) to iteratively find the solution. It cannot guarantee to find all solutions of a functions. It just find a solution (normally real) within the specified interval.
For you code, you may find another solution by change the sign of the following line
% D = (b.^2 - 4*f(p2).*d).^(1/2);
D = -(b.^2 - 4*f(p2).*d).^(1/2);
Walter Roberson
el 1 de Ag. de 2021
Your program only ever tries to find one root. You have a while loop, but that while loop is only dealing with one root.
You have several options:
- As you are solving a polynomial with real-valued coefficients, all imaginary roots must occur as complex conjugates. So after you find one root, take its complex conjugate and test it to see whether it works as well
- rewrite the code to try several different initial guesses, and keep trying guesses until you reach a sanity limit or you have found as many unique roots as you think you should have
- rewrite your code to be working on a vector of guesses, and keep iterating until you reach a sanity limit or enough unique roots have reach tolerance. Note that if you fail to try enough different elements originally then you are not certain that enough of them will lead to unique roots.
- use a completely different algorithm, such a symbolic method
3 comentarios
Walter Roberson
el 2 de Ag. de 2021
This looks like a homework problem, so you should be writing the code.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!