Solution for transcendental equation

For the equation (x*tan(x))=0.01, the roots are to be found for which the code i have attached. The roots i have obtained are seemed to be shpwn as NaN. Please rectify my code
Code:
f = @(x) ((x*tan(x))-0.01);
fp = @(x) tan(x)+(x*sec(x)*sec(x));
x0 = 0;
for jj = 1 : 1000 %number of iterations to find some roots
x0 = x0 + (jj-1)*(jj/10^4); %take previous guess and increment
for newton = 1 : 20 %number of newton steps
x0 = x0 - f(x0)/fp(x0);
end
ROOTS(jj,1) = x0;
ERROR(jj,1) = abs(f(x0)); %check that we really found a root
end
ROOTS = unique(ROOTS); %roots may be duplicate
ERROR = unique(ERROR); %
length(ROOTS) %number of roots we actually found
ROOTS(1:10)
ERROR(1:10)
plot(abs(ERROR))
xlabel('root number')
ylabel('absolute error')

Respuestas (1)

Walter Roberson
Walter Roberson el 17 de Dic. de 2022

0 votos

x0 starts as 0. When jj=1, the first increment step adds (1-1)*(1/1e4) which is 0, so x0 stays as 0. fp(0) is 0. f(0) is -0.01. Divide by the fp(0) to get -inf.
Next stage, the trig operations on -inf give nan

5 comentarios

Hariesh Krishnan B
Hariesh Krishnan B el 17 de Dic. de 2022
Editada: Hariesh Krishnan B el 17 de Dic. de 2022
This was the source code i used for which it works.when i change the function it is not coming.any modification in code required?
f = @(x) ((2*x)./(x.^2-1)) - tan(x);
fp = @(x)-tan(x).^2+2.0./(x.^2-1.0)-x.^2.*1.0./(x.^2-1.0).^2.*4.0-1.0;
x0 = 0;
for jj = 1 : 1200 %number of iterations to find some roots
x0 = x0 + (jj-1)*(jj/10^4); %take previous guess and increment
for newton = 1 : 20 %number of newton steps
x0 = x0 - f(x0)/fp(x0);
end
ROOTS(jj,1) = x0;
ERROR(jj,1) = abs(f(x0)); %check that we really found a root
end
ROOTS = unique(ROOTS); %roots may be duplicate
ERROR = unique(ERROR); %
length(ROOTS) %number of roots we actually found
ROOTS(1:10)
ERROR(1:10)
plot(abs(ERROR))
xlabel('root number')
ylabel('absolute error')
Walter Roberson
Walter Roberson el 17 de Dic. de 2022
The output looks okay to me. If you are referring to the fact that your error is not all exactly zero, then that is to be expected due to floating point error.
Hariesh Krishnan B
Hariesh Krishnan B el 18 de Dic. de 2022
Editada: Hariesh Krishnan B el 18 de Dic. de 2022
@Walter RobersonDoes fp mean differential of function f?
Is ther another method to solve using fzero function
Providing more context to question through attached images.
ROOTS = unique(ROOTS); %roots may be duplicate
ERROR = unique(ERROR); %
That code is not really correct. You should be doing something more like
[ROOTS, ~, G] = uniquetol(ROOTS);
ERROR_MINS = splitapply(@min, ERROR, G);
ERROR_MAXS = splitapply(@max, ERROR, G);
so that you find the error relative to each root. (You could go further, and for each clumping, find the entry with the lowest error.)
Walter Roberson
Walter Roberson el 18 de Dic. de 2022
Editada: Walter Roberson el 18 de Dic. de 2022
The code appears to be a version of Newton's method, so fp appears to be the derivative of f.
The code does a fixed number of iterations from each of 1200 starting points.
You can use fzero() for each different starting point. But there are other possibilities as well.
If you have reason to believe that the minimimum distance between roots is at least some particular value, then you can create initial coordinates that far apart, and evaluate the function at those locations, and look for changes in sign, and then zero in on the exact locations using the two sides as end-points. If the roots can potentially be very close together, that stops being economical.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 17 de Dic. de 2022

Editada:

el 18 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by