Computing the absolute error

6 Ansichten (letzte 30 Tage)
Cecilio Flores Roque
Cecilio Flores Roque am 8 Okt. 2020
Bearbeitet: Ameer Hamza am 8 Okt. 2020
How do I compute the absolute error n = |xn − xr| for n = 0, 1, . . . , 50, where we take the output xr from MATLAB’s fzero function with initial guess xinit = 1 to be the “true” root, given this.
xn = bisectionMethod(f, a, b, numiter)
f1= @(x) cos(x)-x;
f2= @(x) exp(-x^2)-x;
f3= @(x) (x^3)-(1/2);
x1= bisectionMethod(f1, 0, 1, 50);
x2= bisectionMethod(f2, 0, 1, 50);
x3= bisectionMethod(f3, 0, 1, 50);

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 8 Okt. 2020
Bearbeitet: Ameer Hamza am 8 Okt. 2020
Consider one function
f1 = @(x) cos(x)-x;
x1r = fzero(f1, 0);
You can do it like this
n = 0:50;
x1n = zeros(size(n)); % all solutions for f1
for i = 1:numel(x1n)
x1n(i) = bisectionMethod(f1, 0, 1, n(i));
end
err = abs(x1n-z1r);
A more efficient approach is to modify bisectionMethod() function such that it returns a complete vector in a single call. The above code is repeating the same calculations several times.

Weitere Antworten (0)

Kategorien

Mehr zu Generating and Calling Reentrant Code finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by