17 views (last 30 days)

Write the function find_zero that is defined like this function

x = find_zero(f,x1,x2)

The first input argument is special. It is a “function handle”. A function handle is gotten by typing @ and the name of any function. For example,

x = find_zero(@sin,-1,1)

will give f the function handle for MATLAB’s built-in sin function. Then, inside find_zero, the statement

y = f(-1)

would set

y = sin(-1)

Note that the @ sign is not used inside the function. Only the caller uses it. All other arguments to find_zero are scalar numbers, and x1 is less than x2. The goal of the function is to find an x that lies in the range from x1 to x2 such that after the command, y = f(x), is executed inside the function find_zero, y is approximately zero as defined by abs(y) < 1e-10. All you know about the function f is that it has one scalar input and one scalar output, and a plot of its values crosses the x-axis exactly once between x1 and x2, as, for example, in the figure 3. It is the responsibility of the caller to call the function with arguments that obey these rules.

Figure 3 Output plot for x1 and x2 (Hint: Remember to label your axis’s) Here are two sample runs:

>> find_zero(@sin,-2.5,2.3) % as shown in the figure

ans =

-6.4000e-11//

>> format long

//>> find_zero(@cos,-2,1.3)

ans =

-1.570796326871000

in the function:

>> function x = find_zero(f,x1,x2) % Function definition

>> y = f(x2); % Computing the function value at x = x2

>> while abs(y) > 1e-10 % exit loop if y approximatly equal to zero

>> x=x2-y*(x2-x1)/(y-f(x1)); % finding new x using secant method

>> x1=x2;%copy the value of x2 to x1

>> x2=x;% copy the value of x to x2

>> y = f(x2); % Computing the function value at new x

>> end % End of loop

>> end % End of program

Harsh
on 11 May 2017

If you still need help with this, please contact MathWorks Technical Support here: https://www.mathworks.com/support/contact_us/

Please be sure to provide a detailed description of the exact issue and attach any relevant files / code / examples required to investigate the issue.

Walter Roberson
on 11 May 2017

Steven Lord
on 15 May 2017

MATLAB does not allow you to define functions like that at the prompt in the Command Window. [This work is too complicated to write as an anonymous function, at least not without some rather creating and advanced techniques.]

If you want to define a function (like your assignment requires you to do) you must define it in a function file like Walter said.

Sign in to comment.

Ganesh Prasad B K
on 29 Jun 2017

Here is the code. Have a look at it

function x = find_zero(f,x1,x2)

tolerance = 1e-10;

if(f(x1)==0)

x=x1;

return

elseif(f(x2)==0)

x=x2;

return

else

while tolerance < abs((x2-x1)/2)

x3= mean([x1,x2]);

if(f(x3)==0)

x=x3;

return

elseif(f(x1)*f(x3)>0)

x1=x3;

elseif(f(x1)*f(x3)<0)

x2=x3;

end

x = x3;

end

end

end

Walter Roberson
on 29 Jun 2017

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/339348-whats-wrong-in-the-code-may-someone-help-me-to-get-same-answer-as-the-example#comment_452177

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/339348-whats-wrong-in-the-code-may-someone-help-me-to-get-same-answer-as-the-example#comment_452177

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/339348-whats-wrong-in-the-code-may-someone-help-me-to-get-same-answer-as-the-example#comment_452181

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/339348-whats-wrong-in-the-code-may-someone-help-me-to-get-same-answer-as-the-example#comment_452181

Sign in to comment.