3

I have my function working if replace my input with a real function. However, when I change it like below, it asks me to input a function about like ten times and then outputs the answer.

I'm guessing this has to do with accepting functions as anonymous from a user? I'm not too sure what is going on. Thank you. This is my code:

f = @(x) input('Input a function');
A = [0 2];
z = myBisection(f,A);

With x.^2+3.*x-4 as my f function and the answer being 1.001

1 Answer 1

1

This is because you're not applying the right methodology for accepting inputs from input. What you are actually doing with f is that you're creating an anonymous function where if you run this, it will then ask you for input from the user. If you actually typed in this statement with your f variable as is, nothing would happen. Something would only happen if you typed in f() (or technically, f can be any input, but because the input variable for the anonymous function isn't being used in how you specified it, we can get away with specifying no inputs) in the command prompt to call this function, and then pushed ENTER. Even when you do this, the output of this function would only store a string, and you need to use this to create an anonymous function.

To fix this, what I would do is use input to store your function as a string. Take note that you need to add a second parameter for input, which is a string flag 's'. Without this, input would be expecting a number, and this obviously wouldn't work. After this, use str2func to turn this into an anonymous function, and then run your bisection method. str2func accepts in a string, and then converts that string into an actual MATLAB function and you can now use this for your purposes. By accepting your anonymous function as a string, you can place any operators you want to your heart's content, so long as the way you place them are syntactically valid. Also, make sure that your function is defined in terms of x like your example in your post.

As such do this:

funcString = input('Input a function: ', 's'); %// Make sure you specify 's'!
f = str2func(['@(x) ' funcString]);
A = [0 2];
z = myBisection(f, A);

Put this inside a .m file, then run this file. It should behave like you are expecting.

Sign up to request clarification or add additional context in comments.

9 Comments

OHHHH I see. So the input command cannot directly accept functions as anonymous functions.
@user3509716 - Correct. It can only accept arithmetic expressions, which then get turned into numbers, or you have to accept the input as a string, and then transform it into an anonymous function.
@raryeng In regards to your fix, I tried it and it returned "Undefined function or variable 'x'" Also, it still keeps asking me to input several times do you know how to fix this?
@user3509716 - Did you see my edit? I forgot to include the 's' as an additional parameter. Please see my edits. Also, copy and paste this directly into your MATLAB command prompt. This should work.
@user3509716 - Not a problem. I only noticed my error now!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.