Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab question and I need codes Write a user-defined function with function cal

ID: 3184306 • Letter: M

Question

Matlab question and I need codes

Write a user-defined function with function call [r, k] = rootfinder (f, x0, kmax, tol) where f is an anonymous function, x0 is a specified value, kmax is the maximum number of iterations, and tol is a specified tolerance. The function sets x -X, calculates fx), and if it is less than the tolerance, then x, approximates the root r. If not, it will increment x, by 0.01 to obtain x, repeat the procedure, and so on. The process terminates as soon as If%)k tol for some k. The outputs of the function are the approximate root and the number of iteration:s it took to find it. Execute the function for f(x) = x2-33x + 2.1, x,-0.5, kmax = 50 tol = 10-2. Repeat Problem forf(r) = 3 + In(2-1)-ex, xo = 1, kmax = 25 tol = 10-2 -

Explanation / Answer

This code would give you the desired value....

function [r,k] = root_finder(f,x0,kmax,tol)
for i=1:kmax
    if(abs(f(x0))>tol)
        x0=x0+0.01;
        continue
    end
    r=x0;
    k=i;
    break
end

for f(x)=x2+3.3x+2.1,x0=0.5,kmax=50,tol=10-2

code for the function is:

function v = f(x)
v=(x.^2)-(3.3.*x)+2.1;
end

Output

0.8600, 37

for function f(x)=3+ln(2x-1)-ex,x0=1,kmax=25,tol=10-2

code for the function:

function v = f(x)
v=3+log((2*x)-1)-exp(x);
end

Output

1.2100, 22