roots finding using muller method

17 次查看(过去 30 天)
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 个评论
Jan
Jan 2021-7-25
Please edit your question. Do you see the pile of blank lines? Deleteing them and using the code block improves the readability. Thanks.
shiv gaur
shiv gaur 2021-7-25
runing program only one root is showing

请先登录,再进行评论。

回答(2 个)

Chunru
Chunru 2021-7-25
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
Walter Roberson 2021-8-1
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:
  1. 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
  2. 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
  3. 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.
  4. use a completely different algorithm, such a symbolic method
  3 个评论
Walter Roberson
Walter Roberson 2021-8-2
This looks like a homework problem, so you should be writing the code.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by