Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Specifying a symbolic function in an input dialog box

Subject: Specifying a symbolic function in an input dialog box

From: Rebekah

Date: 3 Aug, 2012 05:56:14

Message: 1 of 3

Hi, all. I would very much appreciate some advice.

I would like to be able to specify a symbolic function in a dialog box:

syms xi;
prompt = {'Enter function:'};
dlg_title = 'Function input';
num_lines = 1;
def = {'sin(xi)'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
f(xi)=answer{1};
Error using sym/subsindex (line 1555)
Indexing input must be numeric, logical or ':'.

And when I remove the variable it gives me f=sin(xi) just fine but apparently just as a 'char' class object:

syms xi;
prompt = {'Enter function:'};
dlg_title = 'Function input';
num_lines = 1;
def = {'sin(xi)'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
f=answer{1};

>> f
f = sin(xi)

>> f(pi/2)
Subscript indices must either be real positive integers or logicals.
 
>> class(f)
ans = char


Is there a way to type a function into a dialog box and have it function in the script properly?
>>symfun(f) was unsuccessful at converting f from 'char' to 'symfun'.

Thanks in advance.

Rebecca

Subject: Specifying a symbolic function in an input dialog box

From: Christopher Creutzig

Date: 3 Aug, 2012 07:29:45

Message: 2 of 3

On 03.08.12 07:56, Rebekah wrote:

> And when I remove the variable it gives me f=sin(xi) just fine but apparently just as a 'char' class object:

> Is there a way to type a function into a dialog box and have it function in the script properly?
>>>symfun(f) was unsuccessful at converting f from 'char' to 'symfun'.

That is because symfun expects to be given a sym, not a string. Try this
instead:

f = sym(f);
f = symfun(f, symvar(f, 1));



HTH,
Christopher

Subject: Specifying a symbolic function in an input dialog box

From: Rebekah

Date: 3 Aug, 2012 21:16:28

Message: 3 of 3

Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <501B7DE9.6080409@mathworks.com>...
> On 03.08.12 07:56, Rebekah wrote:
>
> > And when I remove the variable it gives me f=sin(xi) just fine but apparently just as a 'char' class object:
>
> > Is there a way to type a function into a dialog box and have it function in the script properly?
> >>>symfun(f) was unsuccessful at converting f from 'char' to 'symfun'.
>
> That is because symfun expects to be given a sym, not a string. Try this
> instead:
>
> f = sym(f);
> f = symfun(f, symvar(f, 1));
>
>
>
> HTH,
> Christopher

I see! And it says it right in the 'symfun' description, of course:

F = symfun(FORMULA,INPUTS) returns a symbolic function
    with inputs INPUTS and body FORMULA. INPUTS must be a
    sym array of symbolic variables and FORMULA is a
    sym object expression.

I'd thought if xi were symbolic it would make f symbolic. My understanding is very bad. Thank you, that worked wonderfully and helped enlighten me.

R

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us