2

I have a MATLAB function to solve a Inertia Tensor , and I have a nested function in my program . All the variables in it are symbolics but it told me

“Error using assignin: Attempt to add ”x“ to a static workspace”

and I don't understand why this happens . Here is my test.m code:


function test

syms x y z 
f=x
f1=f+1
f2=f1^2
    function r=test2        
        r=f2^3;
    end
f3=test2
end

After searching this web-forum I have found some answers . But at the same time I just don't understand it

Andrew Janke explianed it like this : While syms A may look like a static variable declaration, it isn't. It's just a regular function call. It's using Matlab's "command" invocation style to look like syntax, but it's really equivalent to syms('a', 'b', 'c'). on this page : Matlab: "Error using assignin: Attempt to add "c" to a static workspace"

what does static variable mean ?

I also search the HELP doc and it said :In functions and scripts, do not use syms to create symbolic variables with the same names as MATLAB® functions. For these names MATLAB does not create symbolic variables, but keeps the names assigned to the functions.

I only know syms x to create a symbolic variable in the workspace but why does the documentation say MATLAB does not create ?

1
  • If we attempt to dynamically add a variable to the workspace of an anonymous function, a nested function, or a function that contains a nested function, then MATLAB® issues an error of the form what does dynamically add a varibale mean ? Commented Nov 28, 2013 at 13:05

3 Answers 3

1

'Static' means fixed, 'workspace' is what Matlab calls the places where all of its variables are stored. For non-nested functions the workspace starts off as empty when Matlab is at the beginning of the function; as Matlab continues through function's lines of code it continuously add more variables to the workspace.

For functions with a nested function, Matlab first parses the function to see what variable will be created (it specifically looks for x = type lines), then it creates all of these variables (with value as 'unassigned'), and then only does it start to run through the code; but while running through the code, it can never create a new variable.

This is why the code

function TestNestedFunction
syms x;
  function Nested()

  end
end

generates an error, there is no x = to tell it to pre-create the unassigned variable x at the start of the code. It fails at syms x;, as that line tries to create a new variable x, which fails as it may not.

This is also why the following code runs

function TestNestedFunction
syms x;
x = x;
  function Nested()

  end
end

it sees the x = and then pre-creates x. (This is why your example of adding [x, y, z] = deal([]); also works).

You can test this with a break point at the beginning of simple non-nested function and a simple nested function. Just run it step by step.

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

Comments

0

This code works:

function test

x=sym('x')
y=sym('y')
z=sym('z')
f=x
f1=f+1
f2=f1^2
    function r=test2        
        r=f2^3;
    end
f3=test2
end

3 Comments

Thanks very much ! Yeah , it works , but I want to know the difference between syms and sym , they all creat symbolic variable(s) . In this case sym would be work but syms wouldn't . Could you please explain more about it ? @Daniel R
The problem has nothing to to with symbolic variables in special, it's common for all variables. sym and syms use assignin('caller'... to assign the variable to the caller workspace if no return variable is used. Using x=sym('x') makes the variable x available in nested functions as well.
Appreciate for your patience. Actually I noticed another way to solve it like this , just put [x y z]=deal([]) before syms x y z in a function that contains a nested function , it will work very well and the program could look like more concise . Just don't konw why
0

I think the pages you found are quite clear. You need to declare the variables one by one and use:

x = sym('x') 

Otherwise syms will try to assign the values into a workspace where this is not allowed.

2 Comments

sym only takes one input argument
Thank you all the same for answering ! Actually I want to know the reason why sym would be work but syms not . And more about the meaning of “ preallocating the variables ”, it seems prove that sym work in this way @DennisJaheruddin

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.