How does the MATLAB symvar function work? - matlab

I read the documentation and help about the symvar function but I'm still confused about how it works, especially for the following example:
syms x y a
symvar(x + y, 1)
When I run this M-file, I get the answer 'x'. Why am I getting this answer? Why not 'y'? And what is the role of the number 1 written as the second argument?

I'm guessing you typed help symvar into the MATLAB Command Window, which is normally the best first step, but in this case you may have come across a small pitfall. This is because symvar is an overloaded function: there is more than one copy, and the copy that gets called depends on the data type/class of the variables/objects that are passed to it. You can see all the versions by using the which function with the -all option. The output I get on R2018a is:
>> which symvar -all
C:\Program Files\MATLAB\R2018a\toolbox\matlab\funfun\symvar.m
C:\Program Files\MATLAB\R2018a\toolbox\curvefit\curvefit\#fittype\symvar.m % fittype method
C:\Program Files\MATLAB\R2018a\toolbox\symbolic\symbolic\#sym\symvar.m % sym method
C:\Program Files\MATLAB\R2018a\toolbox\matlab\funfun\#inline\symvar.m % inline method
Notice how there is a default version in ...\matlab\funfun, and then three more versions for fittype, sym, and inline objects. When you type help symvar you get the help for the first one, which isn't very helpful because you want the help for the overloaded sym method. To get this, you should type help sym/symvar, and you'll see something like this:
symvar Finds the symbolic variables in a symbolic expression or matrix.
symvar(S), where S is a scalar or matrix sym, returns a vector sym
containing all of the symbolic variables appearing in S. The
variables are returned in lexicographical order. If no symbolic variables
are found, symvar returns the empty vector.
The constants pi, i and j are not considered variables.
symvar(S,N) returns the N symbolic variables closest to 'x' or 'X'.
If N exceeds the number of variables appearing in S, or equals inf,
then all variables appearing in S are returned.
Upper-case variables are returned ahead of lower-case variables.
If S is a symbolic function the inputs to S are listed in front of the
other free variables.
Examples:
syms alpha a b x1 y
symvar(alpha+a+b) returns
[a, alpha, b]
symvar(cos(alpha)*b*x1 + 14*y,2) returns
[x1, y]
symvar(y*(4+3*i) + 6*j) returns
y
You can also use the online documentation for the newest MATLAB version: symvar
These make it clear what symvar does for symbolic variables. It will return a vector of all symbolic variables it finds in the expression you pass it as the first argument. If you specify a second argument as a number, it will only return up to that many symbolic variables, choosing first the ones that are alphabetically closest to 'x'.

Related

Passing vector input into MATLAB function

I am trying to write a function in MATLAB that takes 1x3 vectors as input. My code looks something like this:
function myFunction=([x1, x2, x3], [y1, y2, y3], [z1, z2, z3])
where all inputs are numbers, and then in the body of the function I perform some calculations indexing through the numerical values in the vectors. i want the vectors to be user input, so the user will enter the vectors and their components (x1, x2, etc.) into the function argument. However, I am getting an error saying "Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters." Therefore I believe I either have the syntax or something else wrong. I know MATLAB is supposed to be able to take vector input in functions, so please let me know what I am doing wrong. Thanks!
What you need to do is declare your function like this:
function myFunction(x,y,z)
% your function code here
end
Then within your function you can access the individual elements of the vectors using x(1), y(2), etc.
To call the function, including whatever number you like, you can enter on the Matlab command window (for example),
myFunction([1 2 3],[4 5 6],[7 8 9]) and the code in your function will be called with the x variable set to the vector [1,2,3], the y variable set to [4,5,6] and z to [7,8,9]. The use of commas to delineate values is optional. If your function then accesses y(2) it will get the second value of the y vector which will be 5 - it is important to note that indexing in Matlab is 1-based so the 1st element of x is obtained with x(1).
If you need to return values you can use:
function [a,b,c] = myFunction(x,y,z)
Then just assign the a, b or c in your code before the end statement.
See the offical Matlab documentation for more info.
I would add that much of the advantage of matlab is dealing with data in a vectorised form, so if you can avoid splitting out into separate elements I would do so. For example, if you need to add two vecors, you could do z = [x(1)+y(1), x(2)+y(2), x(3)+y(3)], but much better (more readable, more maintainable, faster) is z=x+y.

Making symbolic functions without hard-coding in MATLAB

