Solution for transcendental equation
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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')
0 Kommentare
Antworten (1)
Walter Roberson
am 17 Dez. 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 Kommentare
Walter Roberson
am 18 Dez. 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
am 18 Dez. 2022
Bearbeitet: Walter Roberson
am 18 Dez. 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.
See for example https://www.mathworks.com/matlabcentral/fileexchange/68994-find-all-root-of-nonlinear-function
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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!