Creating Symbolic Functions
Mostra commenti meno recenti
Hey all. I'm new to Matlab and a first time poster here. I'm trying to create a multivariate symbolic polynomial and access its variables using the argnames function, which I found here:
Running the example code at the linked page, I get the following:
>> syms f(x, y);
??? Error using ==> syms at 61
Not a valid variable name.
Then I looked at the documentation for sym here:
I tried running the example code for a symbolic function found there, which produced:
>> x = sym('x');
>> y = sym('y');
>> f(x, y) = x + y;
??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double
array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in ==> sym.sym>sym.double at 936
Xstr = mupadmex('symobj::double', S.s, 0);
Error in ==> sym.sym>privformatscalar at 2678
x = double(x);
Error in ==> sym.sym>privformat at 2663
s = privformatscalar(x);
Error in ==> sym.sym>sym.subsasgn at 1433
[inds{k},refs{k}] = privformat(inds{k});
Then I tried the following, just to see if it would work (it didn't):
>> x = sym('x');
>> y = sym('y');
>> f = x+y;
>> argnames(f);
??? Undefined function or method 'argnames' for input arguments of type 'sym'.
What am I doing wrong?
6 Commenti
G A
il 15 Mar 2012
Itamar, which version of Matlab are you using?
Walter Roberson
il 15 Mar 2012
What is shown would apply to all MATLAB versions that use the MuPAD symbolic engine. (Not that the Maple symbolic engine would work for those commands either, but the error messages for the Maple engine would not mention MuPAD.)
G A
il 15 Mar 2012
Why then is it not written explicitly in the Matlab documentation that this and that do not work in Matlab with Mapple symbolic engine?
Walter Roberson
il 15 Mar 2012
At the time of the switch over to MuPAD, the release notes had comparisons between the two. After that, everything to do with Maple was legacy except for the symengine command, and that _was_ specifically documented in the release notes when MATLAB changed to disallow changing symengine a few releases later.
MathWorks is not in the habit of specifically documenting that new features will not work with releases that predate the introduction of the feature.
None of the commands that Itamar tried were supported in the MATLAB interface to the Maple symbolic engine, and none of them are supported in the MATLAB interface to the MuPAD symbolic engine. The error messages produced in both cases would have had the same essential meaning, but you can be sure that the Maple error messages would not have included the word "MuPAD" such as in the phrasing "Error in MuPAD command". It might have said "Error in Maple command" for example.
G A
il 15 Mar 2012
Thanks, Walter.
Walter Roberson
il 16 Mar 2012
Dang, they introduced new functionality that used to be illegal.
Risposta accettata
Più risposte (2)
Walter Roberson
il 15 Mar 2012
f = evalin(symengine, '(x,y) -> x+y;');
For something more complicated,
f = evalin(symengine, 'proc(x) local S, N; S := 0; for N to x do S := S + cos(Pi/N) end_do; S end_proc;');
Only parts of MuPAD have direct MATLAB interfaces, and creating new functions (procedures) does not have a direct interface, unfortunately. Though it could be that I have missed a trick.
1 Commento
Itamar
il 16 Mar 2012
G A
il 15 Mar 2012
Could it be an alternative solution to use symvar() instead of argnames()?
syms x y
f = x+y;
symvar(f)
ans =
[ x, y]
6 Commenti
Walter Roberson
il 15 Mar 2012
Plausibly.
On the other hand, symvar() will not return the variable names in any particular order, so if Itamar was trying to distinguish between f(x,y) = atan2(x,y) versus f(y,x) = atan2(x,y) (looks the same but the order of the parameters is important) then there could be problems.
But symvar() might be fine for what Itamar is doing.
G A
il 15 Mar 2012
What about this way:
f = fittype( @(t, x) x+t );
argnames(f)
it does not work with y instead of t
Walter Roberson
il 16 Mar 2012
fittype() does not return a function: it returns an object. I do not know whether there is an argnames() method defined for that kind of object. (I do not have the curvefitting toolbox to test with.)
G A
il 16 Mar 2012
>> f = fittype( @(t, x) x+t );
argnames(f)
ans =
't'
'x'
>> f
f =
General model:
f(t,x) = x+t
Itamar
il 16 Mar 2012
Itamar
il 16 Mar 2012
Categorie
Scopri di più su Library Domains in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!