How to display a graph after getting user to input in the pop up?

27 views (last 30 days)
Question:
A window should pop up that asks the user to input a single variable equation (a.k.a. function) for which the user wishes to find the root.
ii) A graph of this equation should get displayed
My code:
prompt = {'Single variable equation: \color{white} .'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'x'};
options.Interpreter='tex';
answer = inputdlg(prompt,dlg_title,num_lines,defaultans,options);
func = string(answer{1});
What to do next? New to MATLAB, please help.

Accepted Answer

Walter Roberson
Walter Roberson on 9 Sep 2017
  6 Comments
Walter Roberson
Walter Roberson on 12 Sep 2017
The meaning of
@(x) func
is the same as
@(x) func()
which means that a local variable named "x" is to be accepted on input, and then func() is to be called with no parameters.
The syntax
@(x) func(x)
would mean that a local variable named "x" is to be accepted on input, and then func() is to be called passing that variable as a parameter. @(x) func(x) means almost exactly the same thing as @func alone without the (x) part, except that using @(x) func(x) will only work if exactly one input is provided, whereas @func would work if multiple inputs are provided if func accepts multiple inputs.
In
f = str2func((['@(',char(symvar(func)),')', func]));
then notice the symvar(func) and at that point func is a character vector. symvar(func) looks through the string to figure out which variable the user used -- so for example if the user had entered 'sin(t).^2' then symvar would return the 't' . When symvar is used properly, the user is not restricted to using a particular variable name.
The above code constructs @(VARIABLENAME) CODE . In the 'sin(t).^2' example, @(t) sin(t).^2 would be constructed; if the user had coded 'besselj(1/2, x)' then @(x) besselj(1/2,x) would be constructed.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!