I want to make symbolic functions theta1(t), theta2(t), theta3(t),...,thetaN(t) where N is some parameter I can define in MATLAB. I know that I can use something like sym('theta',[1 N]) to get [theta1, theta2, theta3,..., thetaN]. However, how can I do the same thing with theta being a function of t? The way to hard-code it would be like syms theta1(t) theta2(t) theta3(t) ... thetaN(t), but I want to make this general.
I do not want to directly use the sym command here because "support of character vectors that are not valid variable names and do not define a number will be removed in a future release", meaning something like sym('theta1(t)') would not be valid in future releases.
Any suggestions?
Figured part of it out. I could do something like the following
for i = 1:N
syms(strcat('theta',num2str(i),'(t)'))
end
However, if I want to assign a variable that contains all the symbolic expressions I'm still stuck. If I try
for i = 1:N
my_array(i) = syms(strcat('theta',num2str(i),'(t)'))
end
I get Error using syms (line 133). Using input and output arguments simultaneously is not supported. It works if I use sym instead of syms, but this leads to the warning I mentioned in my original post.

Matlab symbolic function conversion without dot for matrix operation

When converting symbolic expression to matlabFunction, expression like
x=sym('x')
f=- x^3/6 + x
g=matlabFunction(f)
-> #(x)x-x.^3.*(1.0./6.0)
which is not what I want because x is gonna be a matrix and my application requires actual matrix multiplication such as x^3 instead of the dot product form of x.^3
The only way to get it working is to use anonymous function, i.e.
g=#(x) - x^3/6 + x
->#(x)-x^3/6+x
However, the issue with anonymous function is that I cannot use substitution but to type the entire formulation, i.e.
g=#(x) f
-> #(x)f which shows that expression substitution does not work
In short, I will need to solve either one of the technical difficulties: (1) If I use matlabFunction, how do I remove all the dot after the conversion? or (2) If I use anonymous function, how do I bypass typing the symbolic expression if I have already defined 'f' for the expression?
I am totally lost here and I hope someone familiar with matlab can give me 2 cents.
Thank you!
You can convert the sym object to a string when calculating the anonymous function:
g=#(x)eval(char(f))
Alternatively, you can use the following code
h=eval(['#(x)' char(f)])
instead of matlabFunction

Using fzero to solve eqn in MatLab

I hope this is the right area. I'm trying to get this code to work in MatLab.
function y=test(x)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
I then jump to the command value and type this:
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
I then try to find the zeros of the first equation by typing this and I get errors:
solution=fzero(#test,5000000)
I'm getting the following error:
Error: File: test.m Line: 5 Column: 1 This statement is not
inside any function. (It follows the END that terminates the
definition of the function "test".)
New error
Error using fzero (line 289)
FZERO cannot continue because user supplied function_handle ==> #(x)
(test(x,B,b,a,r,p))
failed with the error below.
Subscript indices must either be real positive integers or logicals.
I would guess that this is a problem of scoping, you are defining variables (B, b, etc...) in the command line but trying to use them inside your test function where they are out of scope. You should alter your test function to take these in as parameters and then use an anonymous function so that your call to test in fsolve still only takes a single parameter:
function y=test(x, B, b, r, a, p)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
and
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
solution=fzero(#(x)(test(x,B,b,a,r,p)),5000000)
As an aside, unless you really do mean matrix multiplication, I would suggest that you replace all your *s and /s in test with the element-wise operators .* and ./. If you are dealing with scalars, it doesn't matter now, but it makes a big difference if you later want to scale your project and need a vectorized solution.
Regarding the errors you have added to your question:
You can't put code after the end in your function file. (With the exception of local functions). Your objective function should be an .m-file containing the code for one single function.
This is because in your test function you have ...b((1-(b/x)^(B-1))... which in MATLAB means you are trying to index the variable b in which case the value of (1-(b/x)^(B-1) has to be a positive integer. I'm guess you are missing a *
Your
function y=test(x)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
cannot access variables in your workspace. You need to pass the values in somehow. You could do something like:
function y=test(x,B,b,a,r,p)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
and then you can create an implicit wrapper function:
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
solution = fzero(#(x) test(x,B,b,a,r,p),5000000)
I haven't tested whether fzero returns sensible results, but this code shouldn't give an error.

MATLAB symbolic variables couldn't be used in nested function

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 ?
'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.
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
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.