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

MATLAB CODE function mynewrep(f,f1,x0,n) x = x0; for i = 1:n x = x - f(x)/f1(x);

ID: 3601421 • Letter: M

Question

MATLAB CODE

function mynewrep(f,f1,x0,n)

x = x0;

for i = 1:n

    x = x - f(x)/f1(x);

     disp([ i    x     f(x)])

     if f(x)<0.01

         break

     end

end

end

Modify the code to find the root only at f(x)<0.01 using Newton-Rephson Method without showing any iteration. Also find put the root of equation, f(x)=x5-3x-10, take initial guess, x0=2

Please write full code (Full Answer )

function mynewrep(f,f1,x0,n)

x = x0;

for i = 1:n

    x = x - f(x)/f1(x);

     disp([ i    x     f(x)])

     if f(x)<0.01

         break

     end

end

end

Explanation / Answer

Function Programs Begin by clicking on the new document icon in the top left of the Matlab window (it looks like an empty sheet of paper). In the document window type the following: function y = myfunc ( x ) y = 2* x .^2 - 3* x + 1; end Save this file as: myfunc.m in your working directory. This file can now be used in the command window just like any predefined Matlab function; in the command window enter: x = -2:.1:2; % Produces a vector of x values y = myfunc ( x ); % Produces a vector of y values plot (x , y ) Note that the fact we used x and y in both the function program and in the command window was just a coincidence. In fact, it is the name of the file myfunc.m that actually mattered, not what anything in it was called. We could just as well have made the function function nonsense = yourfunc ( inputvector ) nonsense = 2* inputvector .^2 - 3* inputvector + 1; end Look back at the program. All function programs are like this one, the essential elements are: • Begin with the word function. • There is an input and an output. • The output, name of the function and the input must appear in the first line. • The body of the program must assign a value to the output variable(s). 6 7 • The program cannot access variables in the current workspace unless they are input. • Internal variables inside a function do not appear in the current workspace. Functions can have multiple inputs, which are separated by commas. For example: function y = myfunc2d (x , p ) y = 2* x .^ p - 3* x + 1; end Functions can have multiple outputs, which are collected into a vector. Open a new document and type: function [ x2 x3 x4 ] = mypowers ( x ) x2 = x .^2; x3 = x .^3; x4 = x .^4; end Save this file as mypowers.m. In the command window, we can use the results of the program to make graphs: x = -1:.1:1 [ x2 x3 x4 ] = mypowers ( x ); plot (x ,x , ’ black ’ ,x , x2 , ’ blue ’ ,x , x3 , ’ green ’ ,x , x4 , ’ red