필터 지우기
필터 지우기

Creating Symbolic Functions

조회 수: 4 (최근 30일)
Itamar
Itamar 2012년 3월 15일
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
G A
G A 2012년 3월 15일
Thanks, Walter.
Walter Roberson
Walter Roberson 2012년 3월 16일
Dang, they introduced new functionality that used to be illegal.

댓글을 달려면 로그인하십시오.

채택된 답변

Alexander
Alexander 2012년 3월 16일
The concept of 'symbolic functions' including the function argnames have been introduced in MATLAB 2012a. You cannot use this feature in earlier versions.
  댓글 수: 2
G A
G A 2012년 3월 16일
That was my feeling.
Itamar
Itamar 2012년 3월 16일
I'm running MATLAB 7.12.0.635 (R2011a), so that explains it. I really should have checked what version of the software I was using. Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 3월 15일
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
Itamar
Itamar 2012년 3월 16일
I only started using MATLAB relatively recently, and I don't have a very clear picture of how things are structured; in particular I didn't realize there was a separate system responsible for performing symbolic computations. I'll probably go take a look at some MuPAD documentation now. Thanks for the help.

댓글을 달려면 로그인하십시오.


G A
G A 2012년 3월 15일
Could it be an alternative solution to use symvar() instead of argnames()?
syms x y
f = x+y;
symvar(f)
ans =
[ x, y]
  댓글 수: 6
Itamar
Itamar 2012년 3월 16일
I think that symvar is probably fine for me. And if I'm reading the documentation correctly, then symvar should return the variable names in lexicographic order:
http://www.mathworks.com/help/toolbox/symbolic/symvar.html
Also, it doesn't seem as though function evaluation notation works for 'sym' objects; it looks like I would be using the 'subs' function (or something similar), i.e.:
>> x = sym('x');
>> y = sym('y');
>> f = x+y^2;
>> subs(f,[x,y],[1,2])
ans =
5
>> subs(f,[y,x],[2,1])
ans =
5
So it seems like I could distinguish between asymmetric functions in this context.
Itamar
Itamar 2012년 3월 16일
And thanks!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by