i can't use syms in matlab

8 次查看(过去 30 天)
adem ski
adem ski 2019-12-17
clc
clear all
close all
a=1;
b=5;
sysm x real
adem=1+exp(-1);
dadem=diff(adem);
ddadem=diff(dadem);
ddadem_real=sub(ddadem,x);
[maxi,idx]=max(ddadem_real)
??? Undefined function or method 'sysm' for input arguments of type 'char'.
Error in ==> Unddddtitled at 6
sysm x real

回答(3 个)

Image Analyst
Image Analyst 2019-12-17
Did you mean to use/type syms instead of sysm?
Or do you have your own function called sysm? Because I'm not finding it in my help.
  8 个评论
Image Analyst
Image Analyst 2019-12-17
And when you wrote the line
ddadem_real=subs(ddadem,x);
or
ddadem_real=sub(ddadem,x);
exactly what did you think subs() or sub() would do?
adem ski
adem ski 2019-12-17
I want to get the max value

请先登录,再进行评论。


Fangjun Jiang
Fangjun Jiang 2019-12-17
run "ver symbolic". If you don't see the Symbolic Math Toolbox, then you don't have the Symbolic Math Toolbox.
  2 个评论
adem ski
adem ski 2019-12-17
yes
Symbolic Math Toolbox Version 5.2 (R2009a)
Fangjun Jiang
Fangjun Jiang 2019-12-17
Then see the answer from Image Analyst. You had a typo. It should be "syms", not "sysm".

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2019-12-17
adem=1+exp(-1);
That is a numeric scalar, nothing to do with the symbolic toolbox. You declared symbolic x on the line above but you do not use x here.
dadem=diff(adem);
diff() applied to a numeric array is the numeric difference operator, like adem(2:end)-adem(1:end-1) . It is not the mathematical derivative operator when applied to a numeric array, it is the finite difference operator when applied to numeric arrays. When you apply the finite difference operator to a numeric scalar, then because there is no second value to take the difference against, the result is the empty array.
ddadem_real=subs(ddadem,x);
If we ignore for a second that ddadem is the empty numeric array, then with x being a symbolic variable, the effect of subs(ddadem,x) would be to look inside ddadem for places where symbolic x occurred, and to look in the workspace for a current definition for x, and replace the symbolic x with the current definition of x. The current definition of x is as the symbolic variable x so if subs() did find symbolic variable x inside ddadem it would replace it with the same symbolic variable. The only change would be the side effect of converting the numeric ddadem to symbolic.
I recommend against using the two-input form of subs(): it is very easy to get wrong. For example if you had
syms x
y = x^2 + 5;
x = 7;
subs(y, x)
then this does not do the obvious of looking inside y for the variable named x and replacing it with the current numeric value of x. Instead, it would be an error, because at the time of the call, x would be a numeric variable but the second parameter of subs() must be a symbolic variable (or array of symbolic variables.) To achieve that effect you would need to do something like
syms x
x_variable_name = x;
y = x^2 + 5;
x = 7;
subs(y, x_variable_name)
then x_variable_name would have saved a copy of x when it was the symbolic variable, and that saved copy would provide the symbolic variable name x to subs() to permit subs() to know to look inside the workspace for a numeric variable named x to do the substitution.
In the large majority of cases if you use a symbolic variable name and assign a numeric value to the same variable later, you will get confused about whether the variable name is representing the symbolic variable or the numeric variable -- and if you do not get confused, then you can be sure that other people reading your code will get confused. I recommend strongly against risking it. I recommend instead something like
syms x
y = x^2 + 5;
x_numeric = 7;
subs(y, x, x_numeric)

Community Treasure Hunt

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

Start Hunting!

Translated by