Clear Filters
Clear Filters

Solution for transcendental equation

9 views (last 30 days)
Hariesh Krishnan B
Hariesh Krishnan B on 17 Dec 2022
Edited: Walter Roberson on 18 Dec 2022
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')

Answers (1)

Walter Roberson
Walter Roberson on 17 Dec 2022
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 Comments
Walter Roberson
Walter Roberson on 18 Dec 2022
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 on 18 Dec 2022
Edited: Walter Roberson on 18 Dec 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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!